Docker Supervisor


Traditionally a Docker container runs 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.

Supervisor, to manage multiple processes in a container.

Using Supervisor allows you to better control, manage, and restart the processes inside the container.

Lets say you want to create SSH process running on the docker container.

Lets build the image file.

1) Will use ubuntu image.

2) Install SSH and Supervisord.

3) Create folder for SSH and supervisor.

4) Create supervisord.conf and update with SSH startup command.

5) Copy the supervisord.conf to the image.

6) Expose port # 22.

7) Start the supervisord.

root@masterk8s:/docker# cat supervisord.conf

[supervisord]

nodaemon=true

[program:sshd]

command=/usr/sbin/sshd -D

root@masterk8s:/docker#

Now, Lets start creating the docker image file.

root@masterk8s:/docker# cat dockerfile

FROM ubuntu:latest

RUN apt-get update

RUN apt-get install openssh-server -y

RUN mkdir /var/run/sshd

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

root@masterk8s:/docker#

root@masterk8s:/docker# docker build -t rajasekar:supervisord .

Due to DNS issue inside the docker. I am spawing the docker container with network - host or we can build using host network.

root@masterk8s:/docker# docker build --network=host -t rajasekar:supervisord .

root@masterk8s:/# docker images  | grep -i supervisord

rajasekar                          supervisord   c0c3d44967d0   35 seconds ago   227MB

root@masterk8s:/#

root@masterk8s:/# docker history c0c3d44967d0

IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT

c0c3d44967d0   50 seconds ago   /bin/sh -c #(nop)  CMD ["/usr/bin/supervisor…   0B

52f96147e7ac   50 seconds ago   /bin/sh -c #(nop)  EXPOSE 22                    0B

2dad551e9d21   51 seconds ago   /bin/sh -c #(nop) COPY file:7381c7204f81628f…   70B

5b45d5ba8ddf   51 seconds ago   /bin/sh -c mkdir /var/run/sshd                  0B

27ab7d5be6ad   53 seconds ago   /bin/sh -c apt-get install openssh-server -y    110MB

ce70a0d553e1   5 minutes ago    /bin/sh -c apt-get update                       39.6MB

a8780b506fa4   13 days ago      /bin/sh -c #(nop)  CMD ["bash"]                 0B

<missing>      13 days ago      /bin/sh -c #(nop) ADD file:29c72d5be8c977aca…   77.8MB

root@masterk8s:/#

Image is completed. Lets create a container.

root@masterk8s:/docker# docker run -it -p 8080:22 c0c3d44967d0 bash

root@5a004a6876ce:/#

root@masterk8s:~# docker container ls

CONTAINER ID   IMAGE          COMMAND   CREATED          STATUS          PORTS                                   NAMES

5a004a6876ce   c0c3d44967d0   "bash"    30 seconds ago   Up 24 seconds   0.0.0.0:8080->22/tcp, :::8080->22/tcp   nifty_chaplygin

root@masterk8s:~#


Comments

Popular posts from this blog

SRE/DevOps Syllabus

AWS Code Commit - CI/CD Series Part 1

Docker - Preventing IP overlapping