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