Docker Community Forums

Share and learn in the Docker community.

Automatically launch a service on container run

Hello guys, i’ve created a docker file containing apache2, nginx and postgresql, i want these services to be started automatically when i run the container; I tried to use sysv init by running the command update-rc.d service_name defaults then update-rc.d service_name enable but none of the service is running. Please i need help

First, there is a problem in your Dockerfile:

RUN service mysql restart && /tmp/setup.sh
Docker images do not save running processes. Therefore, your RUN command executes only during docker build phase and stops after the build is completed. Instead, you need to specify the command when the container is started using the CMD or ENTRYPOINT commands like below:

CMD mysql start
Secondly, the docker container needs a process (last command) to keep running, otherwise the container will exit/stop. Therefore, the normal service mysql start command cannot be used directly in the Dockerfile.

Solution
There are three typical ways to keep the process running:

Using service command and append non-end command after that like tail -F

CMD service mysql start && tail -F /var/log/mysql/error.log
This is often preferred when you have a single service running as it makes the outputted log accessible to docker.

Or use foreground command to do this

CMD /usr/bin/mysqld_safe
This works only if there is a script like mysqld_safe.

Or wrap your scripts into start.sh and put this in end

CMD /start.sh
This is best if the command must perform a series of steps, again, /start.sh should stay running.

lewish95, thanks for information.

A docker container is an isolated process on the host. It does not “boot”. It will start whatever you defined as ENTRYPOINT and/or CMD. See: https://docs.docker.com/engine/reference/builder/#entrypoint. Container Images following best practices, neither cover severall service (yours does), nor do they use a classic system deamon like initv or systemd.