Docker Community Forums

Share and learn in the Docker community.

Run a job at container startup - it doesn't run


(Ben Zhou) #1

I need to run a script to start a job when the container starts. I call the script in /etc/rc.local, it doesn’t run, I then add the line “@reboot /home/userid/myscript.sh” in crontab, but it still doesn’t run.

What could I have missed? The same runs find in the base Ubuntu system.

thanks for sharing any insight.

Ben


(Dvohra) #2

On which OS does it not run?


(David Maze) #3

Typically containers run single processes, and those processes aren’t init(8) or crond(8).

Imagine you have a local command, say, /usr/local/bin/foo. You need to run a script to start a job when that command starts. How would you go about that? Those are the techniques you need to apply to do this.

This is a little easier in Docker space for two reasons. First, you control the default command, so you can make your wrapper script be /start-foo, and in your Dockerfile end with CMD /start-foo. The second is that Docker’s entrypoint feature gives a hook to do exactly this. I’ve seen at least a couple of examples floating around the forum.

Finally, this setup makes sense if you need to do some initialization (“before I run my application, I need to create some database tables”). If you need to actually start a second long-running process (“my application requires a database”) that’s best done by launching a second container. You can use Docker Compose to describe the multi-container setup.


(Ben Zhou) #4

Thanks much for the insight. I created an image for MQ from the Ubuntu 14.04 base, and I want to start the queue manager every time the container starts and shutdown the queue manager when it exit. with your point, it just hits me I could use entrypoint and CMD to trigger the job.