<?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>DEV Community: Nduka NZEKA</title>
    <description>The latest articles on DEV Community by Nduka NZEKA (@gnzeka).</description>
    <link>https://dev.to/gnzeka</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4263551%2F9e11b9ce-ffb8-4356-baad-3740c40cb32c.JPG</url>
      <title>DEV Community: Nduka NZEKA</title>
      <link>https://dev.to/gnzeka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gnzeka"/>
    <language>en</language>
    <item>
      <title>Writing a Dockerfile: five instructions, explained one by one</title>
      <dc:creator>Nduka NZEKA</dc:creator>
      <pubDate>Wed, 25 Nov 2026 21:23:30 +0000</pubDate>
      <link>https://dev.to/ilariadigitalschool/writing-a-dockerfile-five-instructions-explained-one-by-one-3kgc</link>
      <guid>https://dev.to/ilariadigitalschool/writing-a-dockerfile-five-instructions-explained-one-by-one-3kgc</guid>
      <description>&lt;p&gt;To run your Python app with Docker, you first need a Dockerfile. It is the recipe for your image: a text file, named &lt;code&gt;Dockerfile&lt;/code&gt;, that lists the steps to build it.&lt;/p&gt;

&lt;p&gt;We made a 28-second video that goes through the five instructions you need:&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body flex items-center justify-between"&gt;
        &lt;a href="https://www.tiktok.com/@ilariaschoolen/video/7708724969462697249" rel="noopener noreferrer" class="c-link fw-bold flex items-center"&gt;
          &lt;span class="mr-2"&gt;tiktok.com&lt;/span&gt;
          

        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;You can also watch it on our site: &lt;a href="https://www.ilaria-academy.com/en/videos/writing-a-dockerfile-5b83" rel="noopener noreferrer"&gt;Writing a Dockerfile&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The rest of this post goes through each instruction of the video, with the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The app
&lt;/h2&gt;

&lt;p&gt;A folder with two files: the code and the list of its dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my-app/
├── app.py
└── requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The whole Dockerfile
&lt;/h2&gt;

&lt;p&gt;Here is the file from the video. It goes at the root of the folder:&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;FROM&lt;/span&gt; ubuntu:22.04
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /app
&lt;span class="k"&gt;COPY&lt;/span&gt; . .
&lt;span class="k"&gt;RUN&lt;/span&gt; apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get install -y python3 python3-pip \
    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; pip3 install -r requirements.txt
&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"python3"&lt;/span&gt;, &lt;span class="s2"&gt;"app.py"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Five instructions, read from top to bottom.&lt;/p&gt;

&lt;h2&gt;
  
  
  FROM picks the starting image
&lt;/h2&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; ubuntu:22.04
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An image almost always starts from an existing one. Here, Ubuntu 22.04, pulled from Docker Hub. It contains a minimal Ubuntu system and nothing else: no Python, no code of yours.&lt;/p&gt;

&lt;p&gt;The part after the colon is the tag, which is the version. Always write one. Without it, Docker takes &lt;code&gt;latest&lt;/code&gt;, and your build may change the day a new version comes out.&lt;/p&gt;

&lt;h2&gt;
  
  
  WORKDIR sets the working folder
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The instructions that follow (&lt;code&gt;COPY&lt;/code&gt;, &lt;code&gt;RUN&lt;/code&gt;, &lt;code&gt;CMD&lt;/code&gt;) work from &lt;code&gt;/app&lt;/code&gt;, inside the image. If the folder does not exist, Docker creates it. It is the &lt;code&gt;cd&lt;/code&gt; of the Dockerfile, and it stays in effect when the container starts.&lt;/p&gt;

&lt;h2&gt;
  
  
  COPY copies your code into it
&lt;/h2&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two paths: the first one is on your machine, the second one is in the image. The first dot is the build context, the folder you give to &lt;code&gt;docker build&lt;/code&gt;. The second one is the working folder, so &lt;code&gt;/app&lt;/code&gt;. After this line, &lt;code&gt;app.py&lt;/code&gt; and &lt;code&gt;requirements.txt&lt;/code&gt; are in the image, along with everything else in the folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  RUN installs your dependencies
&lt;/h2&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; apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get install -y python3 python3-pip \
    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; pip3 install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;RUN&lt;/code&gt; executes a command while the image is being built, and keeps the result in the image. Here it installs Python, which the Ubuntu image does not have, then the packages listed in &lt;code&gt;requirements.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-y&lt;/code&gt; matters: nobody is there to answer "yes" during a build.&lt;/p&gt;

