docker - no crontab for root

My Dockerfile appears to build correctly (it tells me so). When I run the container, I get the below error message. I have tried running the commands (CMD) with and without the service's directory.

crontab.sh basically writes a cron schedule to a text file (cron.jobs) and then imports the text file to crontab.

Dockerfile:

FROM node:0.10
MAINTAINER Tom

VOLUME /var/log/

RUN mkdir /pulse
ADD . /pulse
WORKDIR /pulse

RUN apt-get update && apt-get install -y cron

ADD *.sh /pulse/
RUN chmod 750 /pulse/crontab.sh && chmod 750 /pulse/

RUN chmod 644 /etc/crontab

CMD cron -f
CMD touch /var/log/cron.log && sh /pulse/crontab.sh && tail -f /var/log/cron.log
CMD cron /pulse/cron.jobs
CMD crontab -l

edited to add crontab.sh

crontab.sh (some crons have been removed):

#!/bin/bash

cat <<- 'EOF' > cron.jobs

0 * * * * node /pulse/scripts/awsPulseTest.js > /tmp/awsPulseTest.log 2>&1

EOF

crontab cron.jobs

Error:

no crontab for root

Side notes:

  • Pulse is the name of the service.
  • The version of node is old due to the service, this will be upgraded.
  • The service is essentially for cron jobs in node

It's an issue with the dockerfile (rather than the commands in the file). Only one CMD is run (the last one) - see https://docs.docker.com/engine/reference/builder/#cmd

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

As the other answers have already explained, only one CMD will be run per Dockerfile and the command you want to run is wrong.

But there is a more pressing issue with your setup IMO - Docker containers are not usually designed to work this way. What you should do instead is running the cron services from the host (or your orchestrator) as one-off processes (probably using something like docker run or docker-compose run, or, if for some reason you don't want to start a separate container for this, I guess you could use docker exec).

This is just my view on how containers should be used though, so obviously you should take it with a grain of salt.

If you add this to /etc/crontab, this wouldn't show up in root's personal crontab, as this contains only the user-specific crontab edited with crontab -e, not the system-wide one in /etc.


More details:

My guess is that /pulse/crontab.sh (which you don't show, why?) adds the relevant crontab line to the system wide crontab file /etc/crontab. You later execute the command crontab -l, but this only shows an error because it lists roots personal crontab only (which happens to be empty), not the system-wide one in /etc/crontab. This is all perfectly normal and expected. To show the line your script added, you would replace CMD crontab -l with CMD cat /etc/crontab.

All of this has nothing to to with any dockerfile commands like ADD, RUN or CMD, it's just basic Linux stuff.

A combination of cron - docker - no crontab for root - Server Fault and cron - docker - no crontab for root - Server Fault helped to solve this.