github.com/feiyang21687/docker@v1.5.0/docs/sources/articles/cfengine_process_management.md (about)

     1  page_title: Process Management with CFEngine
     2  page_description: Managing containerized processes with CFEngine
     3  page_keywords: cfengine, process, management, usage, docker, documentation
     4  
     5  # Process Management with CFEngine
     6  
     7  Create Docker containers with managed processes.
     8  
     9  Docker monitors one process in each running container and the container
    10  lives or dies with that process. By introducing CFEngine inside Docker
    11  containers, we can alleviate a few of the issues that may arise:
    12  
    13   - It is possible to easily start multiple processes within a
    14     container, all of which will be managed automatically, with the
    15     normal `docker run` command.
    16   - If a managed process dies or crashes, CFEngine will start it again
    17     within 1 minute.
    18   - The container itself will live as long as the CFEngine scheduling
    19     daemon (cf-execd) lives. With CFEngine, we are able to decouple the
    20     life of the container from the uptime of the service it provides.
    21  
    22  ## How it works
    23  
    24  CFEngine, together with the cfe-docker integration policies, are
    25  installed as part of the Dockerfile. This builds CFEngine into our
    26  Docker image.
    27  
    28  The Dockerfile's `ENTRYPOINT` takes an arbitrary
    29  amount of commands (with any desired arguments) as parameters. When we
    30  run the Docker container these parameters get written to CFEngine
    31  policies and CFEngine takes over to ensure that the desired processes
    32  are running in the container.
    33  
    34  CFEngine scans the process table for the `basename` of the commands given
    35  to the `ENTRYPOINT` and runs the command to start the process if the `basename`
    36  is not found. For example, if we start the container with
    37  `docker run "/path/to/my/application parameters"`, CFEngine will look for a
    38  process named `application` and run the command. If an entry for `application`
    39  is not found in the process table at any point in time, CFEngine will execute
    40  `/path/to/my/application parameters` to start the application once again. The
    41  check on the process table happens every minute.
    42  
    43  Note that it is therefore important that the command to start your
    44  application leaves a process with the basename of the command. This can
    45  be made more flexible by making some minor adjustments to the CFEngine
    46  policies, if desired.
    47  
    48  ## Usage
    49  
    50  This example assumes you have Docker installed and working. We will
    51  install and manage `apache2` and `sshd`
    52  in a single container.
    53  
    54  There are three steps:
    55  
    56  1. Install CFEngine into the container.
    57  2. Copy the CFEngine Docker process management policy into the
    58     containerized CFEngine installation.
    59  3. Start your application processes as part of the `docker run` command.
    60  
    61  ### Building the image
    62  
    63  The first two steps can be done as part of a Dockerfile, as follows.
    64  
    65      FROM ubuntu
    66      MAINTAINER Eystein Måløy Stenberg <eytein.stenberg@gmail.com>
    67  
    68      RUN apt-get update && apt-get install -y wget lsb-release unzip ca-certificates
    69  
    70      # install latest CFEngine
    71      RUN wget -qO- http://cfengine.com/pub/gpg.key | apt-key add -
    72      RUN echo "deb http://cfengine.com/pub/apt $(lsb_release -cs) main" > /etc/apt/sources.list.d/cfengine-community.list
    73      RUN apt-get update && apt-get install -y cfengine-community
    74  
    75      # install cfe-docker process management policy
    76      RUN wget https://github.com/estenberg/cfe-docker/archive/master.zip -P /tmp/ && unzip /tmp/master.zip -d /tmp/
    77      RUN cp /tmp/cfe-docker-master/cfengine/bin/* /var/cfengine/bin/
    78      RUN cp /tmp/cfe-docker-master/cfengine/inputs/* /var/cfengine/inputs/
    79      RUN rm -rf /tmp/cfe-docker-master /tmp/master.zip
    80  
    81      # apache2 and openssh are just for testing purposes, install your own apps here
    82      RUN apt-get update && apt-get install -y openssh-server apache2
    83      RUN mkdir -p /var/run/sshd
    84      RUN echo "root:password" | chpasswd  # need a password for ssh
    85  
    86      ENTRYPOINT ["/var/cfengine/bin/docker_processes_run.sh"]
    87  
    88  By saving this file as Dockerfile to a working directory, you can then build
    89  your image with the docker build command, e.g.,
    90  `docker build -t managed_image`.
    91  
    92  ### Testing the container
    93  
    94  Start the container with `apache2` and `sshd` running and managed, forwarding
    95  a port to our SSH instance:
    96  
    97      $ sudo docker run -p 127.0.0.1:222:22 -d managed_image "/usr/sbin/sshd" "/etc/init.d/apache2 start"
    98  
    99  We now clearly see one of the benefits of the cfe-docker integration: it
   100  allows to start several processes as part of a normal `docker run` command.
   101  
   102  We can now log in to our new container and see that both `apache2` and `sshd`
   103  are running. We have set the root password to "password" in the Dockerfile
   104  above and can use that to log in with ssh:
   105  
   106      ssh -p222 root@127.0.0.1
   107  
   108      ps -ef
   109      UID        PID  PPID  C STIME TTY          TIME CMD
   110      root         1     0  0 07:48 ?        00:00:00 /bin/bash /var/cfengine/bin/docker_processes_run.sh /usr/sbin/sshd /etc/init.d/apache2 start
   111      root        18     1  0 07:48 ?        00:00:00 /var/cfengine/bin/cf-execd -F
   112      root        20     1  0 07:48 ?        00:00:00 /usr/sbin/sshd
   113      root        32     1  0 07:48 ?        00:00:00 /usr/sbin/apache2 -k start
   114      www-data    34    32  0 07:48 ?        00:00:00 /usr/sbin/apache2 -k start
   115      www-data    35    32  0 07:48 ?        00:00:00 /usr/sbin/apache2 -k start
   116      www-data    36    32  0 07:48 ?        00:00:00 /usr/sbin/apache2 -k start
   117      root        93    20  0 07:48 ?        00:00:00 sshd: root@pts/0
   118      root       105    93  0 07:48 pts/0    00:00:00 -bash
   119      root       112   105  0 07:49 pts/0    00:00:00 ps -ef
   120  
   121  If we stop apache2, it will be started again within a minute by
   122  CFEngine.
   123  
   124      service apache2 status
   125       Apache2 is running (pid 32).
   126      service apache2 stop
   127               * Stopping web server apache2 ... waiting    [ OK ]
   128      service apache2 status
   129       Apache2 is NOT running.
   130      # ... wait up to 1 minute...
   131      service apache2 status
   132       Apache2 is running (pid 173).
   133  
   134  ## Adapting to your applications
   135  
   136  To make sure your applications get managed in the same manner, there are
   137  just two things you need to adjust from the above example:
   138  
   139   - In the Dockerfile used above, install your applications instead of
   140     `apache2` and `sshd`.
   141   - When you start the container with `docker run`,
   142     specify the command line arguments to your applications rather than
   143     `apache2` and `sshd`.