Docker Supervisor tutorial

admin/ March 15, 2017/ Docker, Linux/ 0 comments

Why Supervisor with Docker ?

Generally a Docker container starts with a single process when it is launched, for example an Apache daemon or a SSH server daemon. Often though you want to run more than one process in a container. There are a number of ways you can achieve this ranging from using a simple Bash script as the value of your container’s CMD instruction to installing a process management tool.

GITHUB URL -: https://github.com/arvindr226/supervisord.git

$ git clone https://github.com/arvindr226/supervisord.git 

Lets start with supervisor to run multiple process in the single container.

Create a Dockerfile
Choose Ubuntu LTS 16.04 default image.

From ubuntu16.04
Maintainer Arvind Rawat <[email protected]>

Install the apache2, openssh-server and supervisor.

RUN sudo apt get update && apt install -y apache2 openssh-server supervisor
RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor

Configure the ssh for root user in Dockerfile

RUN echo 'root:gotechnies' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed '[email protected]\s*required\s*[email protected] optional [email protected]' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

Add a supervisor configuration file

COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

The above configuration will add the supervisord.conf file inthe container while build the docker image.

Add the below content in the supervisord.conf

[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D

[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"

Exposing ports with running Supervisor

Let’s enable the required default ports to access the http (port 80) and ssh (port 22) in the Dockerfile and write a line to start supervisor execute using the CMD in Dockerfile.

EXPOSE 22 80
CMD ["/usr/bin/supervisord"]

Your Final complete Dockerfile will look like below.

From ubuntu:16.04
Maintainer Arvind Rawat <[email protected]>

RUN apt-get update && apt-get install -y openssh-server apache2 supervisor
RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor

RUN echo 'root:gotechnies' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
RUN sed '[email protected]\s*required\s*[email protected] optional [email protected]' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
#Copy supervisord.conf 
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 22 80
CMD ["/usr/bin/supervisord"]

Now build your docker image with this Dockerfile

$ docker build -t gotechnies .

Start your docker container like below

$ docker run -d -p 80:80 -p 2222:22 gotechnies

Try to connect with ssh server

$ ssh [email protected] -p2222 

Use the password you have added in the Dockerfile.

gotechnies

Check the apache on web browser http://localhost

Leave a Reply