【问题标题】:Docker ubuntu cron tail logs not visibleDocker ubuntu cron 尾日志不可见
【发布时间】:2018-12-26 05:09:15
【问题描述】:

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

我正在使用 docker-compose。

docker-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

现在我想也许我不需要登录到文件?可以将此附加到sttoud,但不确定如何执行此操作。

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

【问题讨论】:

    标签: docker logging cron docker-compose tty


    【解决方案1】:

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

    【讨论】:

    【解决方案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 up 时的输出:

    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
    

    我的理解是以上就是目标。

    【讨论】:

    • 对我不起作用。文件中的“RUN ln -sf”结果始终为空。取出它并填充日志,但它们不会流式传输到 docker compose 输出
    • @Robbo_UK 在 OP 中提到我们不需要文件中的输出。让我知道您是否想要在文件和标准输出中都使用它。此外,我在运行容器时使用标准输出对我的帖子进行了编辑。
    • 输出到sttdout没问题。你的 hello-cron 是什么样子的。
    • 和你的一样。基本上只需添加我引用的行并运行“docker-compose up”。
    • 嗯它不适合我......我认为这是正确的答案。我已经把它放在github上。一定是错过了什么不同的东西吗? github.com/davidsonr/docker-ubuntu-cron
    【解决方案3】:

    由于 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

    【讨论】:

    • 因为这不会 logrotate cron.log 文件,它可能会根据您的 cron 输出变得非常大..
    猜你喜欢
    • 2020-01-13
    • 2020-01-25
    • 2015-08-12
    • 1970-01-01
    • 2019-12-11
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多