github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/docs/sources/articles/using_supervisord.md (about)

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