github.com/portworx/docker@v1.12.1/docs/admin/using_supervisord.md (about) 1 <!--[metadata]> 2 +++ 3 aliases = ["/engine/articles/using_supervisord/"] 4 title = "Using Supervisor with Docker" 5 description = "How to use Supervisor process management with Docker" 6 keywords = ["docker, supervisor, process management"] 7 [menu.main] 8 parent = "engine_admin" 9 weight="13" 10 +++ 11 <![end-metadata]--> 12 13 # Using Supervisor with Docker 14 15 > **Note**: 16 > - **If you don't like sudo** then see [*Giving non-root 17 > access*](../installation/binaries.md#giving-non-root-access) 18 19 Traditionally a Docker container runs a single process when it is launched, for 20 example an Apache daemon or a SSH server daemon. Often though you want to run 21 more than one process in a container. There are a number of ways you can 22 achieve this ranging from using a simple Bash script as the value of your 23 container's `CMD` instruction to installing a process management tool. 24 25 In this example you're going to make use of the process management tool, 26 [Supervisor](http://supervisord.org/), to manage multiple processes in a 27 container. Using Supervisor allows you to better control, manage, and restart 28 the processes inside the container. To demonstrate this we're going to install 29 and manage both an SSH daemon and an Apache daemon. 30 31 ## Creating a Dockerfile 32 33 Let's start by creating a basic `Dockerfile` for our new image. 34 35 ```Dockerfile 36 FROM ubuntu:16.04 37 MAINTAINER examples@docker.com 38 ``` 39 40 ## Installing Supervisor 41 42 You can now install the SSH and Apache daemons as well as Supervisor in the 43 container. 44 45 ```Dockerfile 46 RUN apt-get update && apt-get install -y openssh-server apache2 supervisor 47 RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor 48 ``` 49 50 The first `RUN` instruction installs the `openssh-server`, `apache2` and 51 `supervisor` (which provides the Supervisor daemon) packages. The next `RUN` 52 instruction creates four new directories that are needed to run the SSH daemon 53 and Supervisor. 54 55 ## Adding Supervisor's configuration file 56 57 Now let's add a configuration file for Supervisor. The default file is called 58 `supervisord.conf` and is located in `/etc/supervisor/conf.d/`. 59 60 ```Dockerfile 61 COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf 62 ``` 63 64 Let's see what is inside the `supervisord.conf` file. 65 66 ```ini 67 [supervisord] 68 nodaemon=true 69 70 [program:sshd] 71 command=/usr/sbin/sshd -D 72 73 [program:apache2] 74 command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND" 75 ``` 76 77 The `supervisord.conf` configuration file contains directives that configure 78 Supervisor and the processes it manages. The first block `[supervisord]` 79 provides configuration for Supervisor itself. The `nodaemon` directive is used, 80 which tells Supervisor to run interactively rather than daemonize. 81 82 The next two blocks manage the services we wish to control. Each block controls 83 a separate process. The blocks contain a single directive, `command`, which 84 specifies what command to run to start each process. 85 86 ## Exposing ports and running Supervisor 87 88 Now let's finish the `Dockerfile` by exposing some required ports and 89 specifying the `CMD` instruction to start Supervisor when our container 90 launches. 91 92 ```Dockerfile 93 EXPOSE 22 80 94 CMD ["/usr/bin/supervisord"] 95 ``` 96 97 These instructions tell Docker that ports 22 and 80 are exposed by the 98 container and that the `/usr/bin/supervisord` binary should be executed when 99 the container launches. 100 101 ## Building our image 102 103 Your completed Dockerfile now looks like this: 104 105 ```Dockerfile 106 FROM ubuntu:16.04 107 MAINTAINER examples@docker.com 108 109 RUN apt-get update && apt-get install -y openssh-server apache2 supervisor 110 RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor 111 112 COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf 113 114 EXPOSE 22 80 115 CMD ["/usr/bin/supervisord"] 116 ``` 117 118 And your `supervisord.conf` file looks like this; 119 120 ```ini 121 [supervisord] 122 nodaemon=true 123 124 [program:sshd] 125 command=/usr/sbin/sshd -D 126 127 [program:apache2] 128 command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND" 129 ``` 130 131 132 You can now build the image using this command; 133 134 ```bash 135 $ docker build -t mysupervisord . 136 ``` 137 138 ## Running your Supervisor container 139 140 Once you have built your image you can launch a container from it. 141 142 ```bash 143 $ docker run -p 22 -p 80 -t -i mysupervisord 144 2013-11-25 18:53:22,312 CRIT Supervisor running as root (no user in config file) 145 2013-11-25 18:53:22,312 WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing 146 2013-11-25 18:53:22,342 INFO supervisord started with pid 1 147 2013-11-25 18:53:23,346 INFO spawned: 'sshd' with pid 6 148 2013-11-25 18:53:23,349 INFO spawned: 'apache2' with pid 7 149 ... 150 ``` 151 152 You launched a new container interactively using the `docker run` command. 153 That container has run Supervisor and launched the SSH and Apache daemons with 154 it. We've specified the `-p` flag to expose ports 22 and 80. From here we can 155 now identify the exposed ports and connect to one or both of the SSH and Apache 156 daemons.