For now we installed and played around docker images and containers. Normally docker container runs a single process when it is launched, for example an Apache daemon or a SSH server daemon. Because 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 Apache and MySQL on a single docker container that by far seem to be achievable only by passing shell script in CMD parameter of Dockerfile.
But in real environment we cannot run separate daemon always for example apache, MySQL etc. 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.
Now will the process management tool called Supervisor, to manage multiple processes in a container. Using Supervisor allows you to better control, manage, and restart the processes inside the container. To demonstrate this we’re going to install and manage both an MySQL daemon and an Apache daemon.
“The Supervisor is a process control and monitoring tool that can be used to control multiple processes on UNIX-like OS.”
Building our image
Here is the sample minimal Dockerfile, to build the image with multiple services like httpd, and mysql.
FROM centos:6
RUN yum install -y epel-release && \
yum update -y && \
yum install -y httpd \
mysql-server \
openssh-server \
supervisor && \
yum clean all
RUN /sbin/service mysqld start && \
mysqladmin -u root password ”
RUN /sbin/service sshd start
RUN echo “[supervisord]” > /etc/supervisord.conf && \
echo “nodaemon=true” >> /etc/supervisord.conf && \
echo “” >> /etc/supervisord.conf && \
echo “[program:mysqld]” >> /etc/supervisord.conf && \
echo “command=/usr/bin/mysqld_safe” >> /etc/supervisord.conf && \
echo “” >> /etc/supervisord.conf && \
echo “[program:httpd]” >> /etc/supervisord.conf && \
echo “command=/usr/sbin/apachectl -D FOREGROUND” >> /etc/supervisord.conf
CMD [“/usr/bin/supervisord”]
Create Docker-compose file called Docker-compose.yml
# vim docker-compose.yml
web:
build: .
command: /usr/bin/supervisord
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-compose build
Running your Supervisor container
This command will take some time to fetch necessary packages from 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-compose up
Creating docker_web_1
Attaching to docker_web_1
web_1 | 2016-08-20 15:44:04,143 CRIT Supervisor running as root (no user in config file)
web_1 | 2016-08-20 15:44:04,156 INFO supervisord started with pid 1
web_1 | 2016-08-20 15:44:04,157 INFO spawned: ‘httpd’ with pid 5
web_1 | 2016-08-20 15:44:04,163 INFO spawned: ‘mysqld’ with pid 6
web_1 | 2016-08-20 15:44:05,231 INFO success: httpd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
web_1 | 2016-08-20 15:44:05,231 INFO success: mysqld entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
(press ctrl+z to stop the commend)
To check the running images use below comment
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c4a7d9260c2 docker_web “/usr/bin/supervisord” About a minute ago Up About a minute docker_web_1
Let connect to the running container to confirm that the daemons are well up and running
# docker exec -i -t 3c4a7d9260c2 bash (it will login to the container, you can check the running services using basic Linux comment)
# ps aux | grep mysql
root 6 0.0 0.1 10836 1280 ? S 15:44 0:00 /bin/sh /usr/bin/mysqld_safe
mysql 72 0.0 3.2 188396 33500 ? Sl 15:44 0:00 /usr/libexec/mysqld –basedir=/usr –datadir=/var/lib/mysql –user=mysql –pid-file=/var/run/mysqld/mysqld.pid –skip-external-locking –log-error=/var/log/mysqld.log –socket=/var/lib/mysql/mysql.sock
root 100 0.0 0.0 61232 552 ? R+ 15:45 0:00 grep mysql
# ps aux | grep httpd
root 10 0.0 0.4 174408 4684 ? S 15:44 0:00 /usr/sbin/httpd -D FOREGROUND
apache 78 0.0 0.2 174540 2180 ? S 15:44 0:00 /usr/sbin/httpd -D FOREGROUND
apache 79 0.0 0.2 174540 2180 ? S 15:44 0:00 /usr/sbin/httpd -D FOREGROUND
apache 80 0.0 0.2 174540 2180 ? S 15:44 0:00 /usr/sbin/httpd -D FOREGROUND
apache 81 0.0 0.2 174540 2180 ? S 15:44 0:00 /usr/sbin/httpd -D FOREGROUND
apache 82 0.0 0.2 174540 2180 ? S 15:44 0:00 /usr/sbin/httpd -D FOREGROUND
apache 83 0.0 0.2 174540 2180 ? S 15:44 0:00 /usr/sbin/httpd -D FOREGROUND
apache 84 0.0 0.2 174540 2180 ? S 15:44 0:00 /usr/sbin/httpd -D FOREGROUND
apache 85 0.0 0.2 174540 2180 ? S 15:44 0:00 /usr/sbin/httpd -D FOREGROUND
root 109 0.0 0.0 61232 772 ? S+ 15:57 0:00 grep httpd