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