10

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

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.

4
  • Ah, and there is that on top of everything else :) To OP: This doesn't invalidate my explanation and you would essentially have to replace your CMD with RUN. – Sven May 22 '17 at 8:54
  • 2
    I think to call this an issue is a little off. This behavior is by design. Containers are not mini-OSes. They are application-centric. – JimmyJames May 22 '17 at 17:26
  • Agreed. My intention was to clarify that the OP's problem lay in their docker config rather than any details of cron. – Paul Haldane May 22 '17 at 17:33
  • This is not a docker issue. It is by design. – Andrew Savinykh May 22 '17 at 19:13
5

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.

3
  • 2
    I have only a passing familiarity with cron but I think in order to make this sensible, the CMD would need to run crond. Otherwise, the container will exit after the crontab command finishes. – JimmyJames May 22 '17 at 17:24
  • @JimmyJames What you are saying is correct, but like I explained above, the better solution is to run these commands from an outside cron as one-off docker runs. More container-y. :) – Artur Ciesielski May 23 '17 at 13:37
  • You could be right. The question is interesting because it's not clear whether it makes sense to run crond as your container's command. It might be workable but it does seem awkward in a container. – JimmyJames May 23 '17 at 14:25
3

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.

2
  • I think I understand this. My build settings are on user but my run settings are attempting to run from the root's crontab? that makes sense, i'll try to work around that. I'm guessing you didn't mean: ADD . /etc/crontab as that doesn't work. – TomFirth May 22 '17 at 8:18
  • @TomFirth: This doesn't make much sense. Please read my edit for further details. – Sven May 22 '17 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.