2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
2016-01-23 23:36:40 -05:00
|
|
|
aliases = ["/engine/articles/host_integration/"]
|
2015-06-07 23:07:20 -04:00
|
|
|
title = "Automatically start containers"
|
|
|
|
description = "How to generate scripts for upstart, systemd, etc."
|
|
|
|
keywords = ["systemd, upstart, supervisor, docker, documentation, host integration"]
|
|
|
|
[menu.main]
|
2016-01-23 23:36:40 -05:00
|
|
|
parent = "engine_admin"
|
2016-07-19 00:40:07 -04:00
|
|
|
weight="5"
|
2015-06-07 23:07:20 -04:00
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
# Automatically start containers
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-10-28 11:06:04 -04:00
|
|
|
As of Docker 1.2,
|
2015-10-09 19:50:41 -04:00
|
|
|
[restart policies](../reference/run.md#restart-policies-restart) are the
|
2014-10-28 11:06:04 -04:00
|
|
|
built-in Docker mechanism for restarting containers when they exit. If set,
|
|
|
|
restart policies will be used when the Docker daemon starts up, as typically
|
|
|
|
happens after a system boot. Restart policies will ensure that linked containers
|
|
|
|
are started in the correct order.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-10-28 11:06:04 -04:00
|
|
|
If restart policies don't suit your needs (i.e., you have non-Docker processes
|
|
|
|
that depend on Docker containers), you can use a process manager like
|
|
|
|
[upstart](http://upstart.ubuntu.com/),
|
|
|
|
[systemd](http://freedesktop.org/wiki/Software/systemd/) or
|
|
|
|
[supervisor](http://supervisord.org/) instead.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-10-28 11:06:04 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
## Using a process manager
|
2014-10-28 11:06:04 -04:00
|
|
|
|
|
|
|
Docker does not set any restart policies by default, but be aware that they will
|
|
|
|
conflict with most process managers. So don't set restart policies if you are
|
|
|
|
using a process manager.
|
|
|
|
|
2014-04-15 20:53:12 -04:00
|
|
|
When you have finished setting up your image and are happy with your
|
|
|
|
running container, you can then attach a process manager to manage it.
|
2014-10-28 11:06:04 -04:00
|
|
|
When you run `docker start -a`, Docker will automatically attach to the
|
2014-05-14 13:22:49 -04:00
|
|
|
running container, or start it if needed and forward all signals so that
|
|
|
|
the process manager can detect when a container stops and correctly
|
|
|
|
restart it.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Here are a few sample scripts for systemd and upstart to integrate with
|
2014-10-28 11:06:04 -04:00
|
|
|
Docker.
|
|
|
|
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-10-28 11:06:04 -04:00
|
|
|
## Examples
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-10-28 11:06:04 -04:00
|
|
|
The examples below show configuration files for two popular process managers,
|
|
|
|
upstart and systemd. In these examples, we'll assume that we have already
|
|
|
|
created a container to run Redis with `--name=redis_server`. These files define
|
|
|
|
a new service that will be started after the docker daemon service has started.
|
|
|
|
|
|
|
|
|
|
|
|
### upstart
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
description "Redis container"
|
|
|
|
author "Me"
|
|
|
|
start on filesystem and started docker
|
|
|
|
stop on runlevel [!2345]
|
|
|
|
respawn
|
|
|
|
script
|
|
|
|
/usr/bin/docker start -a redis_server
|
|
|
|
end script
|
|
|
|
|
2014-10-28 11:06:04 -04:00
|
|
|
### systemd
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
[Unit]
|
|
|
|
Description=Redis container
|
2015-02-09 03:25:42 -05:00
|
|
|
Requires=docker.service
|
2014-04-15 20:53:12 -04:00
|
|
|
After=docker.service
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
Restart=always
|
|
|
|
ExecStart=/usr/bin/docker start -a redis_server
|
|
|
|
ExecStop=/usr/bin/docker stop -t 2 redis_server
|
|
|
|
|
|
|
|
[Install]
|
2016-06-21 10:45:29 -04:00
|
|
|
WantedBy=default.target
|
2015-02-09 03:25:42 -05:00
|
|
|
|
2016-06-22 04:00:56 -04:00
|
|
|
If you intend to use this as a system service, put the above contents in a file
|
|
|
|
in the `/etc/systemd/system` directory, e.g.
|
|
|
|
`/etc/systemd/system/docker-redis_server.service`.
|
|
|
|
|
2015-02-11 22:02:54 -05:00
|
|
|
If you need to pass options to the redis container (such as `--env`),
|
|
|
|
then you'll need to use `docker run` rather than `docker start`. This will
|
|
|
|
create a new container every time the service is started, which will be stopped
|
|
|
|
and removed when the service is stopped.
|
2015-02-09 03:25:42 -05:00
|
|
|
|
|
|
|
[Service]
|
|
|
|
...
|
2015-02-11 22:02:54 -05:00
|
|
|
ExecStart=/usr/bin/docker run --env foo=bar --name redis_server redis
|
2016-07-01 09:19:28 -04:00
|
|
|
ExecStop=/usr/bin/docker stop -t 2 redis_server
|
|
|
|
ExecStopPost=/usr/bin/docker rm -f redis_server
|
2015-02-09 03:25:42 -05:00
|
|
|
...
|
2016-06-22 04:00:56 -04:00
|
|
|
|
|
|
|
To start using the service, reload systemd and start the service:
|
|
|
|
|
|
|
|
systemctl daemon-reload
|
|
|
|
systemctl start docker-redis_server.service
|
|
|
|
|
|
|
|
To enable the service at system startup, execute:
|
|
|
|
|
|
|
|
systemctl enable docker-redis_server.service
|