host_integration.rst 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. :title: Automatically Start Containers
  2. :description: How to generate scripts for upstart, systemd, etc.
  3. :keywords: systemd, upstart, supervisor, docker, documentation, host integration
  4. Automatically Start Containers
  5. ==============================
  6. You can use your Docker containers with process managers like ``upstart``,
  7. ``systemd`` and ``supervisor``.
  8. Introduction
  9. ------------
  10. If you want a process manager to manage your containers you will need to run
  11. the docker daemon with the ``-r=false`` so that docker will not automatically
  12. restart your containers when the host is restarted.
  13. When you have finished setting up your image and are happy with your
  14. running container, you may want to use a process manager to manage
  15. it. When your run ``docker start -a`` docker will automatically attach
  16. to the process and forward all signals so that the process manager can
  17. detect when a container stops and correctly restart it.
  18. Here are a few sample scripts for systemd and upstart to integrate with docker.
  19. Sample Upstart Script
  20. ---------------------
  21. In this example we've already created a container to run Redis with an id of
  22. 0a7e070b698b. To create an upstart script for our container, we create a file
  23. named ``/etc/init/redis.conf`` and place the following into it:
  24. .. code-block:: bash
  25. description "Redis container"
  26. author "Me"
  27. start on filesystem and started docker
  28. stop on runlevel [!2345]
  29. respawn
  30. script
  31. # Wait for docker to finish starting up first.
  32. FILE=/var/run/docker.sock
  33. while [ ! -e $FILE ] ; do
  34. inotifywait -t 2 -e create $(dirname $FILE)
  35. done
  36. /usr/bin/docker start -a 0a7e070b698b
  37. end script
  38. Next, we have to configure docker so that it's run with the option ``-r=false``.
  39. Run the following command:
  40. .. code-block:: bash
  41. $ sudo sh -c "echo 'DOCKER_OPTS=\"-r=false\"' > /etc/default/docker"
  42. Sample systemd Script
  43. ---------------------
  44. .. code-block:: bash
  45. [Unit]
  46. Description=Redis container
  47. Author=Me
  48. After=docker.service
  49. [Service]
  50. Restart=always
  51. ExecStart=/usr/bin/docker start -a 0a7e070b698b
  52. ExecStop=/usr/bin/docker stop -t 2 0a7e070b698b
  53. [Install]
  54. WantedBy=local.target