A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 4 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. title: Docker in Action - fitter, happier, more productive - Real Python
  2. url: https://realpython.com/blog/python/docker-in-action-fitter-happier-more-productive/
  3. hash_url: 37adabe90396072db9cfdd99baa61cd0
  4. <div class="center-text">
  5. <img class="no-border" src="https://realpython.com/images/blog_images/docker-in-action/docker-in-action.png" alt="docker in action"/>
  6. </div>
  7. <p>With Docker you can easily deploy a web application along with it’s dependencies, environment variables, and configuration settings – everything you need to recreate your environment quickly and efficiently.</p>
  8. <p>This tutorial looks at just that.</p>
  9. <p><strong>Updated 02/28/2015</strong>: Added <a href="https://docs.docker.com/compose/">Docker Compose</a> and upgraded Docker and boot2docker to the latest versions.</p>
  10. <hr/>
  11. <p><strong>We’ll start by creating a Docker container for running a Python Flask application. From there, we’ll look at a nice development workflow to manage the local development of an app as well as continuous integration and delivery, step by step …</strong></p>
  12. <blockquote><p>I (<a href="https://twitter.com/mikeherman">Michael Herman</a>) originally presented this workflow at <a href="https://www.pytennessee.org/"> PyTennessee</a> on February 8th, 2015. You can view the slides <a href="http://realpython.github.io/fitter-happier-docker/">here</a>, if interested.</p></blockquote>
  13. <h2>Workflow</h2>
  14. <ol>
  15. <li>Code locally on a feature branch</li>
  16. <li>Open a pull request on Github against the master branch</li>
  17. <li>Run automated tests against the Docker container</li>
  18. <li>If the tests pass, manually merge the pull request into master</li>
  19. <li>Once merged, the automated tests run again</li>
  20. <li>If the second round of tests pass, a build is created on Docker Hub</li>
  21. <li>Once the build is created, it’s then automatically (err, automagically) deployed to production</li>
  22. </ol>
  23. <div class="center-text">
  24. <img class="no-border" src="https://realpython.com/images/blog_images/docker-in-action/steps.jpg" alt="docker in action workflow"/>
  25. </div>
  26. <blockquote><p>This tutorial is meant for Mac OS X users, and we’ll be utilizing the following tools/technologies – Python v2.7.9, Flask v0.10.1, Docker v1.5.0, Docker Compose, v1.1.0, boot2docker 1.5.0, Redis v2.8.19</p></blockquote>
  27. <p>Let’s get to it…</p>
  28. <hr/>
  29. <p>First, some Docker-specific terms:</p>
  30. <ul>
  31. <li>A <em>Dockerfile</em> is a file that contains a set of instructions used to create an <em>image</em>.</li>
  32. <li>An <em>image</em> is used to build and save snapshots (the state) of an environment.</li>
  33. <li>A <em>container</em> is an instantiated, live <em>image</em> that runs a collection of processes.</li>
  34. </ul>
  35. <blockquote><p>Be sure to check out the Docker <a href="https://docs.docker.com/">documentation</a> for more info on <a href="https://docs.docker.com/reference/builder/">Dockerfiles</a>, <a href="https://docs.docker.com/terms/image/">images</a>, and <a href="https://docs.docker.com/terms/container/">containers</a>.</p></blockquote>
  36. <h2>Why Docker?</h2>
  37. <p>You can truly mimic your production environment on your local machine. No more having to debug environment specific bugs or worrying that your app will perform differently in production.</p>
  38. <ol>
  39. <li>Version control for infrastructure</li>
  40. <li>Easily distribute/recreate your entire development environment</li>
  41. <li>Build once, run anywhere – aka The Holy Grail!</li>
  42. </ol>
  43. <h2>Docker Setup</h2>
  44. <p>Since Darwin (the kernel for OS X) does not have the Linux kernel features required to run Docker containers, we need to install <a href="http://boot2docker.io/">boot2docker</a> – which is a <em>lightweight</em>t Linux distribution designed specifically to run Docker. In essence, it starts a small VM that’s configured to run Docker containers.</p>
  45. <p>Create a new directory called “fitter-happier-docker” to house your Flask project.</p>
  46. <p>Next follow the instructions from the guide <a href="https://docs.docker.com/installation/mac/">Installing Docker on Mac OS X</a> to install both Docker and the official boot2docker package.</p>
  47. <p>Test the install:</p>
  48. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  49. <span class="line-number">2</span>
  50. <span class="line-number">3</span>
  51. </pre></td><td class="code"><pre><code class="sh"><span class="line"><span class="nv">$ </span>boot2docker version
  52. </span><span class="line">Boot2Docker-cli version: v1.5.0
  53. </span><span class="line">Git commit: ccd9032
  54. </span></code></pre></td></tr></table></div></figure>
  55. <h2>Compose Up!</h2>
  56. <p><a href="http://docs.docker.com/compose/">Docker Compose</a> is an orchestration framework that handles the building and running of multiple services (via separate containers) using a simple <em>.yml</em> file. It makes it super easy to link services together running in different containers.</p>
  57. <blockquote><p>Following along with me? Grab the code in a pre-Compose state from the <a href="https://github.com/realpython/fitter-happier-docker/releases/tag/pre-compose">repository</a>.</p></blockquote>
  58. <p>Start by installing the requirements via pip and then make sure Compose is installed:</p>
  59. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  60. <span class="line-number">2</span>
  61. <span class="line-number">3</span>
  62. </pre></td><td class="code"><pre><code class="sh"><span class="line"><span class="nv">$ </span>pip install docker-compose
  63. </span><span class="line"><span class="nv">$ </span>docker-compose --version
  64. </span><span class="line">docker-compose 1.1.0
  65. </span></code></pre></td></tr></table></div></figure>
  66. <p>Now let’s get our Flask application up and running along with Redis.</p>
  67. <p>Add a new file called <em>docker-compose.yml</em> to the root directory:</p>
  68. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  69. <span class="line-number">2</span>
  70. <span class="line-number">3</span>
  71. <span class="line-number">4</span>
  72. <span class="line-number">5</span>
  73. <span class="line-number">6</span>
  74. <span class="line-number">7</span>
  75. <span class="line-number">8</span>
  76. <span class="line-number">9</span>
  77. <span class="line-number">10</span>
  78. <span class="line-number">11</span>
  79. <span class="line-number">12</span>
  80. <span class="line-number">13</span>
  81. </pre></td><td class="code"><pre><code class="yaml"><span class="line"><span class="l-Scalar-Plain">web</span><span class="p-Indicator">:</span>
  82. </span><span class="line"> <span class="l-Scalar-Plain">build</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">web</span>
  83. </span><span class="line"> <span class="l-Scalar-Plain">volumes</span><span class="p-Indicator">:</span>
  84. </span><span class="line"> <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">web:/code</span>
  85. </span><span class="line"> <span class="l-Scalar-Plain">ports</span><span class="p-Indicator">:</span>
  86. </span><span class="line"> <span class="p-Indicator">-</span> <span class="s">"80:5000"</span>
  87. </span><span class="line"> <span class="l-Scalar-Plain">links</span><span class="p-Indicator">:</span>
  88. </span><span class="line"> <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">redis</span>
  89. </span><span class="line"> <span class="l-Scalar-Plain">command</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">python app.py</span>
  90. </span><span class="line"><span class="l-Scalar-Plain">redis</span><span class="p-Indicator">:</span>
  91. </span><span class="line"> <span class="l-Scalar-Plain">image</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">redis:2.8.19</span>
  92. </span><span class="line"> <span class="l-Scalar-Plain">ports</span><span class="p-Indicator">:</span>
  93. </span><span class="line"> <span class="p-Indicator">-</span> <span class="s">"6379:6379"</span>
  94. </span></code></pre></td></tr></table></div></figure>
  95. <p>Here we add the services that make up our stack:</p>
  96. <ol>
  97. <li><strong>web</strong>: First, we build the image from the “web” directory and then mount that directory to the “code” directory within the Docker container. The Flask app is ran via the <code>python app.py</code> command. This exposes port 5000 on the container, which is forwarded to port 80 on the host environment.</li>
  98. <li><strong>redis</strong>: Next, the Redis service is built from the Docker Hub “Redis” <a href="https://registry.hub.docker.com/_/redis/">image</a>. Port 6379 is exposed and forwarded.</li>
  99. </ol>
  100. <p>Did you notice the Dockerfile in the “web” directory? This file is used to build our image, starting with an Ubuntu base, the required dependencies are installed and the app is built.</p>
  101. <h2>Build and run</h2>
  102. <p>With one simple command we can build the image and run the container:</p>
  103. <figure class="code"><figcaption><span/></figcaption></figure>
  104. <div class="center-text">
  105. <img class="no-border" src="https://realpython.com/images/blog_images/docker-in-action/figup.png" alt="fig up"/>
  106. </div>
  107. <p>This command builds an image for our Flask app, pulls the Redis image, and then starts everything up.</p>
  108. <p>Grab a cup of coffee. Or two. This will take some time the first time you build the container. That said, since Docker caches each step (or <em><a href="https://docs.docker.com/terms/layer/">layer</a></em>) of the build process from the Dockerfile, rebuilding will happen <em>much</em> quicker because only the steps that have <em>changed</em> since the last build are rebuilt.</p>
  109. <blockquote><p>If you do change a line/step/layer in your Dockerfile, it will recreate/rebuild everything in that line – so be mindful of this when you structure your Dockerfile.</p></blockquote>
  110. <p>Docker Compose brings each container up at once in parallel. Each container also has a unique name and each process within the stack trace/log is color-coded for readability.</p>
  111. <p>Ready to test?</p>
  112. <p>Open your web browser and navigate to the IP address associated with the <code>DOCKER_HOST</code> variable – i.e., <a href="http://192.168.59.103/,">http://192.168.59.103/,</a> in this example. (Run <code>boot2docker ip</code> to get the address.)</p>
  113. <p>You should see the text, “Hello! This page has been seen 1 times.” in your browser:</p>
  114. <div class="center-text">
  115. <img class="no-border" src="https://realpython.com/images/blog_images/docker-in-action/test.png" alt="test flask app running on docker"/>
  116. </div>
  117. <p>Refresh. The page counter should have incremented.</p>
  118. <p>Kill the processes (Ctrl-C), and then run the following command to run the process in the background.</p>
  119. <figure class="code"><figcaption><span/></figcaption></figure>
  120. <p>Want to view the currently running processes?</p>
  121. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  122. <span class="line-number">2</span>
  123. <span class="line-number">3</span>
  124. <span class="line-number">4</span>
  125. <span class="line-number">5</span>
  126. </pre></td><td class="code"><pre><code class="sh"><span class="line"><span class="nv">$ </span>docker-compose ps
  127. </span><span class="line"> Name Command State Ports
  128. </span><span class="line">--------------------------------------------------------------------------------------------------
  129. </span><span class="line">fitterhappierdocker_redis_1 /entrypoint.sh redis-server Up 0.0.0.0:6379-&gt;6379/tcp
  130. </span><span class="line">fitterhappierdocker_web_1 python app.py Up 0.0.0.0:80-&gt;5000/tcp, 80/tcp
  131. </span></code></pre></td></tr></table></div></figure>
  132. <blockquote><p>Both processes are running in a different container, connected via Docker Compose!</p></blockquote>
  133. <h2>Next Steps</h2>
  134. <p>Once done, kill the processes via <code>docker-compose stop</code>, then run <code>boot2docker down</code> to gracefully shutdown the VM. Commit your changes locally, and then push to Github.</p>
  135. <p>So, what did we accomplish?</p>
  136. <p>We set up our local environment, detailing the basic process of building an <em>image</em> from a <em>Dockerfile</em> and then creating an instance of the <em>image</em> called a <em>container</em>. We tied everything together with Docker Compose to build and connect different containers for both the Flask app and Redis process.</p>
  137. <p><strong>Now, let’s look at a nice continuous integration workflow powered by <a href="https://circleci.com/">CircleCI</a></strong>.</p>
  138. <blockquote><p>Still with me? You can grab the updated code from the <a href="https://github.com/realpython/fitter-happier-docker/releases/tag/added-compose">repository</a>.</p></blockquote>
  139. <h2>Docker Hub</h2>
  140. <p>Thus far we’ve worked with Dockerfiles, images, and containers (abstracted by Docker Compose, of course).</p>
  141. <p>Are you familiar with the Git workflow? Images are like Git repositories while containers are similar to a cloned repository. Sticking with that metaphor, <a href="https://hub.docker.com/">Docker Hub</a>, which is repository of Docker images, is akin to Github.</p>
  142. <ol>
  143. <li>Signup <a href="https://hub.docker.com/account/signup/">here</a>, using your Github credentials.</li>
  144. <li>Then add a new automated build. And add your Github repo that you created and pushed to. Just accept all the default options, except for the “Dockerfile Location” – change this to “/web”.</li>
  145. </ol>
  146. <p>Once added, this will trigger an initial build. Make sure the build is successful.</p>
  147. <h3>Docker Hub for CI</h3>
  148. <p>Docker Hub, in itself, acts as a continuous integration server since you can configure it to create an <a href="https://docs.docker.com/userguide/dockerrepos/#automated-builds">automated build</a> every time you push a new commit to Github. In other words, it ensures you do not cause a regression that completely breaks the build process when the code base is updated.</p>
  149. <blockquote><p>There are some drawbacks to this approach – namely that you cannot push (via <code>docker push</code>) updated images directly to Docker Hub. Docker Hub must pull in changes from your repo and create the images itself to ensure that their are no errors. Keep this in mind as you go through this workflow. The Docker documentation is not clear with regard to this matter.</p></blockquote>
  150. <p>Let’s test this out. Add an assert to the test suite:</p>
  151. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  152. </pre></td><td class="code"><pre><code class="python"><span class="line"><span class="bp">self</span><span class="o">.</span><span class="n">assertNotEqual</span><span class="p">(</span><span class="n">four</span><span class="p">,</span> <span class="mi">102</span><span class="p">)</span>
  153. </span></code></pre></td></tr></table></div></figure>
  154. <p>Commit and push to Github to generate a new build on Docker Hub. Success?</p>
  155. <p><strong>Bottom-line:</strong> It’s good to know that if a commit does cause a regression that Docker Hub will catch it, but since this is the last line of defense before deploying (to either staging or production) you ideally want to catch any breaks before generating a new build on Docker Hub. Plus, you also want to run your unit and integration tests from a <em>true</em> continuous integration server – which is exactly where CircleCI comes into play.</p>
  156. <h2>CircleCI</h2>
  157. <div class="center-text">
  158. <img class="no-border" src="https://realpython.com/images/blog_images/docker-in-action/circleci.png" alt="circleci"/>
  159. </div>
  160. <p><a href="https://circleci.com/">CircleCI</a> is a continuous integration and delivery platform that supports testing within Docker containers. Given a Dockerfile, CircleCI builds an image, starts a new container, and then runs tests inside that container.</p>
  161. <p>Remember the workflow we want? <a href="https://realpython.com/blog/python/docker-in-action-fitter-happier-more-productive/#workflow">Link</a>.</p>
  162. <p>Let’s take a look at how to achieve just that…</p>
  163. <h3>Setup</h3>
  164. <p>The best place to start is the excellent <a href="https://circleci.com/docs/getting-started">Getting started with CircleCI</a> guide…</p>
  165. <p>Sign up with your Github account, then add the Github repo to create a new project. This will automatically add a webhook to the repo so that anytime you push to Github a new build is triggered. You should receive an email once the hook is added.</p>
  166. <p>Next we need to add a configuration file to the root folder of repo so that CircleCI can properly create the build.</p>
  167. <h3><em>circle.yml</em></h3>
  168. <p>Add the following build commands/steps:</p>
  169. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  170. <span class="line-number">2</span>
  171. <span class="line-number">3</span>
  172. <span class="line-number">4</span>
  173. <span class="line-number">5</span>
  174. <span class="line-number">6</span>
  175. <span class="line-number">7</span>
  176. <span class="line-number">8</span>
  177. <span class="line-number">9</span>
  178. <span class="line-number">10</span>
  179. <span class="line-number">11</span>
  180. <span class="line-number">12</span>
  181. </pre></td><td class="code"><pre><code class="yaml"><span class="line"><span class="l-Scalar-Plain">machine</span><span class="p-Indicator">:</span>
  182. </span><span class="line"> <span class="l-Scalar-Plain">services</span><span class="p-Indicator">:</span>
  183. </span><span class="line"> <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">docker</span>
  184. </span><span class="line">
  185. </span><span class="line"><span class="l-Scalar-Plain">dependencies</span><span class="p-Indicator">:</span>
  186. </span><span class="line"> <span class="l-Scalar-Plain">override</span><span class="p-Indicator">:</span>
  187. </span><span class="line"> <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">pip install -r requirements.txt</span>
  188. </span><span class="line">
  189. </span><span class="line"><span class="l-Scalar-Plain">test</span><span class="p-Indicator">:</span>
  190. </span><span class="line"> <span class="l-Scalar-Plain">override</span><span class="p-Indicator">:</span>
  191. </span><span class="line"> <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">docker-compose run -d --no-deps web</span>
  192. </span><span class="line"> <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">python web/tests.py</span>
  193. </span></code></pre></td></tr></table></div></figure>
  194. <p>Essentially, we create a new image, run the container, then run your unit tests.</p>
  195. <blockquote><p>Notice how we’re using the command <code>docker-compose run -d --no-deps web</code>, to run the web process, instead of <code>docker-compose up</code>. This is because CircleCI already has Redis <a href="https://circleci.com/docs/environment#databases">running</a> and available to us for our tests. So, we just need to run the web process.</p></blockquote>
  196. <p>With the <em>circle.yml</em> file created, push the changes to Github to trigger a new build. <em>Remember: this will also trigger a new build on Docker Hub.</em></p>
  197. <p>Success?</p>
  198. <p>Before moving on, we need to change our workflow since we won’t be pushing directly to the master branch anymore.</p>
  199. <h3>Feature Branch Workflow</h3>
  200. <blockquote><p>For these unfamiliar with the Feature Branch workflow, check out <a href="https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow">this</a> excellent introduction.</p></blockquote>
  201. <p>Let’s run through a quick example…</p>
  202. <h3>Create the feature branch</h3>
  203. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  204. <span class="line-number">2</span>
  205. </pre></td><td class="code"><pre><code class="sh"><span class="line"><span class="nv">$ </span>git checkout -b circle-test master
  206. </span><span class="line">Switched to a new branch <span class="s1">'circle-test'</span>
  207. </span></code></pre></td></tr></table></div></figure>
  208. <h3>Update the app</h3>
  209. <p>Add a new assert in <em>tests.py</em>:</p>
  210. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  211. </pre></td><td class="code"><pre><code class="python"><span class="line"><span class="bp">self</span><span class="o">.</span><span class="n">assertNotEqual</span><span class="p">(</span><span class="n">four</span><span class="p">,</span> <span class="mi">60</span><span class="p">)</span>
  212. </span></code></pre></td></tr></table></div></figure>
  213. <h3>Issue a Pull Request</h3>
  214. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  215. <span class="line-number">2</span>
  216. <span class="line-number">3</span>
  217. </pre></td><td class="code"><pre><code class="sh"><span class="line"><span class="nv">$ </span>git add web/tests.py
  218. </span><span class="line"><span class="nv">$ </span>git commit -m <span class="s2">"circle-test"</span>
  219. </span><span class="line"><span class="nv">$ </span>git push origin circle-test
  220. </span></code></pre></td></tr></table></div></figure>
  221. <p>Even before you create the actual pull request, CircleCI starts creating the build. Go ahead and create the pull request, then once the tests pass on CircleCI, press the Merge button. Once merged, the build is triggered on Docker Hub.</p>
  222. <h3>Refactoring the workflow</h3>
  223. <p>If you jump back to the overall workflow at the <a href="https://realpython.com/blog/python/docker-in-action-fitter-happier-more-productive/#workflow">top of this post</a>, you’ll see that we don’t actually want to trigger a new build on Docker Hub until the tests pass against the master branch. So, let’s make some quick changes to the workflow:</p>
  224. <ol>
  225. <li>Open your repository on Docker Hub, and then under <em>Settings</em> click <em>Automated Build</em>.</li>
  226. <li>Uncheck the Active box: “When active we will build when new pushes occur”.</li>
  227. <li>Save.</li>
  228. <li>Click <em>Build Triggers</em> under <em>Settings</em></li>
  229. <li>Change the status to on.</li>
  230. <li>Copy the example curl command – i.e., <code>$ curl --data "build=true" -X POST https://registry.hub.docker.com/u/mjhea0/fitter-happier-docker/trigger/84957124-2b85-410d-b602-b48193853b66/</code></li>
  231. </ol>
  232. <p>Now add the following code to the bottom of your <em>circle.yml</em> file:</p>
  233. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  234. <span class="line-number">2</span>
  235. <span class="line-number">3</span>
  236. <span class="line-number">4</span>
  237. <span class="line-number">5</span>
  238. </pre></td><td class="code"><pre><code class="yaml"><span class="line"><span class="l-Scalar-Plain">deployment</span><span class="p-Indicator">:</span>
  239. </span><span class="line"> <span class="l-Scalar-Plain">hub</span><span class="p-Indicator">:</span>
  240. </span><span class="line"> <span class="l-Scalar-Plain">branch</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">master</span>
  241. </span><span class="line"> <span class="l-Scalar-Plain">commands</span><span class="p-Indicator">:</span>
  242. </span><span class="line"> <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">$DEPLOY</span>
  243. </span></code></pre></td></tr></table></div></figure>
  244. <p>Here we fire the <code>$DEPLOY</code> variable <em>after</em> we merge to master <em>and</em> the tests pass. We’ll add the actual value of this variable as an environment variable on CircleCI:</p>
  245. <ol>
  246. <li>Open up the <em>Project Settings</em>, and click <em>Environment variables</em>.</li>
  247. <li>Add a new variable with the name “DEPLOY” and paste the example curl command (that you copied) from Docker Hub as the value.</li>
  248. </ol>
  249. <p>Now let’s test.</p>
  250. <figure class="code"><figcaption><span/></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
  251. <span class="line-number">2</span>
  252. <span class="line-number">3</span>
  253. </pre></td><td class="code"><pre><code class="sh"><span class="line"><span class="nv">$ </span>git add circle.yml
  254. </span><span class="line"><span class="nv">$ </span>git commit -m <span class="s2">"circle-test"</span>
  255. </span><span class="line"><span class="nv">$ </span>git push origin circle-test
  256. </span></code></pre></td></tr></table></div></figure>
  257. <p>Open a new pull request, and then once the tests pass on Circle CI, merge to master. Another build is trigged. Then once the tests pass again, a new build will be triggered on Docker Hub via the curl command. Nice.</p>
  258. <blockquote><p>Remember how I said that I configured Docker Hub to pull updated code to create a new image? Well, you could also set it up to where you can push images directly to Docker Hub. So once these test pass, you could simply push the image to update Docker Hub and then deploy to staging or production, directly from CircleCI. Since I have it set up differently, I handle the push to production from Docker Hub, not CircleCI. There’s positives and negatives to both approaches, as you will soon find out.</p></blockquote>
  259. <h2>Conclusion</h2>
  260. <p>So, we went over a nice development workflow that included setting up a local environment coupled with continuous integration via <a href="https://circleci.com/">CircleCI</a> (steps 1 through 6):</p>
  261. <ol>
  262. <li>Code locally on a feature branch</li>
  263. <li>Open a pull request on Github against the master branch</li>
  264. <li>Run automated tests against the Docker container</li>
  265. <li>If the tests pass, manually merge the pull request into master</li>
  266. <li>Once merged, the automated tests run again</li>
  267. <li>If the second round of tests pass, a build is created on Docker Hub</li>
  268. <li>Once the build is created, it’s then automatically (err, automagically) deployed to production</li>
  269. </ol>
  270. <p>What about the final piece – delivering this app to the production environment (step 7)? You can actually follow another one of my Docker blog <a href="https://blog.rainforestqa.com/2015-01-15-docker-in-action-from-deployment-to-delivery-part-3-continuous-delivery">posts</a> to extend this workflow to include delivery.</p>
  271. <p>Comment below if you have questions. Grab the final code <a href="https://github.com/realpython/fitter-happier-docker/releases/tag/final-code">here</a>. Cheers!</p>
  272. <hr/>
  273. <p><strong>If you have a workflow of your own, please let us know. I am currently experimenting with <a href="https://github.com/saltstack/salt">Salt</a> as well as <a href="https://www.tutum.co/">Tutum</a> to better handle orchestration and delivery on Digital Ocean and Linode.</strong></p>