title: Best way to manage docker containers with supervisord
body: <p>I have to setup "dockerized" environments (integration, qa and production) on the <strong>same</strong> server (client's requirement). Each environment will be composed as follow:</p>
  
  <ul>
  <li>rabbitmq</li>
  <li>celery</li>
  <li>flower</li>
  <li>python 3 based application called "A" (specific branch per
  environment)</li>
  </ul>
  
  <p>Over them, jenkins will handle the deployment based on CI. </p>
  
  <p>Using set of containers per environment sounds like the best approach. </p>
  
  <p>But now I need, process manager to run and supervise all of them:</p>
  
  <ul>
  <li>3 rabbit containers,</li>
  <li>3 celery/flower containers,</li>
  <li>3 "A" containers,</li>
  <li>1 jenkins containers.</li>
  </ul>
  
  <p>Supervisord seem to be the best choice, but during my tests, i'm not able to "properly" restart a container. Here a snippet of the supervisord.conf</p>
  
  <pre><code>[program:docker-rabbit]
  command=/usr/bin/docker run -p 5672:5672 -p 15672:15672 tutum/rabbitmq
  startsecs=20
  autorestart=unexpected
  exitcodes=0,1
  stopsignal=KILL
  </code></pre>
  
  <p>So I wonder what is the best way to separate each environment and be able to manage and supervise each service (a container). </p>
  
  <p><strong>[EDIT My solution inspired by Thomas response]</strong></p>
  
  <p>each container is run by a .sh script that looking like</p>
  
  <p><em>rabbit-integration.py</em></p>
  
  <pre><code>#!/bin/bash
  
  #set -x
  SERVICE="rabbitmq"
  SH_S = "/path/to_shs"
  export MY_ENV="integration"
  . $SH_S/env_.sh
  . $SH_S/utils.sh
  
  SERVICE_ENV=$SERVICE-$MY_ENV
  ID_FILE=/tmp/$SERVICE_ENV.name # pid file 
  
  trap stop SIGHUP SIGINT SIGTERM  # trap signal for calling the stop function
  run_rabbitmq   
  </code></pre>
  
  <p><em>$SH_S/env_.sh</em> is looking like:</p>
  
  <pre><code># set env variable 
  ...
  case $MONARCH_ENV in
      $INTEGRATION)
         AMQP_PORT="5672"
         AMQP_IP="172.17.42.1"
       ...
      ;;
      $PREPRODUCTION)
         AMQP_PORT="5673"
         AMQP_IP="172.17.42.1"
         ...
          ;;
      $PRODUCTION)
          AMQP_PORT="5674"
          REDIS_IP="172.17.42.1"
          ...
  esac
  </code></pre>
  
  <p><em>$SH_S/utils.sh</em> is looking like:</p>
  
  <pre><code>#!/bin/bash
  
  function random_name(){
          echo "$SERVICE_ENV-$(cat /proc/sys/kernel/random/uuid)"
  }
  function stop (){
          echo "stopping docker container..."
          /usr/bin/docker stop `cat $ID_FILE`
  }
  function run_rabbitmq (){
          # do no daemonize and use stdout
          NAME="$(random_name)"
          echo $NAME &gt; $ID_FILE
          /usr/bin/docker run -i --name "$NAME" -p $AMQP_IP:$AMQP_PORT:5672 -p $AMQP_ADMIN_PORT:15672 -e RABBITMQ_PASS="$AMQP_PASSWORD" myimage-rabbitmq &amp;
          PID=$!
          wait $PID
  }
  </code></pre>
  
  <p>At least <em>myconfig.intergration.conf</em> is looking like:</p>
  
  <pre><code>[program:rabbit-integration]
  command=/path/sh_s/rabbit-integration.sh
  startsecs=20
  priority=90
  autorestart=unexpected
  exitcodes=0,1
  stopsignal=TERM
  </code></pre>
  
  <p>In the case i want use the same container the startup function is looking like:</p>
  
  <pre><code>function _run_my_container () {
      NAME="my_container"
      /usr/bin/docker start -i $NAME &amp;
      PID=$!
      wait $PID
      rc=$?
      if [[ $rc != 0 ]]; then
         _run_my_container 
      fi
  }
  </code></pre>
  
  <p>where </p>
  
  <pre><code>function _run_my_container (){
      /usr/bin/docker run -p{} -v{} --name "$NAME" myimage &amp;
      PID=$!
      wait $PID
  }
  </code></pre>