I need to run some programs on startup. On a “normal” (not docker) system i would just create a crontab like
@reboot sudo dothisorthat
Alright, so i thought, i could do the same with docker. So i put this line of code into my dockerfile:
RUN (crontab -l; echo "@reboot sudo dothisorthat >> /var/log/cron.log") | crontab
I found this syntax in a high voted stackoverflow example. Looks a little bit weird, but okay. So i build my container with
docker build . and i get this error message:
Step 9/10 : RUN (crontab -l; echo “@reboot sudo dothisorthat >> /var/log/cron.log”) | crontab
—> Running in 93f46961d7d2
no crontab for root
So what is a proper way to run a program/script on reboot inside a docker container with or without cron?
So, when would this be necessary?
You have the entrypoint that executes a program at start, so fx. you want something to run a shell script before, lets say apache.
Then i would write a wrapper for apache, and make docker execute that wrapper instead of apache:
echo test > /tmp/test
In the end I want to start two programs after 2 seconds at system start (or in this case container start). I realized this in a “normal” system via a crontab, but I am open to suggestions, eg using Entrypoint. The crontab looks like this to be exact:
@reboot sleep 2 && sudo startprogram1 && startprogram2
To be honest i don’t understand the entrypoint syntax (even with the docker documentation) in this specific example. Could you tell me how the crontab would look like as entrypoint?
I would rather do it with a process manager like systemd even “on a normal system”. If you do it in an entrypoint, it means you will delay your main process by 2 seconds, because it needs to wait until you start your additional processes. Or you need to write a perocess manager in bash which I don’t recommend. It would be something like this, but even more complicated since I used only one process. tutorial-linux-signals/start-trap.sh at main · itsziget/tutorial-linux-signals · GitHub
Just go to the root of that project and you can read about different solutions including supervisor.
If you want to know more about entrypoints and related directives, you can find my example project here to show how ENTRYPOINT, SHELL and CMD work. GitHub - itsziget/tutorial-docker-command: Scripts to demonstrate the ways to build the main process inside a Docker container
The video is Hungarian, but I already created an English channel, so eventually I will translate the video too. Until that, you can just mute the video and see the screen to see the result without actually running the scripts on your machine if you don’t want to.