-1

I am attempting to start cron automatically in an Ubuntu 20.10 (Groovy Gorilla) Docker container, thus far without success.

From previous searches (example), I've found a method to start cron using Dockerfile as follows:

# Install and enable cron
RUN apt-get install systemd -y
RUN apt-get install cron -y
RUN systemctl enable cron

# Copy cron file to the cron.d directory
COPY cronfile /etc/cron.d/cronfile

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

# Apply cron job
RUN crontab /etc/cron.d/cronfile

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

However, I can't make this work with my server setup. I have another, later, CMD in my Dockerfile (similar to this):

CMD ["/usr/sbin/run-lamp.sh"]

and of course only the second CMD will be run. I have tried combining multiple commands:

CMD cron && tail -f /var/log/cron.log && /usr/sbin/run-lamp.sh

but this does not run run-lamp.sh. I also tried putting the commands inside run-lamp.sh, but nothing has resulted in cron starting. Having said that, it is very easy to start cron manually, by opening up a shell in the container and entering the following:

# cron
# crontab /etc/cron.d/cronfile

I am open to suggestions.

All the files I'm working with are available here:

https://github.com/Downes/gRSShopper

In particular: Dockerfile:

https://github.com/Downes/gRSShopper/blob/master/Dockerfile

run-lamp.sh:

https://github.com/Downes/gRSShopper/blob/master/run-lamp.sh

cronfile:

https://github.com/Downes/gRSShopper/blob/master/cronfile

Thanks in advance.

Downes
  • 1
  • Typically you want to run only one process per container, which in this case would mean running a separate container running only a foreground cron daemon. Would this setup work for you? – David Maze Feb 17 '21 at 02:49
  • That's what I'm investigating now. – Downes Feb 17 '21 at 14:06

1 Answers1

0

First off, you don't need that tail -f /var/log/cron.log, it's useless in a container.

Secondly, tail -f is designed to only stop on a signal, and you never signal it, so it will not stop, and therefore the next command, run-lamp.sh, will not run.

Here's a minimal reproducer:

entrypoint.sh:

#!/bin/bash

touch /tmp/x
sleep 120

cronfile:

*   *   *   *   *   touch /tmp/y
# An empty line is required at the end of this file for a valid cron file.

Dockerfile:

FROM ubuntu:20.10

RUN apt-get update
RUN apt-get install systemd -y
RUN apt-get install cron -y
RUN systemctl enable cron

# Copy cron file to the cron.d directory
COPY cronfile /etc/cron.d/cronfile

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

# Apply cron job
RUN crontab /etc/cron.d/cronfile

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

COPY entrypoint.sh /entrypoint.sh
CMD /entrypoint.sh

Test command:

docker build -t lamp . \
    && docker rm -f lamp \
    && docker run -d --name lamp lamp \
    && echo waiting for cron... \
    && sleep 61 \
    && docker exec lamp ls /tmp \
    && docker exec lamp sh -c "ps -e | grep cron || echo no cron"

Result:

Sending build context to Docker daemon  71.17kB
Step 1/12 : FROM ubuntu:20.10
...
Successfully tagged lamp:latest
lamp
0fbe19e0583b178543ccf1d1108f72b7f3f6dffb664122621bc67d5939b66672
waiting for cron...
x
no cron

However, with this Dockerfile:

FROM ubuntu:20.10

RUN apt-get update
RUN apt-get install systemd -y
RUN apt-get install cron -y
RUN systemctl enable cron

# Copy cron file to the cron.d directory
COPY cronfile /etc/cron.d/cronfile

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

# Apply cron job
RUN crontab /etc/cron.d/cronfile

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

COPY entrypoint.sh /entrypoint.sh
# Run everything in parallel with '&', even the useless tail command
CMD /entrypoint.sh & cron & tail -f /var/log/cron.log

Result:

Sending build context to Docker daemon  87.55kB
Step 1/11 : FROM ubuntu:20.10
...
Successfully tagged lamp:latest
lamp
99dca45fe135326ca96ea90fe21ff7ae23689a56fab5cf0c2ccd8252bc4be84a
waiting for cron...
x
y
     10 ?        00:00:00 cron
root
  • 5,528
  • 1
  • 7
  • 15