2014-04-16 00:53:12 +00:00
|
|
|
page_title: Using Supervisor with Docker
|
|
|
|
page_description: How to use Supervisor process management with Docker
|
|
|
|
page_keywords: docker, supervisor, process management
|
|
|
|
|
|
|
|
# Using Supervisor with Docker
|
|
|
|
|
2014-04-18 20:21:55 +00:00
|
|
|
> **Note**:
|
2014-04-23 20:48:28 +00:00
|
|
|
> - **If you don't like sudo** then see [*Giving non-root
|
2014-12-16 04:25:37 +00:00
|
|
|
> access*](/installation/binaries/#giving-non-root-access)
|
2014-04-16 00:53:12 +00:00
|
|
|
|
|
|
|
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
|
2014-05-21 21:05:19 +00:00
|
|
|
script as the value of your container's `CMD` instruction to installing
|
|
|
|
a process management tool.
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
In this example we're going to make use of the process management tool,
|
2014-04-16 00:53:12 +00:00
|
|
|
[Supervisor](http://supervisord.org/), to manage multiple processes in
|
|
|
|
our container. Using Supervisor allows us to better control, manage, and
|
2014-04-23 20:48:28 +00:00
|
|
|
restart the processes we want to run. To demonstrate this we're going to
|
2014-04-16 00:53:12 +00:00
|
|
|
install and manage both an SSH daemon and an Apache daemon.
|
|
|
|
|
|
|
|
## Creating a Dockerfile
|
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
Let's start by creating a basic `Dockerfile` for our
|
2014-04-16 00:53:12 +00:00
|
|
|
new image.
|
|
|
|
|
2014-05-23 19:15:44 +00:00
|
|
|
FROM ubuntu:13.04
|
2014-07-02 00:30:25 +00:00
|
|
|
MAINTAINER examples@docker.com
|
2014-04-16 00:53:12 +00:00
|
|
|
|
|
|
|
## Installing Supervisor
|
|
|
|
|
|
|
|
We can now install our SSH and Apache daemons as well as Supervisor in
|
|
|
|
our container.
|
|
|
|
|
2014-07-07 18:06:34 +00:00
|
|
|
RUN apt-get update && apt-get install -y openssh-server apache2 supervisor
|
|
|
|
RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
Here we're installing the `openssh-server`,
|
2014-04-16 00:53:12 +00:00
|
|
|
`apache2` and `supervisor`
|
2014-04-23 20:48:28 +00:00
|
|
|
(which provides the Supervisor daemon) packages. We're also creating two
|
2014-04-16 00:53:12 +00:00
|
|
|
new directories that are needed to run our SSH daemon and Supervisor.
|
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
## Adding Supervisor's configuration file
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
Now let's add a configuration file for Supervisor. The default file is
|
2014-04-16 00:53:12 +00:00
|
|
|
called `supervisord.conf` and is located in
|
|
|
|
`/etc/supervisor/conf.d/`.
|
|
|
|
|
2014-07-09 21:13:26 +00:00
|
|
|
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
Let's see what is inside our `supervisord.conf`
|
2014-04-16 00:53:12 +00:00
|
|
|
file.
|
|
|
|
|
|
|
|
[supervisord]
|
|
|
|
nodaemon=true
|
|
|
|
|
|
|
|
[program:sshd]
|
|
|
|
command=/usr/sbin/sshd -D
|
|
|
|
|
|
|
|
[program:apache2]
|
|
|
|
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
|
|
|
|
|
|
|
|
The `supervisord.conf` configuration file contains
|
|
|
|
directives that configure Supervisor and the processes it manages. The
|
|
|
|
first block `[supervisord]` provides configuration
|
2014-04-23 20:48:28 +00:00
|
|
|
for Supervisor itself. We're using one directive, `nodaemon`
|
2014-04-16 00:53:12 +00:00
|
|
|
which tells Supervisor to run interactively rather than
|
|
|
|
daemonize.
|
|
|
|
|
|
|
|
The next two blocks manage the services we wish to control. Each block
|
|
|
|
controls a separate process. The blocks contain a single directive,
|
|
|
|
`command`, which specifies what command to run to
|
|
|
|
start each process.
|
|
|
|
|
|
|
|
## Exposing ports and running Supervisor
|
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
Now let's finish our `Dockerfile` by exposing some
|
2014-04-16 00:53:12 +00:00
|
|
|
required ports and specifying the `CMD` instruction
|
|
|
|
to start Supervisor when our container launches.
|
|
|
|
|
|
|
|
EXPOSE 22 80
|
|
|
|
CMD ["/usr/bin/supervisord"]
|
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
Here We've exposed ports 22 and 80 on the container and we're running
|
2014-04-16 00:53:12 +00:00
|
|
|
the `/usr/bin/supervisord` binary when the container
|
|
|
|
launches.
|
|
|
|
|
2014-05-08 13:11:17 +00:00
|
|
|
## Building our image
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-08 13:11:17 +00:00
|
|
|
We can now build our new image.
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-01 14:13:34 +00:00
|
|
|
$ sudo docker build -t <yourname>/supervisord .
|
2014-04-16 00:53:12 +00:00
|
|
|
|
|
|
|
## Running our Supervisor container
|
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
Once We've got a built image we can launch a container from it.
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-01 14:13:34 +00:00
|
|
|
$ sudo docker run -p 22 -p 80 -t -i <yourname>/supervisord
|
2014-04-16 00:53:12 +00:00
|
|
|
2013-11-25 18:53:22,312 CRIT Supervisor running as root (no user in config file)
|
|
|
|
2013-11-25 18:53:22,312 WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing
|
|
|
|
2013-11-25 18:53:22,342 INFO supervisord started with pid 1
|
|
|
|
2013-11-25 18:53:23,346 INFO spawned: 'sshd' with pid 6
|
|
|
|
2013-11-25 18:53:23,349 INFO spawned: 'apache2' with pid 7
|
|
|
|
. . .
|
|
|
|
|
2014-04-23 20:48:28 +00:00
|
|
|
We've launched a new container interactively using the `docker run` command.
|
|
|
|
That container has run Supervisor and launched the SSH and Apache daemons with
|
|
|
|
it. We've specified the `-p` flag to expose ports 22 and 80. From here we can
|
|
|
|
now identify the exposed ports and connect to one or both of the SSH and Apache
|
|
|
|
daemons.
|