Image for post
Image for post

How to run multiple services in Docker container ! [using Supervisor]

Abhimanyu Garg
Dec 9, 2018 · 2 min read

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.

Solution

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-

http://supervisord.org/configuration.html

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.

Visit https://www.techmanyu.com/ for more articles.

You May Also Like-

Originally published at www.techmanyu.com on December 9, 2018.

TechManyu

TechManyu is a great platform to share your technical…

Abhimanyu Garg

Written by

Cloud and DevOps Professional with keen interest in System Design & Architecture. SRE | MultiCloud | Kubernetes (CKA) | Microservices | Automation | Ops

TechManyu

TechManyu

TechManyu is a great platform to share your technical knowledge and expertise to help millions of other developers and technical guys out there. Building an interactive community is our ultimate goal through which everyone can share and benefit at the same time. www.techmanyu.com

Abhimanyu Garg

Written by

Cloud and DevOps Professional with keen interest in System Design & Architecture. SRE | MultiCloud | Kubernetes (CKA) | Microservices | Automation | Ops

TechManyu

TechManyu

TechManyu is a great platform to share your technical knowledge and expertise to help millions of other developers and technical guys out there. Building an interactive community is our ultimate goal through which everyone can share and benefit at the same time. www.techmanyu.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store