Docker is used for Application virtualization. So ideally a single docker container should be used to run a single process. Running multiple services on a single container is not the recommended approach but still there can be forced scenarios where we need to start more than one process in a single container.
Now in order to do this we need to build a dockerfile which would start multiple processes. CMD is used in dockerfile to start a process but due to docker limitation, only a single CMD command can be executed per dockerfile. So lets go through a workaround which can be used to start multiple services.
In this example, we are going to start a node process, ssh service as well as tomcat process using a single docker image.
Note: Iterating again, this is not the way docker containers are supposed to be used. This is an alternate approach.
Here we will be using supervisord to run multiple processes from a dockerfile. Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
We will start supervisor service from dockerfile and start other services using supervisor.
FROM node:0.10.38 #using node base image ADD . /app RUN apt-get update RUN apt-get install -y tomcat7 #installing tomcat7 WORKDIR /app RUN npm install #installing npm RUN npm install --save body-parser RUN npm install request RUN apt-get install -y vim RUN apt-get install -y openssh-server #installing openssh service # Installing supervisor service and creating directories for copy supervisor configurations RUN apt-get -y install supervisor && mkdir -p /var/log/supervisor && mkdir -p /etc/supervisor/conf.d ADD supervisor.conf /etc/supervisor.conf #copying config file from local to docker image EXPOSE 3000 CMD ["supervisord", "-c", "/etc/supervisor.conf"] # starting supervisord service
Note: Details about steps are added as comments in the file.
[supervisord] nodaemon=true [program:http] command=service tomcat7 start autostart = true autorestart = false stopasgroup=true [program:ssh] command=service ssh start autostart = true autorestart = false stopasgroup=true [program:uptime] directory = /app/ command = node app.js -p 3000 autostart = true autorestart = false
The supervisor config file contains 3 program section each for starting a service. Rest of the file is very basic and self explanatory. For advance details on supervisor config file refer the below link-
Once the dockerfile is built and container is created from the image, it starts supervisor service which in turn starts the node, tomcat and ssh services as sub processes.
Thanks for checking out.
Let us know in comments if you run into any issues or have feedback.
Please like and share if this article helps.