github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/docs/articles/host_integration.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "Automatically start containers"
     4  description = "How to generate scripts for upstart, systemd, etc."
     5  keywords = ["systemd, upstart, supervisor, docker, documentation,  host integration"]
     6  [menu.main]
     7  parent = "smn_administrate"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # Automatically start containers
    12  
    13  As of Docker 1.2,
    14  [restart policies](../reference/run.md#restart-policies-restart) are the
    15  built-in Docker mechanism for restarting containers when they exit. If set,
    16  restart policies will be used when the Docker daemon starts up, as typically
    17  happens after a system boot. Restart policies will ensure that linked containers
    18  are started in the correct order.
    19  
    20  If restart policies don't suit your needs (i.e., you have non-Docker processes
    21  that depend on Docker containers), you can use a process manager like
    22  [upstart](http://upstart.ubuntu.com/),
    23  [systemd](http://freedesktop.org/wiki/Software/systemd/) or
    24  [supervisor](http://supervisord.org/) instead.
    25  
    26  
    27  ## Using a process manager
    28  
    29  Docker does not set any restart policies by default, but be aware that they will
    30  conflict with most process managers. So don't set restart policies if you are
    31  using a process manager.
    32  
    33  When you have finished setting up your image and are happy with your
    34  running container, you can then attach a process manager to manage it.
    35  When you run `docker start -a`, Docker will automatically attach to the
    36  running container, or start it if needed and forward all signals so that
    37  the process manager can detect when a container stops and correctly
    38  restart it.
    39  
    40  Here are a few sample scripts for systemd and upstart to integrate with
    41  Docker.
    42  
    43  
    44  ## Examples
    45  
    46  The examples below show configuration files for two popular process managers,
    47  upstart and systemd. In these examples, we'll assume that we have already
    48  created a container to run Redis with `--name=redis_server`. These files define
    49  a new service that will be started after the docker daemon service has started.
    50  
    51  
    52  ### upstart
    53  
    54      description "Redis container"
    55      author "Me"
    56      start on filesystem and started docker
    57      stop on runlevel [!2345]
    58      respawn
    59      script
    60        /usr/bin/docker start -a redis_server
    61      end script
    62  
    63  ### systemd
    64  
    65      [Unit]
    66      Description=Redis container
    67      Requires=docker.service
    68      After=docker.service
    69  
    70      [Service]
    71      Restart=always
    72      ExecStart=/usr/bin/docker start -a redis_server
    73      ExecStop=/usr/bin/docker stop -t 2 redis_server
    74  
    75      [Install]
    76      WantedBy=local.target
    77  
    78  If you need to pass options to the redis container (such as `--env`),
    79  then you'll need to use `docker run` rather than `docker start`. This will
    80  create a new container every time the service is started, which will be stopped
    81  and removed when the service is stopped.
    82  
    83      [Service]
    84      ...
    85      ExecStart=/usr/bin/docker run --env foo=bar --name redis_server redis
    86      ExecStop=/usr/bin/docker stop -t 2 redis_server ; /usr/bin/docker rm -f redis_server
    87      ...