&lt;h2&gt;
  
  
  CMD tells Docker which command to run at startup
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"python3"&lt;/span&gt;, &lt;span class="s2"&gt;"app.py"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;CMD&lt;/code&gt; does nothing during the build. It records the default command, the one that runs when a container starts from the image. You can replace it for one run by adding a command after the image name: &lt;code&gt;docker run my-app python3 other.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The form with brackets starts the program directly, without a shell in between, so it receives the stop signal when you stop the container.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;RUN&lt;/code&gt; and &lt;code&gt;CMD&lt;/code&gt; are easy to mix up:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;When&lt;/th&gt;
&lt;th&gt;How many&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;RUN&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;While the image is built&lt;/td&gt;
&lt;td&gt;As many as you need&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CMD&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;When a container starts&lt;/td&gt;
&lt;td&gt;One: if you write several, only the last one counts&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Every instruction adds a layer
&lt;/h2&gt;

&lt;p&gt;An image is not one block. Each instruction adds a step on top of the previous ones, and Docker keeps the result of each step in its build cache. You can see them, once the image is built:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;history&lt;/span&gt; my-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To be precise, the instructions that change files (&lt;code&gt;COPY&lt;/code&gt;, &lt;code&gt;RUN&lt;/code&gt;) add a layer of files, and that is where the size of the image comes from. &lt;code&gt;CMD&lt;/code&gt; only records a setting: it shows up in the history with a size of 0B.&lt;/p&gt;

&lt;p&gt;This is why the order of the lines matters. When you build again, Docker reuses the cache until it reaches a line that changed, or a &lt;code&gt;COPY&lt;/code&gt; whose files changed. From that line on, everything is rebuilt.&lt;/p&gt;

&lt;p&gt;In the file above, &lt;code&gt;COPY . .&lt;/code&gt; comes before &lt;code&gt;RUN&lt;/code&gt;. Change one character in &lt;code&gt;app.py&lt;/code&gt; and Docker installs Python and all the packages again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Going one step further
&lt;/h2&gt;

&lt;p&gt;The file above is the simplest one to read, not the one to ship. Once the five instructions are clear, two changes make the image faster to build and much lighter:&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;FROM&lt;/span&gt; python:3.13-slim
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /app
&lt;span class="k"&gt;COPY&lt;/span&gt; requirements.txt .
&lt;span class="k"&gt;RUN&lt;/span&gt; pip install --no-cache-dir -r requirements.txt
&lt;span class="k"&gt;COPY&lt;/span&gt; . .
&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"python"&lt;/span&gt;, &lt;span class="s2"&gt;"app.py"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Start from an image that already has Python.&lt;/strong&gt; &lt;code&gt;python:3.13-slim&lt;/code&gt; saves the &lt;code&gt;apt-get&lt;/code&gt; step, and the image is much smaller than the one built on Ubuntu. &lt;code&gt;--no-cache-dir&lt;/code&gt; keeps pip's download cache out of it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Copy &lt;code&gt;requirements.txt&lt;/code&gt; first, the code after.&lt;/strong&gt; The packages are reinstalled only when the list of dependencies changes. A change in &lt;code&gt;app.py&lt;/code&gt; rebuilds the last &lt;code&gt;COPY&lt;/code&gt; alone.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;COPY . .&lt;/code&gt; takes everything in the folder, including a &lt;code&gt;.env&lt;/code&gt; file with your passwords if there is one. Add a &lt;code&gt;.dockerignore&lt;/code&gt; file next to the Dockerfile to leave out what has no place in the image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.git
.env
__pycache__
venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is more to a production image, starting with not running the app as root. That is for another post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next
&lt;/h2&gt;

&lt;p&gt;The Dockerfile is only the recipe. Building the image and running a container takes two commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build -t my-app .
docker run my-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is part 2 of the series: &lt;a href="https://www.ilaria-academy.com/en/videos/building-and-running-a-container-182f" rel="noopener noreferrer"&gt;Building and running a container&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;This video is part of a short series on Docker by &lt;a href="https://www.ilaria-academy.com/en/" rel="noopener noreferrer"&gt;Ilaria Digital School&lt;/a&gt;, an online tech school with live, instructor-led classes. Docker is taught in our &lt;a href="https://www.ilaria-academy.com/en/course/full-stack-software-developer-pathway" rel="noopener noreferrer"&gt;Full-Stack Software Developer Pathway&lt;/a&gt;.&lt;/p&gt;

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