OStack程序员社区-中国程序员成长平台

标题: docker - 为什么我无法获得由 docker build 创建的文件尾部的输出 [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-3 10:10
标题: docker - 为什么我无法获得由 docker build 创建的文件尾部的输出

docker 信息:

Containers: 18
 Running: 18
 Paused: 0
 Stopped: 0
Images: 188
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1
runc version: 9df8b306d01f59d3a8029be411de015b7304dd8f
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.0-1-amd64
Operating System: Debian GNU/Linux 9 (stretch)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 15.56 GiB
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Experimental: false
Insecure Registries:
   127.0.0.0/8
Live Restore Enabled: false

Docker 版本:Docker version 1.13.1, build 092cba372
我有这个 docker 文件:
FROM debian:latest
RUN touch /var/log/mylog.log
CMD ["tail", "-F", "/var/log/mylog.log"]

使用 docker build . -t test/test 构建它并使用 docker run -ti test/test 运行它将像这样在标准输出中拖尾文件(忽略尾部警告):
$ docker run --name test -t test/test
tail: unrecognized file system type 0x794c7630 for '/var/log/mylog.log'.   please report this to [email protected]. reverting to polling

执行以下命令将写入文件 /var/log/mylog.log这是一个文件,后面跟着 tail :
docker exec -ti test bash -c "echo 'asd' >> /var/log/mylog.log"

不幸的是,即使cat,另一个终端也没有输出显示文件中有内容:
$ docker exec -ti test bash -c "cat /var/log/mylog.log"
asd

虽然,如果我使用 PID1 创建文件而不是在 dockerfile 中创建它,我会看到带有尾部的文件内容。
如果我docker stop test && docker start test,我也可以获得尾部的内容使用较早的命令。

到底是怎么回事?在 docker build 中创建文件与在实时容器中运行脚本时有什么不同吗?



Best Answer-推荐答案


使用 overlay2 的我也遇到了同样的问题Docker 上的存储驱动程序 4.9.8-moby Alpine 释放。

好像CMD tail正在开通/var/log/mylog.log来自覆盖层的文件 RUN touch /var/log/mylog.log创造。

当您附加到日志时,会在最顶层的覆盖层中创建一个"new"文件,容器用于在运行时对镜像顶部进行的任何文件系统更改,并且这个新文件实际上是附加到的。 tail但是,无论是使用 -f 还是无法正确进行转换或 -F .
docker startdocker stop将问题解决为 tail进程在 /var/log/mylog.log 之后再次启动已更新,然后指向容器覆盖层中的"new"文件。使用略有不同的 CMD将以类似的方式解决该问题:

CMD ["sh", "-c", "touch /var/log/mylog.log && tail -f /var/log/mylog.log"]
debian:testing图像包括 coreutils-8.26-2 与支持覆盖魔数(Magic Number)的修复程序,以删除该警告消息,但仍表现出相同的行为。

这很可能是内核中要修复的覆盖问题。使用 -F 时,coreutils 或许能够解决这个问题。 .

不过,您正在尝试的是 Docker 中的一个边缘案例。使用 tail 的容器因为前台进程通常在运行之前在脚本中完成一堆工作tail ,其中包括运行创建要跟踪的日志文件的命令。这可能就是为什么没有多少人选择这个的原因。

关于docker - 为什么我无法获得由 docker build 创建的文件尾部的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42614853/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4