summaryrefslogtreecommitdiff
path: root/docs/sources/use/host_integration.rst
blob: cb920a5908c9e29174a4593e69423139e9c1e3f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
:title: Automatically Start Containers
:description: How to generate scripts for upstart, systemd, etc.
:keywords: systemd, upstart, supervisor, docker, documentation, host integration



Automatically Start Containers
==============================

You can use your Docker containers with process managers like ``upstart``,
``systemd`` and ``supervisor``.

Introduction
------------

If you want a process manager to manage your containers you will need to run
the docker daemon with the ``-r=false`` so that docker will not automatically 
restart your containers when the host is restarted.  

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.  When your run ``docker start -a`` docker will automatically attach 
to the 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.  

Here are a few sample scripts for systemd and upstart to integrate with docker.


Sample Upstart Script
---------------------

In this example we've already created a container to run Redis with 
``--name redis_server``.  To create an upstart script for our container, 
we create a file named ``/etc/init/redis.conf`` and place the following 
into it:

.. code-block:: bash

   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

Next, we have to configure docker so that it's run with the option ``-r=false``.
Run the following command:

.. code-block:: bash

   $ sudo sh -c "echo 'DOCKER_OPTS=\"-r=false\"' > /etc/default/docker"


Sample systemd Script
---------------------

.. code-block:: bash

    [Unit]
    Description=Redis container
    Author=Me
    After=docker.service

    [Service]
    Restart=always
    ExecStart=/usr/bin/docker start -a redis_server
    ExecStop=/usr/bin/docker stop -t 2 redis_server

    [Install]
    WantedBy=local.target