The output of `tail -f` at the end of the CMD docker is not displayed

Using Docker for Mac 1.13.1 with the following Docker file:

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

#Install packages and clean downloaded packages in the lowest layer
RUN apt-get update && apt-get -y install cron && rm -rf /var/lib/apt/lists/*

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron

# Give execution rights on the cron job and create the log file to tail in the next layer
RUN chmod 0644 /etc/cron.d/hello-cron && touch /var/log/cron.log

# Run the command on container startup
CMD echo "starting" && echo "continuing" && (cron) && echo "tailing..."  && tail -f /var/log/cron.log

With the contab file:

* * * * * root echo "Hello world `date`" >> /var/log/cron.log 2>&1
# Don't remove the empty line at the end of this file. It is required to run the cron job

When I create and run it with:

docker build -t docker-cron-master .
docker run docker-cron-master

I see the result:

docker run docker-cron-master
starting
continuing
tailing...

If I wait a minute, the output tail -fwill not appear. However, if I enter the running container and the tail of the file, I can see the contents:

$ docker exec -it 4eda6b1fc6ce bash
root@4eda6b1fc6ce:/# tail -f /var/log/cron.log
Hello world Fri May  5 09:53:01 UTC 2017
Hello world Fri May  5 09:54:01 UTC 2017

I tried adding another echo at the end of the CMD to make sure that this was only the last command that STDOUT swallowed, but that did not help.

I posted the code on github at https://github.com/simbo1905/docker-cron

Thanks!

+6
source share
2 answers

docker copy-on-write fs. , , , .

, /var/log/cron.log, , , tail, , ., docker exec . , "" , , copy-on-write:

CMD echo "starting" && echo "continuing" && (cron) \
 && echo "tailing..." && : >> /var/log/cron.log && tail -f /var/log/cron.log

, : https://gist.github.com/sudo-bmitch/f91a943174d6aff5a57904485670a9eb

+7

: VOLUME /var/log/

0

Source: https://habr.com/ru/post/1017016/


All Articles