Dockerizing Nginx and SSH using Supervisord
While working with Docker, I came across a use case wherein I was supposed to implement two processes in a single docker container. Docker has a limitation that only one CMD parameter can be provided in the Dockerfile as only one process can be run in the foreground. The use case included running Nginx and SSH on a single docker container that by far seem to be achievable only by passing shell script in CMD parameter of Dockerfile. After a rigorous search on the internet, I found a utility Supervisor by virtue of which we can run more than one process in a Docker container without having to worry about single CMD parameter or using shell scripts.
“The Supervisor is a process control and monitoring tool that can be used to control multiple processes on UNIX-like OS.”
To progress with this blog, above use case has been detailed below. The workaround actually included two files and both the files should be kept at the same location.
1) The Dockerfile that will run Supervisord in the foreground
2) supervisord.conf file that has to be passed in the Dockerfile in which we can tell multiple processes to be run inside Docker container.
Dockerfile:
FROM ubuntu:14.04 MAINTAINER sharad <sharad.aggarwal@tothenew.com> RUN apt-get update && apt-get install -y openssh-server apache2 supervisor nginx RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor RUN echo 'root:password' | chpasswd RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf EXPOSE 22 80 443 CMD ["/usr/bin/supervisord"]
supervisord.conf
[supervisord] nodaemon=true [program:sshd] command=/usr/sbin/sshd -D [program:nginx] command=/usr/sbin/nginx -g "daemon off;" priority=900 stdout_logfile= /dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 username=www-data autorestart=true
Now, run the below command to create an image to be created using above files. This command should be executed from the same location where above files are kept:
docker build -t sharry/supervisor-nginx-ssh .
This command will take sometime to fetch necessary packages from apt repositories and building an image. Below command will show the newly created image in the image list:
docker images
Now, launch a container from this image using below command:
docker run -itd --name nginx-ssh-test sharry/supervisor-nginx-ssh
And a new container is launched with Nginx and SSH successfully running into it. The output will be as shown in the screenshot below:
In the above screenshot, we perform SSH in the Docker container and checked the status of Nginx service. I hope this blog will be helpful.