Restart a Docker Container with Cron
To restart a Docker container on a schedule, call docker restart from cron with the container name and the absolute path to the docker binary. For crash recovery, a restart policy is usually better than cron.
The crontab line
Run sudo crontab -e (docker usually needs root) and add (4 AM daily):
0 4 * * * /usr/bin/docker restart my-container >> /var/log/docker-restart.log 2>&1
Use the absolute path
Find docker with which docker (often /usr/bin/docker). Cron's PATH won't include it otherwise, giving 'docker: command not found'.
Permissions
The cron user must be root or in the docker group. Root via sudo crontab -e is simplest for scheduled maintenance.
When to use a restart policy instead
If the goal is to recover from crashes, use docker run --restart unless-stopped (or restart: unless-stopped in compose) rather than cron. Use cron only for a deliberate scheduled restart, e.g. to clear memory.
FAQ
How do I restart a Docker container every night with cron?
0 4 * * * /usr/bin/docker restart my-container runs at 4 AM daily. Use the absolute path to docker and run it as root or a docker-group user.
Why does docker not work in my crontab?
Cron's minimal PATH doesn't include docker, and the user may lack permission. Use /usr/bin/docker and run via sudo crontab -e as root.
Should I use cron or a restart policy?
For crash recovery use --restart unless-stopped. Use cron only for a scheduled restart on purpose, such as a nightly memory cleanup.