github.com/netbrain/docker@v1.9.0-rc2/docs/articles/using_supervisord.md (about)

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