host_integration.rst 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. :title: Host Integration
  2. :description: How to generate scripts for upstart, systemd, etc.
  3. :keywords: systemd, upstart, supervisor, docker, documentation, host integration
  4. Host Integration
  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 lxc-net 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 edit the docker upstart script (``/etc/init/docker.conf``)
  39. so that we run docker with ``-r=false``. In this example, we also ensure
  40. that docker will start running before *redis* is started.
  41. .. code-block:: bash
  42. description "Docker daemon"
  43. start on filesystem and started lxc-net
  44. start on (starting redis)
  45. stop on runlevel [!2345]
  46. respawn
  47. script
  48. /usr/bin/docker -d -r=false
  49. end script
  50. Sample systemd Script
  51. ---------------------
  52. .. code-block:: bash
  53. [Unit]
  54. Description=Redis container
  55. Author=Me
  56. After=docker.service
  57. [Service]
  58. Restart=always
  59. ExecStart=/usr/bin/docker start -a 0a7e070b698b
  60. ExecStop=/usr/bin/docker stop -t 2 0a7e070b698b
  61. [Install]
  62. WantedBy=local.target