Docker ubuntu cron tail日志不可见

问题描述 投票:1回答:3

尝试运行具有cron调度的docker容器。但是我不能使它成为输出日志。

我正在使用docker-compose。

泊坞窗,compose.yml

---
version: '3'
services:
  cron:
    build:
      context: cron/
    container_name: ubuntu-cron

的cron / Dockerfile

FROM ubuntu:18.10

RUN apt-get update
RUN apt-get update && apt-get install -y cron 

ADD hello-cron /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD cron && tail -F /var/log/cron.log

的cron / HELLO-的cron

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1

以上运行在容器内输出日志很好但是它们没有流式传输到docker。

例如docker logs -f ubuntu-cron返回空结果

如果你登录容器docker exec -it -i ubuntu-cron /bin/bash你有日志。

cat /var/log/cron.log 
Hello world
Hello world
Hello world

现在我想,也许我不需要登录文件?可以把它附加到stroud但不知道如何做到这一点。

这看起来很相似...... How to redirect cron job output to stdout

docker logging cron docker-compose tty
3个回答
2
投票

由于docker图层和inode中的一些奇怪现象,您必须在CMD期间创建文件:

CMD cron && touch /var/log/cron.log && tail -F /var/log/cron.log

这适用于文件和标准输出:

FROM ubuntu:18.10

RUN apt-get update
RUN apt-get update && apt-get install -y cron

ADD hello-cron /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Create the log file to be able to run tail
# Run the command on container startup
CMD cron && touch /var/log/cron.log && tail -F /var/log/cron.log

解释似乎是这样的:

在原始帖子中,tail命令开始“监听”位于图像层中的文件,然后当cron将第一行写入该文件时,docker将文件复制到新层,即容器层(因为性质)复制和写入文件系统,docker的工作方式)。因此,当文件在新层中创建时,它会获得不同的inode,并且tail会继续在之前的状态中进行侦听,因此会丢失对“新文件”的每次更新。 Credits BMitch


2
投票

我尝试了你的设置和以下Dockerfile工作:

FROM ubuntu:18.10

RUN apt-get update
RUN apt-get update && apt-get install -y cron

ADD hello-cron /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0755 /etc/cron.d/hello-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Symlink the cron to stdout
RUN ln -sf /dev/stdout /var/log/cron.log

# Run the command on container startup
CMD cron && tail -F /var/log/cron.log 2>&1

另请注意,我正在使用“docker-compose up”而不是docker来装容器。在这个特定的例子中没关系,但如果你的实际解决方案更大,那可能很重要。

编辑:这是我运行docker-compose时的输出:

neekoy@synchronoss:~$ sudo docker-compose up
Starting ubuntu-cron ... done
Attaching to ubuntu-cron
ubuntu-cron | Hello world
ubuntu-cron | Hello world
ubuntu-cron | Hello world

在日志中显然相同:

neekoy@synchronoss:~$ sudo docker logs daf0ff73a640
Hello world
Hello world
Hello world
Hello world
Hello world

我的理解是以上是目标。


0
投票

尝试重定向此> /dev/stdout,之后您应该看到带有docker日志的日志。

© www.soinside.com 2019 - 2024. All rights reserved.