github.com/portworx/docker@v1.12.1/docs/admin/host_integration.md (about) 1 <!--[metadata]> 2 +++ 3 aliases = ["/engine/articles/host_integration/"] 4 title = "Automatically start containers" 5 description = "How to generate scripts for upstart, systemd, etc." 6 keywords = ["systemd, upstart, supervisor, docker, documentation, host integration"] 7 [menu.main] 8 parent = "engine_admin" 9 weight="5" 10 +++ 11 <![end-metadata]--> 12 13 # Automatically start containers 14 15 As of Docker 1.2, 16 [restart policies](../reference/run.md#restart-policies-restart) are the 17 built-in Docker mechanism for restarting containers when they exit. If set, 18 restart policies will be used when the Docker daemon starts up, as typically 19 happens after a system boot. Restart policies will ensure that linked containers 20 are started in the correct order. 21 22 If restart policies don't suit your needs (i.e., you have non-Docker processes 23 that depend on Docker containers), you can use a process manager like 24 [upstart](http://upstart.ubuntu.com/), 25 [systemd](http://freedesktop.org/wiki/Software/systemd/) or 26 [supervisor](http://supervisord.org/) instead. 27 28 29 ## Using a process manager 30 31 Docker does not set any restart policies by default, but be aware that they will 32 conflict with most process managers. So don't set restart policies if you are 33 using a process manager. 34 35 When you have finished setting up your image and are happy with your 36 running container, you can then attach a process manager to manage it. 37 When you run `docker start -a`, Docker will automatically attach to the 38 running container, or start it if needed and forward all signals so that 39 the process manager can detect when a container stops and correctly 40 restart it. 41 42 Here are a few sample scripts for systemd and upstart to integrate with 43 Docker. 44 45 46 ## Examples 47 48 The examples below show configuration files for two popular process managers, 49 upstart and systemd. In these examples, we'll assume that we have already 50 created a container to run Redis with `--name=redis_server`. These files define 51 a new service that will be started after the docker daemon service has started. 52 53 54 ### upstart 55 56 description "Redis container" 57 author "Me" 58 start on filesystem and started docker 59 stop on runlevel [!2345] 60 respawn 61 script 62 /usr/bin/docker start -a redis_server 63 end script 64 65 ### systemd 66 67 [Unit] 68 Description=Redis container 69 Requires=docker.service 70 After=docker.service 71 72 [Service] 73 Restart=always 74 ExecStart=/usr/bin/docker start -a redis_server 75 ExecStop=/usr/bin/docker stop -t 2 redis_server 76 77 [Install] 78 WantedBy=default.target 79 80 If you intend to use this as a system service, put the above contents in a file 81 in the `/etc/systemd/system` directory, e.g. 82 `/etc/systemd/system/docker-redis_server.service`. 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 93 ExecStopPost=/usr/bin/docker rm -f redis_server 94 ... 95 96 To start using the service, reload systemd and start the service: 97 98 systemctl daemon-reload 99 systemctl start docker-redis_server.service 100 101 To enable the service at system startup, execute: 102 103 systemctl enable docker-redis_server.service