title: How to integrate Capistrano with Docker for deployment?
body: <p>I am not sure my question is relevant as I may try to mix tools (Capistrano and Docker) that should not be mixed.</p>
  
  <p>I have recently dockerized an application that is deployed with Capistrano. Docker compose is used both for development and staging environments.</p>
  
  <p>This is how my project looks like (the application files are not shown):</p>
  
  <pre><code>Capfile
  docker-compose.yml
  docker-compose.staging.yml
  config/
      deploy.rb
      deploy
          staging.rb
  </code></pre>
  
  <p>The Docker Compose files creates all the necessary containers (Nginx, PHP, MongoDB, Elasticsearch, etc.) to run the app in development or staging environment (hence some specific parameters defined in <code>docker-compose.staging.yml</code>).</p>
  
  <p>The app is deployed to the staging environment with this command:</p>
  
  <pre><code>cap staging deploy
  </code></pre>
  
  <p>The folder architecture on the server is the one of Capistrano:</p>
  
  <pre><code>current
  releases
      20160912150720
      20160912151003
      20160912153905
  shared
  </code></pre>
  
  <p>The following command has been run in the <code>current</code> directory of the staging server to instantiate all the necessary containers to run the app:</p>
  
  <pre><code>docker-compose -f docker-compose.yml -f docker-compose.staging.yml up -d
  </code></pre>
  
  <p>So far so good. Things get more complicated on the next deploy: the <code>current</code> symlink will point to a new directory of the <code>releases</code> directory:</p>
  
  <ul>
  <li>If <code>deploy.rb</code> defines commands that need to be executed inside containers (like <code>docker-compose exec php composer install</code> for PHP), Docker tells that the containers don't exist yet (because the existing ones were created on the previous release folder).</li>
  <li>If a <code>docker-compose up -d</code> command is executed in the Capistrano deployment process, I get some errors because of port conflicts (the previous containers still exist).</li>
  </ul>
  
  <p>Do you have an idea on how to solve this issue? Should I move away from Capistrano and do something different?</p>
  
  <p>The idea would be to keep the (near) zero-downtime deployment that Capistrano offers with the flexibility of Docker containers (providing several PHP versions for various apps on the same server for instance).</p>