github.com/netbrain/docker@v1.9.0-rc2/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/run.md#restart-policies-restart) 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 When you have finished setting up your image and are happy with your 35 running container, you can then attach a process manager to manage it. 36 When you run `docker start -a`, Docker will automatically attach to the 37 running container, or start it if needed and forward all signals so that 38 the process manager can detect when a container stops and correctly 39 restart it. 40 41 Here are a few sample scripts for systemd and upstart to integrate with 42 Docker. 43 44 45 ## Examples 46 47 The examples below show configuration files for two popular process managers, 48 upstart and systemd. In these examples, we'll assume that we have already 49 created a container to run Redis with `--name=redis_server`. These files define 50 a new service that will be started after the docker daemon service has started. 51 52 53 ### upstart 54 55 description "Redis container" 56 author "Me" 57 start on filesystem and started docker 58 stop on runlevel [!2345] 59 respawn 60 script 61 /usr/bin/docker start -a redis_server 62 end script 63 64 ### systemd 65 66 [Unit] 67 Description=Redis container 68 Requires=docker.service 69 After=docker.service 70 71 [Service] 72 Restart=always 73 ExecStart=/usr/bin/docker start -a redis_server 74 ExecStop=/usr/bin/docker stop -t 2 redis_server 75 76 [Install] 77 WantedBy=local.target 78 79 If you need to pass options to the redis container (such as `--env`), 80 then you'll need to use `docker run` rather than `docker start`. This will 81 create a new container every time the service is started, which will be stopped 82 and removed when the service is stopped. 83 84 [Service] 85 ... 86 ExecStart=/usr/bin/docker run --env foo=bar --name redis_server redis 87 ExecStop=/usr/bin/docker stop -t 2 redis_server ; /usr/bin/docker rm -f redis_server 88 ...