Need to schedule tasks inside a Docker container? Whether it’s a backup script, data sync, or a periodic cleanup, cron jobs are a simple and reliable tool. But running them inside a Docker container requires a few extra steps.
In this post, you’ll learn how to:
- Set up and run cron inside a container
- Keep it running properly
- Debug common issues with scheduled jobs
🧠 Why Cron in Docker?
By default, Docker containers run a single foreground process. Cron is a background daemon, so you can’t just RUN crontab
and expect it to keep going. You’ll need to explicitly:
- Install
cron
- Add your cron jobs
- Ensure the container keeps running
✅ Step-by-Step: Running Cron in Docker
🏗️ 1. Create a Dockerfile
Here’s an example for a Debian-based image:
FROM ubuntu:22.04
# Install cron and any other dependencies
RUN apt-get update && \
apt-get install -y cron && \
apt-get clean
# Add your cron job file
COPY my-cron-job /etc/cron.d/my-cron-job
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/my-cron-job
# Apply the cron job
RUN crontab /etc/cron.d/my-cron-job
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Start cron and keep container running
CMD cron && tail -f /var/log/cron.log
📄 2. Create the Cron Job File
my-cron-job:
* * * * * root echo "Cron job ran at $(date)" >> /var/log/cron.log 2>&1
⏰ This job runs every minute and logs the timestamp. Adjust timing and command as needed.
Make sure:
- The file ends with a newline
- It has appropriate permissions (
0644
) - The user (e.g.,
root
) is specified in the cron line
🔁 3. Build and Run the Container
docker build -t cron-container .
docker run -d --name cron-job cron-container
Check logs:
docker logs cron-job
You should see entries from /var/log/cron.log
appearing every minute.
🧪 Debugging Tips
- 🧵 Job not running? Make sure your cron file ends with a newline and has the correct syntax
- 🔒 Permission denied? Ensure the cron job is owned by
root
and has0644
permissions - 📦 Cron not installed? Double-check your base image and installation steps
- 🛑 Container exits? Ensure
CMD
keeps the container running (e.g., withtail -f
)
🧰 Alternative: Use Host Cron to Call Docker
If you don’t want to run cron inside the container, a cleaner approach may be:
Run cron on the host and call Docker from it:
* * * * * docker exec my-container /path/to/script.sh
✅ Easier to manage, monitor, and avoids running multiple daemons in a container.
📝 Conclusion
While Docker wasn’t designed to run background daemons like cron, it’s possible with a bit of setup. Whether you run cron inside a container or use the host’s scheduler to invoke containers, you have flexible options to automate tasks in a containerized environment.
🔑 Summary
Task | Command/Step |
---|---|
Install cron | apt install cron |
Add cron job | Copy file to /etc/cron.d/ |
Run container with cron | CMD cron && tail -f /var/log/cron.log |
Build and run | docker build → docker run -d |
Alternative (host cron) | * * * * * docker exec ... |