using_supervisord.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. :title: Using Supervisor with Docker
  2. :description: How to use Supervisor process management with Docker
  3. :keywords: docker, supervisor, process management
  4. .. _using_supervisord:
  5. Using Supervisor with Docker
  6. ============================
  7. .. include:: example_header.inc
  8. Traditionally a Docker container runs a single process when it is launched, for
  9. example an Apache daemon or a SSH server daemon. Often though you want to run
  10. more than one process in a container. There are a number of ways you can
  11. achieve this ranging from using a simple Bash script as the value of your
  12. container's ``CMD`` instruction to installing a process management tool.
  13. In this example we're going to make use of the process management tool,
  14. `Supervisor <http://supervisord.org/>`_, to manage multiple processes in our
  15. container. Using Supervisor allows us to better control, manage, and restart the
  16. processes we want to run. To demonstrate this we're going to install and manage both an
  17. SSH daemon and an Apache daemon.
  18. Creating a Dockerfile
  19. ---------------------
  20. Let's start by creating a basic ``Dockerfile`` for our new image.
  21. .. code-block:: bash
  22. FROM ubuntu:latest
  23. MAINTAINER examples@docker.io
  24. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  25. RUN apt-get update
  26. RUN apt-get upgrade -y
  27. Installing Supervisor
  28. ---------------------
  29. We can now install our SSH and Apache daemons as well as Supervisor in our container.
  30. .. code-block:: bash
  31. RUN apt-get install -y openssh-server apache2 supervisor
  32. RUN mkdir -p /var/run/sshd
  33. RUN mkdir -p /var/log/supervisor
  34. Here we're installing the ``openssh-server``, ``apache2`` and ``supervisor``
  35. (which provides the Supervisor daemon) packages. We're also creating two new
  36. directories that are needed to run our SSH daemon and Supervisor.
  37. Adding Supervisor's configuration file
  38. --------------------------------------
  39. Now let's add a configuration file for Supervisor. The default file is called
  40. ``supervisord.conf`` and is located in ``/etc/supervisor/conf.d/``.
  41. .. code-block:: bash
  42. ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
  43. Let's see what is inside our ``supervisord.conf`` file.
  44. .. code-block:: bash
  45. [supervisord]
  46. nodaemon=true
  47. [program:sshd]
  48. command=/usr/sbin/sshd -D
  49. [program:apache2]
  50. command=/bin/bash -c "source /etc/apache2/envvars && /usr/sbin/apache2 -DFOREGROUND"
  51. The ``supervisord.conf`` configuration file contains directives that configure
  52. Supervisor and the processes it manages. The first block ``[supervisord]``
  53. provides configuration for Supervisor itself. We're using one directive,
  54. ``nodaemon`` which tells Supervisor to run interactively rather than daemonize.
  55. The next two blocks manage the services we wish to control. Each block controls
  56. a separate process. The blocks contain a single directive, ``command``, which
  57. specifies what command to run to start each process.
  58. Exposing ports and running Supervisor
  59. -------------------------------------
  60. Now let's finish our ``Dockerfile`` by exposing some required ports and
  61. specifying the ``CMD`` instruction to start Supervisor when our container
  62. launches.
  63. .. code-block:: bash
  64. EXPOSE 22 80
  65. CMD ["/usr/bin/supervisord"]
  66. Here we've exposed ports 22 and 80 on the container and we're running the
  67. ``/usr/bin/supervisord`` binary when the container launches.
  68. Building our container
  69. ----------------------
  70. We can now build our new container.
  71. .. code-block:: bash
  72. sudo docker build -t <yourname>/supervisord .
  73. Running our Supervisor container
  74. --------------------------------
  75. Once we've got a built image we can launch a container from it.
  76. .. code-block:: bash
  77. sudo docker run -p 22 -p 80 -t -i <yourname>/supervisor
  78. 2013-11-25 18:53:22,312 CRIT Supervisor running as root (no user in config file)
  79. 2013-11-25 18:53:22,312 WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing
  80. 2013-11-25 18:53:22,342 INFO supervisord started with pid 1
  81. 2013-11-25 18:53:23,346 INFO spawned: 'sshd' with pid 6
  82. 2013-11-25 18:53:23,349 INFO spawned: 'apache2' with pid 7
  83. . . .
  84. We've launched a new container interactively using the ``docker run`` command.
  85. That container has run Supervisor and launched the SSH and Apache daemons with
  86. it. We've specified the ``-p`` flag to expose ports 22 and 80. From here we can
  87. now identify the exposed ports and connect to one or both of the SSH and Apache
  88. daemons.