This website collects cookies to deliver better user experience
How to run a cron as a non-root user in Docker 🦾
How to run a cron as a non-root user in Docker 🦾
Yesterday, I was looking to add a cron to an existing Docker image, but it took longer than I would like. Even though I previously had a recipe for adding a cron to a Docker image, I had new contraints that made this solution unsuitable... 😐
Let's go over all the constraints I had:
Using a Debian-based image
The user running the image is a unprivileged user
The cron is not the only process
There is an entrypoint script
After searching on the internet, I finally found a working solution with the help of this Stack Overflow answer. So if anyone needs it, this article could serve as a reference. 😁
I hope again it will helps you as well! 😎
Dockerfile
Make sure to replace APPLICATION by the directory you want and NON_ROOT_USER by the name of your unprivileged user. 😉
# https://hub.docker.com/_/debianFROM debian:buster-20210511WORKDIR /opt/APPLICATION# https://packages.debian.org/search?suite=buster&keywords=cron# Install packages for our applicationRUN apt-get update && apt-get -yinstall\
# Cron to periodically restart some services
cron=3.0pl1-134+deb10u1 \
&& echo "*/5 * * * * /opt/APPLICATION/restart.sh > /proc/1/fd/1 2>&1" >> /etc/cron.d/restart-cron \
# Give the necessary rights to the user to run the cron
&& crontab -u NON_ROOT_USER /etc/cron.d/restart-cron \
&& chmod u+s /usr/sbin/cron
COPY --chown=NON_ROOT_USER:NON_ROOT_USER . .USER NON_ROOT_USERENTRYPOINT ["./entrypoint.sh"]
Minimal code of a custom Docker image with a cron.
entrypoint.sh
#!/bin/bashecho"Start cron"
cron
echo"cron started"# Run forevertail-f /dev/null
A simple entrypoint script that launchs cron.
restart.sh
#!/bin/bashecho"Restart the services..."# ...echo"Services restarted."
A custom script that will be executed at a certain frequency.