微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我无法获得由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版本1.13.1,版本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运行它将在stdout中将文件拖尾(忽略尾警告):

$docker run --name test -t test/test
tail: unrecognized file system type 0x794c7630 for '/var/log/mylog.log'.   please report this to bug-coreutils@gnu.org. 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使用早期命令启动测试.

到底发生了什么?在docker build中创建文件与live容器中的运行脚本有什么不同吗?

最佳答案
对于我在Docker 4.9.8-moby Alpine版本上使用overlay2存储驱动程序,同样的问题也出现了.

似乎CMD tail正在从RUN touch /var/log/mylog.log创建的覆盖层打开/var/log/mylog.log文件.

当您追加到日志时,会在最顶层的覆盖层中创建一个“新”文件,容器在运行时用于对图像顶部进行的任何文件系统更改,并且实际上正在附加此新文件.但是,无论是-f还是-F,tail都无法正确接收转换.

docker start和docker stop解决了问题,因为在更新/var/log/mylog.log之后尾部进程再次启动,然后指向容器覆盖层中的“new”文件.使用略有不同的CMD将以类似的方式解决问题:

CMD ["sh","-c","touch /var/log/mylog.log && tail -f /var/log/mylog.log"]

debian:测试图像包括coreutils-8.26-2,修复了支持叠加幻数以删除该警告消息,但仍然表现出相同的行为.

这很可能是在内核中修复的重叠问题.在使用-F时,coreutils可能能够解决这个问题.

你在尝试的是Docker中的一个边缘案例.使用tail作为前台进程的容器通常在运行tail之前在脚本中完成大量工作,其中包括运行创建要挂起的日志文件的命令.可能是为什么很多人都没有选择这个.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