docker - no crontab for root
Solution 1
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.
Solution 2
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.
Solution 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 root
s 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.
Related videos on Youtube
TomFirth
Keen to learn everything I can. Quite a bit of experience in breaking websites with php and am now moving that knowledge to Javascript.
Updated on September 18, 2022Comments
-
TomFirth over 1 year
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
-
TomFirth almost 7 yearsA combination of serverfault.com/a/851500/395574 and serverfault.com/a/851479/395574 helped to solve this.
-
TomFirth almost 7 yearsI 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. -
FooBee almost 7 years@TomFirth: This doesn't make much sense. Please read my edit for further details.
-
FooBee almost 7 yearsAh, 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
withRUN
. -
JimmyJames almost 7 yearsI 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 almost 7 yearsI think to call this an issue is a little off. This behavior is by design. Containers are not mini-OSes. They are application-centric.
-
Paul Haldane almost 7 yearsAgreed. My intention was to clarify that the OP's problem lay in their docker config rather than any details of cron.
-
Andrew Savinykh almost 7 yearsThis is not a docker issue. It is by design.
-
Artur Ciesielski almost 7 years@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 run
s. More container-y. :) -
JimmyJames almost 7 yearsYou 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.