Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker ubuntu cron tail logs not visible

Trying to run a docker container that has a cron scheduling. However I cannot make it output logs.

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

The above runs fine its outputting logs inside the container however they are not streamed to the docker.

e.g. docker logs -f ubuntu-cron returns empty results

but

if you login to the container docker exec -it -i ubuntu-cron /bin/bash you have logs.

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

Now Im thinking that maybe I dont need to log to a file? could attach this to sttoud but not sure how to do this.

This looks similar... How to redirect cron job output to stdout

like image 807
Robbo_UK Avatar asked Jul 18 '18 10:07

Robbo_UK


People also ask

How can I see the logs inside a docker container?

In order to review a container's logs from the command line, you can use the docker logs <container-id> command. Using this command, the logs shown above are displayed this way: Hello there!

How do I see cron job history?

See Access the Visual Modeler for information on how to access the Visual Modeler home page. Click the System Administration tab. Click the Job Scheduler tab. In the list of cron jobs, identify the job whose history you want to view.

Where are crontab logs stored?

By default installation the cron jobs get logged to a file called /var/log/syslog . You can also use systemctl command to view last few entries.

Where are the docker logs?

By default, Docker stores log files in a dedicated directory on the host using the json-file log driver. The log file directory is /var/lib/docker/containers/<container_id> on the host where the container is running.


2 Answers

Due to some weirdness in the docker layers and inodes, you have to create the file during the CMD:

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

This works both for file and 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

The explanation seems to be this one:

In the original post tail command starts "listening" to a file which is in a layer of the image, then when cron writes the first line to that file, docker copies the file to a new layer, the container layer (because of the nature of copy-and-write filesystem, the way that docker works). So when the file gets created in a new layer it gets a different inode and tail keeps listening in the previous state, so looses every update to the "new file". Credits BMitch

like image 152
Robert Avatar answered Sep 22 '22 17:09

Robert


I tried your setup and the following Dockerfile works:

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

Also note that I'm bringing the container up with "docker-compose up" rather than docker. It wouldn't matter in this particular example, but if your actual solution is bigger it might matter.

EDIT: Here's the output when I run 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

Same in the logs obviously:

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

My understanding is that the above is the goal.

like image 34
Neekoy Avatar answered Sep 25 '22 17:09

Neekoy