In partnership with

Dear friend,

You have learnt the theory of how Docker and container images work.

It’s time to apply that knowledge to write a Dockerfile.

final Dockerfile

After that, you can tell all your friends you’re a certified Docker pro.

Here are the 5 steps.

Step 1: Pick a base image

Docker images consist of layers.

6 common image layers

First, you need to pick a base layer (image).

You can build it yourself.

But it’s much more convenient to pull a prebuilt base image with everything we need.

Here’s how to do it.

Go to Dockerhub, search for Python.

Dockerhub is like PyPi but for container images.

Dockerhub

Pick one with the Python version and operating system you need.

image tags explaiend

Then, in the Dockerfile, use the FROM command to use that as a base image.

FROM Python:3.14.0-slim

Step 2: Set WORKDIR

You just added the tools to the container.

But it doesn’t have any meaningful file or code.

So, we need to copy our program into it.

To do that, first you need to select a work directory.

Let’s set it to:

WORKDIR /app

Step 3: Install the dependency

Before running your code, you need to have the dependency ready.

So, copy in your requirements.txt

Then install the dependency with the RUN command.

RUN pip install --no-cache-dir -r requirements.txt

This runs the pip install command in the container’s shell.

Step 4: Copy the rest of your source code

This is pretty straightforward to do:

COPY src ./src

Now you have all the files ready to run your code.

Step 5: Define start command

You need to set a command to start your app.

Your first intuition may be using the RUN command again:

RUN python src/main.py

But that’s not what we want to do. The run commands run during the image building process.

We want to run the command only when we are actually starting a container.

To do that, use the CMD command:

CMD ["python", "src/main.py"]

And this is the final result:

Fee from Anime Coders

How did you like today's email?

Login or Subscribe to participate

Find out why 100K+ engineers read The Code twice a week

Staying behind on tech trends can be a career killer.

But let’s face it, no one has hours to spare every week trying to stay updated.

That’s why over 100,000 engineers at companies like Google, Meta, and Apple read The Code twice a week.

Here’s why it works:

  • No fluff, just signal – Learn the most important tech news delivered in just two short emails.

  • Supercharge your skills – Get access to top research papers and resources that give you an edge in the industry.

  • See the future first – Discover what’s next before it hits the mainstream, so you can lead, not follow.

Keep Reading

No posts found