host_integration.rst 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 can then attach a process manager to manage
  15. it. When your run ``docker start -a`` docker will automatically attach
  16. to the running container, or start it if needed and forward all signals
  17. so that the process manager can detect when a container stops and correctly
  18. restart it.
  19. Here are a few sample scripts for systemd and upstart to integrate with docker.
  20. Sample Upstart Script
  21. ---------------------
  22. In this example we've already created a container to run Redis with
  23. ``--name redis_server``. To create an upstart script for our container,
  24. we create a file named ``/etc/init/redis.conf`` and place the following
  25. into it:
  26. .. code-block:: bash
  27. description "Redis container"
  28. author "Me"
  29. start on filesystem and started docker
  30. stop on runlevel [!2345]
  31. respawn
  32. script
  33. # Wait for docker to finish starting up first.
  34. FILE=/var/run/docker.sock
  35. while [ ! -e $FILE ] ; do
  36. inotifywait -t 2 -e create $(dirname $FILE)
  37. done
  38. /usr/bin/docker start -a redis_server
  39. end script
  40. Next, we have to configure docker so that it's run with the option ``-r=false``.
  41. Run the following command:
  42. .. code-block:: bash
  43. $ sudo sh -c "echo 'DOCKER_OPTS=\"-r=false\"' > /etc/default/docker"
  44. Sample systemd Script
  45. ---------------------
  46. .. code-block:: bash
  47. [Unit]
  48. Description=Redis container
  49. Author=Me
  50. After=docker.service
  51. [Service]
  52. Restart=always
  53. ExecStart=/usr/bin/docker start -a redis_server
  54. ExecStop=/usr/bin/docker stop -t 2 redis_server
  55. [Install]
  56. WantedBy=local.target