Run Cron Inside a Docker Container
To run cron in Docker you must install cron, add your crontab, and keep the daemon in the foreground so the container stays alive. Environment variables also need to be passed through explicitly.
Minimal Dockerfile
FROM debian:stable-slim
RUN apt-get update && apt-get install -y cron
COPY mycron /etc/cron.d/mycron
RUN chmod 0644 /etc/cron.d/mycron && crontab /etc/cron.d/mycron
CMD ["cron", "-f"]
Keep cron in the foreground
cron -f runs in the foreground so the container doesn't exit immediately. Without -f the daemon backgrounds and Docker stops the container.
Pass environment variables
Cron does not inherit the container's env. Dump it at startup and source it in the job, or write the vars into the crontab file: printenv | sed 's/^/export /' > /etc/environment in your entrypoint, then reference them.
See the output
Redirect job output to /proc/1/fd/1 so it appears in docker logs: * * * * * root /app/job.sh > /proc/1/fd/1 2>&1.
FAQ
How do I run cron inside a Docker container?
Install cron in the Dockerfile, copy your crontab to /etc/cron.d, and set CMD to cron -f so the daemon runs in the foreground and keeps the container alive.
Why does my Docker cron container exit immediately?
You started cron without -f. Use cron -f (foreground) as the container command; a backgrounded daemon lets Docker stop the container.
Why don't my Docker cron jobs see environment variables?
Cron doesn't inherit the container env. Export the variables to a file in your entrypoint and source them in the job, or put them at the top of the crontab.