<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by William Suzuki on Medium]]></title>
        <description><![CDATA[Stories by William Suzuki on Medium]]></description>
        <link>https://medium.com/@wllmszk?source=rss-13b3915b6f86------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZcBwi-HiGltprmOPmo1Q3Q.jpeg</url>
            <title>Stories by William Suzuki on Medium</title>
            <link>https://medium.com/@wllmszk?source=rss-13b3915b6f86------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 11:48:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wllmszk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Build a base image for all your Dockerfiles]]></title>
            <link>https://medium.com/@wllmszk/build-a-base-image-for-all-your-dockerfiles-45fb20db2dff?source=rss-13b3915b6f86------2</link>
            <guid isPermaLink="false">https://medium.com/p/45fb20db2dff</guid>
            <category><![CDATA[dockerfiles]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[William Suzuki]]></dc:creator>
            <pubDate>Fri, 14 Jul 2023 18:02:21 GMT</pubDate>
            <atom:updated>2023-07-14T18:02:21.022Z</atom:updated>
            <content:encoded><![CDATA[<p>In my tests, I have to use different Dockerfiles for different types of tests and most of those Dockerfiles have the same beginning. Usually installing something using apt-get install ...</p><figure><img alt="# Original Dockerfile # src/Dockerfile FROM ubuntu RUN apt-get update &amp;&amp; \ apt-get install -y &amp;&amp; \ python3 &amp;&amp; \ python3-pip &amp;&amp; \ libcurl4-openssl-dev &amp;&amp; \ libssl-dev &amp;&amp; \ build-essential &amp;&amp; \ libzip-dev &amp;&amp; \ libzip4 &amp;&amp; \ sqlite3 COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD python3 app.py" src="https://cdn-images-1.medium.com/max/1024/1*bSQzoaLpsQLSnc6dD2hyig.png" /><figcaption>Original Dockerfile, before using a base image for modularity</figcaption></figure><p>(I think it is best to just use a Python image that contains the version that you’ll need, instead of building the image manually as I did here. Take a look at all the<a href="https://hub.docker.com/_/python">Python images</a> available.)</p><p>Using a separate Dockerfile for the base image and reusing it across different projects or Dockerfiles can be super handy. While Docker does cache commands that haven’t changed, this technique still brings some perks, especially when starting from scratch on a new machine or using container registries like AWS ECR or DockerHub.</p><p>By creating a special base image with all your common dependencies, configurations, and settings, you keep things modular and DRY (Don’t Repeat Yourself). You don’t have to scatter the same stuff everywhere, just define it once in the base image and reuse it effortlessly wherever you need it.</p><p>Not only does this approach make your code more organized and modular, but it can also save you time when spinning up containers on fresh machines or using container registries. Instead of building the base image from scratch every time, you can take advantage of the pre-built one, cutting down on the overall build time.</p><p>Building a separate Dockerfile for the base image and using it across your projects is a smart move. It keeps things tidy, promotes code reuse, and can save you time when setting up containers from scratch or using container registries.</p><h3>How to do it?</h3><p>Create the Dockerfile.base file.</p><figure><img alt="# src/Dockerfile.base
 FROM ubuntu
 RUN apt-get update &amp;&amp; \
 apt-get install -y &amp;&amp; \
 upgrade &amp;&amp; \
 python3 &amp;&amp; \
 python3-pip &amp;&amp; \
 libcurl4-openssl-dev &amp;&amp; \
 libssl-dev &amp;&amp; \
 build-essential &amp;&amp; \
 libzip-dev &amp;&amp; \
 libzip4 &amp;&amp; \
 sqlite3
 
 COPY requirements.txt .
 RUN pip install -r requirements.txt" src="https://cdn-images-1.medium.com/max/1024/1*i0oRaXUWGxgcbGJBZQ1wXw.png" /></figure><p>All the other Dockerfiles will use this base image for building something on top.</p><p>If you think the requirements.txtwill change frequently in the future you have two options:</p><ol><li>Keep it in the base image. In this case, you’ll need to remember to build this image every time therequirements.txtchange.*</li><li>Move lines 15 and 16: <br>COPY requirements.txt .<br>RUN pip install -r requirements.txt<br>to every downstream Dockerfile. In this case, if the requirements.txt changes all builds will need to install the packages from scratch. Which can take some time if you have lots of packages and lots of other downstream Dockerimages.</li></ol><p>*This will not be really a problem if you use something like a Makefile. And bundle the build base image and derived image in the same command.</p><p>To build the image we can run:</p><figure><img alt="docker build -t base-python-image -f Dockerfile.base ." src="https://cdn-images-1.medium.com/max/1024/1*gmBkHyBSgUfKOIOiZ7efIw.png" /></figure><p>Instead of base-python-imageyou should use the name of the base image that fits in your project.</p><figure><img alt="# src/Dockerfile
 FROM base-python-image
 
 COPY . .
 CMD python3 app.py" src="https://cdn-images-1.medium.com/max/736/1*7SmqEscS-SWcyorcBMDs6A.png" /><figcaption>Dockerfile after using a base image for modularity</figcaption></figure><p>And one way that you can run your container is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CuPdiRdJl58p6oVLzlMkeA.png" /></figure><p>The -twill make the output by colourful. And --rm will delete the container when the tests finish running.</p><h3>Conclusion</h3><p>Applying modularity in Dockerfiles is a straightforward process that can be beneficial across different programming languages and frameworks. By identifying components that change frequently and those that are commonly used, you can enhance the reusability and maintainability of your Dockerfiles.</p><p>It is recommended to include common components in the base image, as this promotes consistency and avoids duplication across multiple Dockerfiles. Additionally, considering including frequently changing components that are widely utilized in the base image can help simplify the overall build process.</p><p>To ensure the base image is consistently built and to leverage caching effectively, using a Makefile is a useful approach. By incorporating a Makefile, you can automate the build process and eliminate the risk of forgetting to build the base image. Moreover, caching mechanisms can be leveraged to prevent redundant rebuilding of the base image, thereby optimizing the build time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45fb20db2dff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>