<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nickolena</title>
    <description>The latest articles on Forem by Nickolena (@ndfishe).</description>
    <link>https://forem.com/ndfishe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F19886%2F595a93ad-1cd9-4141-83d5-d670c7d17094.png</url>
      <title>Forem: Nickolena</title>
      <link>https://forem.com/ndfishe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ndfishe"/>
    <language>en</language>
    <item>
      <title>My favorite way to write a Dockerfile for a Python app</title>
      <dc:creator>Nickolena</dc:creator>
      <pubDate>Wed, 24 Feb 2021 16:54:48 +0000</pubDate>
      <link>https://forem.com/ndfishe/my-favorite-way-to-write-a-dockerfile-for-a-python-app-260i</link>
      <guid>https://forem.com/ndfishe/my-favorite-way-to-write-a-dockerfile-for-a-python-app-260i</guid>
      <description>&lt;p&gt;There's more than one way to skin a cat, but this pattern makes my heart flutter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Containerize a Python app
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;asgi.py&lt;/code&gt; in our app directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Dockerfile
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; python:3.7-buster as base&lt;/span&gt;

&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; SHELL=/bin/bash \&lt;/span&gt;
    USER=python \
    UID=10001

&lt;span class="c"&gt;# Create a user for the app to be owned by&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-eux&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; adduser &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--disabled-password&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--gecos&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--home&lt;/span&gt; &lt;span class="s2"&gt;"/var/lib/python3"&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--shell&lt;/span&gt; &lt;span class="s2"&gt;"/sbin/nologin"&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--no-create-home&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--uid&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;UID&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;USER&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# Manually create the users home directory&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"/var/lib/python3"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;chown&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;USER&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; /var/lib/python3

&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; base as build&lt;/span&gt;

&lt;span class="c"&gt;# Install the app requirements in its own layer.  &lt;/span&gt;
&lt;span class="c"&gt;# Isolating the "build" stage from the application stage removes&lt;/span&gt;
&lt;span class="c"&gt;# some of the unwanted cruft that comes with install &lt;/span&gt;
&lt;span class="c"&gt;# python dependencies&lt;/span&gt;
&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="s"&gt; ${USER}&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;      &lt;span class="nt"&gt;--user&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;      &lt;span class="s1"&gt;'uvicorn[standard]'&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;      &lt;span class="s1"&gt;'fastapi'&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;      &lt;span class="s1"&gt;'wheel'&lt;/span&gt;

&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; base&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=build --chown=${USER} /var/lib/python3/.local /var/lib/python3/.local&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; PATH=$PATH:/var/lib/python3/.local/bin&lt;/span&gt;

&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="s"&gt; root &lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["docker-entrypoint.sh"]&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; docker-entrypoint.sh /usr/bin/docker-entrypoint.sh &lt;/span&gt;

&lt;span class="c"&gt;# Create a designated location for the app as the python user&lt;/span&gt;
&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="s"&gt; ${USER}&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /usr/lib/python&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; ./app ./app&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["app.asgi:app"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Entrypoint
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash -e&lt;/span&gt;

&lt;span class="c"&gt;# If the CMD has not changed process it as a pure Python implementation&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;1&lt;/span&gt;&lt;span class="p"&gt;#-&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;1&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;command&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;1&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt; uvicorn &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--host&lt;/span&gt; 0.0.0.0 &lt;span class="nt"&gt;--http&lt;/span&gt; h11 &lt;span class="nt"&gt;--loop&lt;/span&gt; asyncio
&lt;span class="k"&gt;fi

&lt;/span&gt;&lt;span class="nb"&gt;echo 
echo&lt;/span&gt; &lt;span class="s2"&gt;"Running &lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo

exec&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Breaking down the stages of the Dockerfile
&lt;/h2&gt;

&lt;p&gt;This Dockerfile is separated in a few stages to organized by the function of its build step.  The multi-stage build strips our application of unnecessary files.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;base&lt;/code&gt; stage
&lt;/h3&gt;

&lt;p&gt;The first stage is aliased as the "base".  This stages bootstraps our required instructions that the remaining layers share.  The user lets us install requirements outside of the root user.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;build&lt;/code&gt; stage
&lt;/h3&gt;

&lt;p&gt;The second stage brings in external requirements and installs them as the &lt;code&gt;python&lt;/code&gt; user.  This is useful because only the user's &lt;code&gt;.local&lt;/code&gt; directory needs to be copied in the final stage.  Copying the &lt;code&gt;.local&lt;/code&gt; directory brings with only the requirements that application requires to run.&lt;/p&gt;

&lt;h3&gt;
  
  
  The nameless application stage
&lt;/h3&gt;

&lt;p&gt;The base image of the app starts with a clean layer from the first stage. Removing the need to redeclare any instructions already provided.  &lt;/p&gt;

&lt;p&gt;The requirements can be copied to the user's &lt;code&gt;.local&lt;/code&gt; directory like they were installed by pip.  The app layer is not based on the &lt;code&gt;build&lt;/code&gt; layer to reduce any left over's brought in by &lt;code&gt;pip install&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, the app itself is copied into a directory outside of user's home.  &lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.docker.com/blog/containerized-python-development-part-1/"&gt;https://www.docker.com/blog/containerized-python-development-part-1/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/a/55757473/12429735"&gt;https://stackoverflow.com/a/55757473/12429735&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/nodejs/docker-node/blob/master/docker-entrypoint.sh"&gt;https://github.com/nodejs/docker-node/blob/master/docker-entrypoint.sh&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>docker</category>
      <category>fastapi</category>
    </item>
  </channel>
</rss>
