github.com/dinever/docker@v1.11.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 +++ 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 19 launched, for example an Apache daemon or a SSH server daemon. Often 20 though you want to run more than one process in a container. There are a 21 number of ways you can achieve this ranging from using a simple Bash 22 script as the value of your container's `CMD` instruction to installing 23 a process management tool. 24 25 In this example we're going to make use of the process management tool, 26 [Supervisor](http://supervisord.org/), to manage multiple processes in 27 our container. Using Supervisor allows us to better control, manage, and 28 restart the processes we want to run. To demonstrate this we're going to 29 install 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 34 new image. 35 36 FROM ubuntu:13.04 37 MAINTAINER examples@docker.com 38 39 ## Installing Supervisor 40 41 We can now install our SSH and Apache daemons as well as Supervisor in 42 our container. 43 44 RUN apt-get update && apt-get install -y openssh-server apache2 supervisor 45 RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor 46 47 Here we're installing the `openssh-server`, 48 `apache2` and `supervisor` 49 (which provides the Supervisor daemon) packages. We're also creating four 50 new directories that are needed to run our SSH daemon and Supervisor. 51 52 ## Adding Supervisor's configuration file 53 54 Now let's add a configuration file for Supervisor. The default file is 55 called `supervisord.conf` and is located in 56 `/etc/supervisor/conf.d/`. 57 58 COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf 59 60 Let's see what is inside our `supervisord.conf` 61 file. 62 63 [supervisord] 64 nodaemon=true 65 66 [program:sshd] 67 command=/usr/sbin/sshd -D 68 69 [program:apache2] 70 command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND" 71 72 The `supervisord.conf` configuration file contains 73 directives that configure Supervisor and the processes it manages. The 74 first block `[supervisord]` provides configuration 75 for Supervisor itself. We're using one directive, `nodaemon` 76 which tells Supervisor to run interactively rather than 77 daemonize. 78 79 The next two blocks manage the services we wish to control. Each block 80 controls a separate process. The blocks contain a single directive, 81 `command`, which specifies what command to run to 82 start each process. 83 84 ## Exposing ports and running Supervisor 85 86 Now let's finish our `Dockerfile` by exposing some 87 required ports and specifying the `CMD` instruction 88 to start Supervisor when our container launches. 89 90 EXPOSE 22 80 91 CMD ["/usr/bin/supervisord"] 92 93 Here We've exposed ports 22 and 80 on the container and we're running 94 the `/usr/bin/supervisord` binary when the container 95 launches. 96 97 ## Building our image 98 99 We can now build our new image. 100 101 $ docker build -t <yourname>/supervisord . 102 103 ## Running our Supervisor container 104 105 Once We've got a built image we can launch a container from it. 106 107 $ docker run -p 22 -p 80 -t -i <yourname>/supervisord 108 2013-11-25 18:53:22,312 CRIT Supervisor running as root (no user in config file) 109 2013-11-25 18:53:22,312 WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing 110 2013-11-25 18:53:22,342 INFO supervisord started with pid 1 111 2013-11-25 18:53:23,346 INFO spawned: 'sshd' with pid 6 112 2013-11-25 18:53:23,349 INFO spawned: 'apache2' with pid 7 113 . . . 114 115 We've launched a new container interactively using the `docker run` command. 116 That container has run Supervisor and launched the SSH and Apache daemons with 117 it. We've specified the `-p` flag to expose ports 22 and 80. From here we can 118 now identify the exposed ports and connect to one or both of the SSH and Apache 119 daemons.