如何从正在运行的Docker容器中读取文件和标准输出?

huangapple go评论101阅读模式
英文:

How to read files and stdout from a running Docker container

问题

你可以通过以下步骤在主机上启动一个应用程序,以便读取正在运行的 Docker 容器中的文件和标准输出:

  1. 首先,使用以下命令启动 Docker 容器:

    docker start containerid
    
  2. 然后,你需要在主机上创建一个应用程序,用于读取容器生成的文件和标准输出。你可以使用编程语言(如Python)编写这个应用程序,并使用适当的库来与 Docker 容器进行通信。

    对于文件的读取,你可以使用 Docker 提供的 docker cp 命令将文件从容器复制到主机上的指定目录,然后在应用程序中读取这些文件。

    对于标准输出的读取,你可以使用 Docker 提供的 docker logs 命令来获取容器的日志输出。你可以将这些日志输出重定向到一个文件,然后在应用程序中读取该文件。

    请注意,你需要在主机上安装 Docker,并具有适当的权限来执行这些操作。

另外,你还可以创建另一个 Docker 容器,用于读取另一个容器的文件和标准输出。这是可能的,你可以在第二个容器中安装 Docker 客户端,并使用相同的方法来读取文件和标准输出。

英文:

How would I go about starting an application in my host machine in order to read files and stdout from a running docker container?

Essentially I want to do this:

docker start containerid   
./myapp // This app will *somehow* have access files and stdout generated by the container I just stared. 

How would I go about doing that? To be more specific with where I am trying to go with this; I want to read the logs and stdout of a docker container and have those logs processed somewhere else.

I am also willing to create another docker container which can read files and stdout from another container, but I don't know if that's possible.

答案1

得分: 134

通过docker logs $containerid命令(使用-f参数可以持续查看日志)可以获取由Docker容器启动的进程的标准输出(stdout)。另一种选择是通过docker远程API直接流式传输日志。

如果需要访问日志文件(仅在必要时考虑将日志记录到stdout或其他标准解决方案,如syslogd),您唯一的实时选项是配置一个卷(就像Marcus Hughes建议的那样),以便将日志存储在容器之外,并可从主机或另一个容器进行处理。

如果不需要实时访问日志,可以使用docker export命令以tar格式导出文件

英文:

The stdout of the process started by the docker container is available through the docker logs $containerid command (use -f to keep it going forever). Another option would be to stream the logs directly through the docker remote API.

For accessing log files (only if you must, consider logging to stdout or other standard solution like syslogd) your only real-time option is to configure a volume (like Marcus Hughes suggests) so the logs are stored outside the container and available for processing from the host or another container.

If you do not need real-time access to the logs, you can export the files (in tar format) with docker export

答案2

得分: 16

要查看标准输出(stdout),你可以使用-i参数启动Docker容器。当然,这并不能让你离开已启动的进程并探索容器。

docker start -i containerid

或者,你可以在以下位置查看容器的文件系统:

/var/lib/docker/containers/containerid/root/

然而,这两种方法都不是理想的。如果你想查看日志或任何持久化存储,正确的做法是在使用docker run命令时使用-v开关来挂载一个卷。这样,你可以在主机上检查日志文件,或者将它们附加到另一个容器并在那里检查。

英文:

To view the stdout, you can start the docker container with -i. This of course does not enable you to leave the started process and explore the container.

docker start -i containerid

Alternatively you can view the filesystem of the container at

/var/lib/docker/containers/containerid/root/

However neither of these are ideal. If you want to view logs or any persistent storage, the correct way to do so would be attaching a volume with the -v switch when you use docker run. This would mean you can inspect log files either on the host or attach them to another container and inspect them there.

1: https://docs.docker.com/engine/reference/commandline/volume_create/ "attaching a volume"

答案3

得分: 9

有点晚了,但这就是我正在使用journald做的事情。它非常强大。

你需要在安装有systemd-journald的操作系统上运行你的Docker容器。

docker run -d --log-driver=journald myapp

这将把所有内容传输到主机的journald中,journald会处理日志修剪、存储格式等,并为您提供一些很酷的选项来查看它们:

journalctl CONTAINER_NAME=myapp -f

这将将日志实时输出到控制台,

journalctl CONTAINER_NAME=myapp > output.log

这将把所有日志保存到一个文件中,供您带走,或者

journalctl CONTAINER_NAME=myapp --since=17:45

此外,如果您更喜欢,您仍然可以通过docker logs ....来查看日志。

不再需要> my.log-v "/apps/myapp/logs:/logs"等操作了。

英文:

A bit late but this is what I'm doing with journald. It's pretty powerful.

You need to be running your docker containers on an OS with systemd-journald.

docker run -d --log-driver=journald myapp

This pipes the whole lot into host's journald which takes care of stuff like log pruning, storage format etc and gives you some cool options for viewing them:

journalctl CONTAINER_NAME=myapp -f

which will feed it to your console as it is logged,

journalctl CONTAINER_NAME=myapp > output.log

which gives you the whole lot in a file to take away, or

journalctl CONTAINER_NAME=myapp --since=17:45

Plus you can still see the logs via docker logs .... if that's your preference.

No more > my.log or -v "/apps/myapp/logs:/logs" etc

答案4

得分: 6

你可以查看容器的文件系统,路径为:

/var/lib/docker/devicemapper/mnt/$CONTAINER_ID/rootfs/

你可以使用以下命令来实时查看日志文件:

tail -f mylogfile.log
英文:

You can view the filesystem of the container at

/var/lib/docker/devicemapper/mnt/$CONTAINER_ID/rootfs/

and you can just

tail -f mylogfile.log

答案5

得分: 4

在Docker容器和主机系统之间共享文件,或在不同容器之间共享文件,最好使用volumes(卷)来实现。

将您的应用程序运行在另一个容器中可能是最佳解决方案,因为这将确保您的整个应用程序能够良好地隔离和轻松部署。您正在尝试的操作听起来与这篇优秀的博客文章中描述的设置非常接近,请参阅该文章!

英文:

Sharing files between a docker container and the host system, or between separate containers is best accomplished using volumes.

Having your app running in another container is probably your best solution since it will ensure that your whole application can be well isolated and easily deployed. What you're trying to do sounds very close to the setup described in this excellent blog post, take a look!

1: https://docs.docker.com/userguide/dockervolumes/ "volumes"
2: http://crosbymichael.com/advanced-docker-volumes.html

答案6

得分: 0

我正在Linux上运行这个。我最终做的是在docker run中删除-t选项,然后通过tr -d将输出通过管道删除\r字符。

所以我有:

docker run -i | tr -d '\r'

这样的输出可以被捕获并存储在一个bash变量中。这实际上是有意义的。

英文:

I'm running this in Linux.

What i ended up doing is removing the -t option in docker run, and then piping the output through tr -d to delete the \r characters.

So I have:

docker run -i <containerName> <binaryname> | tr -d '\r'

The output of this can be captured and stored in a bash variable. And it actually makes sense.

huangapple
  • 本文由 发表于 2014年7月8日 06:41:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/24621067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定