代码之家  ›  专栏  ›  技术社区  ›  Robbo_UK

docker ubuntu cron tail日志不可见

  •  1
  • Robbo_UK  · 技术社区  · 6 年前

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

    我在用Docker合成。

    复方制剂

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

    克朗/你好克朗

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

    上面的运行良好,其输出日志在容器内,但他们不是流到码头。

    例如 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

    3 回复  |  直到 6 年前
        1
  •  3
  •   Robert    6 年前

    由于Docker层和索引节点有些古怪,您必须在命令期间创建文件:

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

    这对file和stdout都有效:

    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 在前一个状态下继续监听,因此会丢失对“新文件”的每个更新。 Credits BMitch

        2
  •  3
  •   alexkb    6 年前

    我尝试了您的设置,以下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
    

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

        3
  •  0
  •   Kilian    6 年前

    尝试对此重定向 > /dev/stdout ,之后您应该会看到您的日志和Docker日志。