<?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: Oleh Korkh</title>
    <description>The latest articles on Forem by Oleh Korkh (@korkholeh).</description>
    <link>https://forem.com/korkholeh</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%2F147440%2Fb5cd46d1-052e-4c04-b0b4-958f24756020.jpg</url>
      <title>Forem: Oleh Korkh</title>
      <link>https://forem.com/korkholeh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/korkholeh"/>
    <language>en</language>
    <item>
      <title>Using pip-tools for local development with Docker</title>
      <dc:creator>Oleh Korkh</dc:creator>
      <pubDate>Wed, 17 Mar 2021 16:40:51 +0000</pubDate>
      <link>https://forem.com/korkholeh/using-pip-tools-for-local-development-with-docker-4n35</link>
      <guid>https://forem.com/korkholeh/using-pip-tools-for-local-development-with-docker-4n35</guid>
      <description>&lt;p&gt;Docker is a great tool even for local development. At the same time, managing Python requirements in containers can be tricky. The pretty standard behavior is to use &lt;code&gt;requirements.txt&lt;/code&gt; file that adds to the image during the building process. Usually, we are using something like that in the &lt;code&gt;Dockerfile&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;...
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; ./requirements /requirements.txt&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-U&lt;/span&gt; pip &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--no-cache-dir&lt;/span&gt;  &lt;span class="nt"&gt;-r&lt;/span&gt; /requirements.txt &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /requirements.txt
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These instructions work fine for production deployment but not very useful for the development stage. Periodically you need to install a new library. With using a virtual environment, without Docker, we could use &lt;code&gt;pip install ...&lt;/code&gt; and then &lt;code&gt;pip freeze &amp;gt; requirements.txt&lt;/code&gt; for storing the installed libraries' information to the file. But with Docker, it is not a case because we need to install libraries on the build stage. At the same time, it is hard to change &lt;code&gt;requirements.txt&lt;/code&gt; manually without breaking dependencies.&lt;/p&gt;

&lt;p&gt;There are available Python dependency managers, like &lt;code&gt;Pipenv&lt;/code&gt; or &lt;code&gt;Poetry&lt;/code&gt;.  But I prefer to use &lt;code&gt;pip-tools&lt;/code&gt; because it is more straightforward. We can add it to the container image by improving the following line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-U&lt;/span&gt; pip pip-tools &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--no-cache-dir&lt;/span&gt;  &lt;span class="nt"&gt;-r&lt;/span&gt; /requirements.txt &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The use case of the tool is simple. First of all, we prepare a file with the name &lt;code&gt;requirements.in&lt;/code&gt; that will contain a list of dependencies. Each line in this file is a dependency. We can also use the same rules for managing versions like the regular &lt;code&gt;pip install&lt;/code&gt; command. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;django==3.1.*
celery
pillow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now use the command &lt;code&gt;pip-compile requirements.in&lt;/code&gt; for generating &lt;code&gt;requirements.txt&lt;/code&gt; with all necessary dependencies. With &lt;code&gt;pip-tools&lt;/code&gt; installed in the container, we can use the command like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-rm&lt;/span&gt; ... pip-compile requirements.in
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the file &lt;code&gt;requirements.txt&lt;/code&gt; will be generated, including all required dependencies of described libraries. So, it is possible to rebuild the container for installing new libraries.&lt;/p&gt;

&lt;p&gt;You can check the &lt;code&gt;pip-tools&lt;/code&gt; &lt;a href="https://github.com/jazzband/pip-tools"&gt;documentation&lt;/a&gt; for available commands and options. For example, how to upgrade a particular library. It is a pretty simple yet powerful tool.&lt;/p&gt;

</description>
      <category>python</category>
      <category>docker</category>
      <category>pip</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
