2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
2016-01-23 23:36:40 -05:00
|
|
|
aliases = ["/engine/articles/using_supervisord/"]
|
2015-06-07 23:07:20 -04:00
|
|
|
title = "Using Supervisor with Docker"
|
|
|
|
description = "How to use Supervisor process management with Docker"
|
|
|
|
keywords = ["docker, supervisor, process management"]
|
|
|
|
[menu.main]
|
2016-01-23 23:36:40 -05:00
|
|
|
parent = "engine_admin"
|
2015-06-07 23:07:20 -04:00
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
# Using Supervisor with Docker
|
|
|
|
|
2015-10-09 19:50:41 -04:00
|
|
|
> **Note**:
|
|
|
|
> - **If you don't like sudo** then see [*Giving non-root
|
|
|
|
> access*](../installation/binaries.md#giving-non-root-access)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04: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 script as the value of your
|
|
|
|
container's `CMD` instruction to installing a process management tool.
|
|
|
|
|
|
|
|
In this example you're going to make use of the process management tool,
|
|
|
|
[Supervisor](http://supervisord.org/), to manage multiple processes in a
|
|
|
|
container. Using Supervisor allows you to better control, manage, and restart
|
|
|
|
the processes inside the container. To demonstrate this we're going to install
|
|
|
|
and manage both an SSH daemon and an Apache daemon.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
## Creating a Dockerfile
|
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
Let's start by creating a basic `Dockerfile` for our new image.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
```Dockerfile
|
|
|
|
FROM ubuntu:16.04
|
|
|
|
MAINTAINER examples@docker.com
|
|
|
|
```
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
## Installing Supervisor
|
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
You can now install the SSH and Apache daemons as well as Supervisor in the
|
|
|
|
container.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
```Dockerfile
|
|
|
|
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-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
The first `RUN` instruction installs the `openssh-server`, `apache2` and
|
|
|
|
`supervisor` (which provides the Supervisor daemon) packages. The next `RUN`
|
|
|
|
instruction creates four new directories that are needed to run the SSH daemon
|
|
|
|
and Supervisor.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
## Adding Supervisor's configuration file
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
Now let's add a configuration file for Supervisor. The default file is called
|
|
|
|
`supervisord.conf` and is located in `/etc/supervisor/conf.d/`.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
```Dockerfile
|
|
|
|
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
|
|
|
|
```
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
Let's see what is inside the `supervisord.conf` file.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
```ini
|
|
|
|
[supervisord]
|
|
|
|
nodaemon=true
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
[program:sshd]
|
|
|
|
command=/usr/sbin/sshd -D
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
[program:apache2]
|
|
|
|
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
|
|
|
|
```
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
The `supervisord.conf` configuration file contains directives that configure
|
|
|
|
Supervisor and the processes it manages. The first block `[supervisord]`
|
|
|
|
provides configuration for Supervisor itself. The `nodaemon` directive is used,
|
|
|
|
which tells Supervisor to run interactively rather than daemonize.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
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.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
## Exposing ports and running Supervisor
|
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
Now let's finish the `Dockerfile` by exposing some required ports and
|
|
|
|
specifying the `CMD` instruction to start Supervisor when our container
|
|
|
|
launches.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
```Dockerfile
|
|
|
|
EXPOSE 22 80
|
|
|
|
CMD ["/usr/bin/supervisord"]
|
|
|
|
```
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
These instructions tell Docker that ports 22 and 80 are exposed by the
|
|
|
|
container and that the `/usr/bin/supervisord` binary should be executed when
|
|
|
|
the container launches.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-08 09:11:17 -04:00
|
|
|
## Building our image
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
Your completed Dockerfile now looks like this:
|
|
|
|
|
|
|
|
```Dockerfile
|
|
|
|
FROM ubuntu:16.04
|
|
|
|
MAINTAINER examples@docker.com
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
|
|
|
|
|
|
|
|
EXPOSE 22 80
|
|
|
|
CMD ["/usr/bin/supervisord"]
|
|
|
|
```
|
|
|
|
|
|
|
|
And your `supervisord.conf` file looks like this;
|
|
|
|
|
|
|
|
```ini
|
|
|
|
[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"
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
You can now build the image using this command;
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
```bash
|
|
|
|
$ docker build -t mysupervisord .
|
|
|
|
```
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
## Running your Supervisor container
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
Once you have built your image you can launch a container from it.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
```bash
|
|
|
|
$ docker run -p 22 -p 80 -t -i mysupervisord
|
|
|
|
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-15 20:53:12 -04:00
|
|
|
|
2016-05-05 19:52:51 -04:00
|
|
|
You launched a new container interactively using the `docker run` command.
|
2014-04-23 16:48:28 -04:00
|
|
|
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.
|