Skip to main content

Command Palette

Search for a command to run...

Episode 3: The Secret Scroll (The Dockerfile)

Updated
4 min read
F
Learner by morning, philosophical writer by night. I’m a CS grad from a remote mountain village, now navigating the digital peaks of Docker, Python, and FastAPI. My process is simple: I break things, I build them back, and I turn the struggle into a story. I’m a self-learner who believes the best way to master a concept is to teach it. If you want to see how the chaos of software turns into a narrative, you’re in the right place.

Jack was standing in front of his empty "Apartment Building." He knew about Namespaces and Cgroups, but he was frustrated. He had his App code sitting on his laptop, but he couldn't just "throw" the files into the building.

He needed a way to give Docker a precise, step-by-step Instruction Book.

"It’s like an ancient scroll," Jack realized. "If I write the instructions correctly, Docker will read it and build exactly what I need, every single time."

So, how do we write this "Instruction Book"? Jack realized that every Dockerfile is just a list of steps to turn an empty room into a working office for his code.

Here are the 5 core commands you need to know:

FROM python:3.9

FROM: The Foundation This is the very first step. Before you do anything, you need to decide what your "building" is made of. Does your app need Python? Node.js?

The Docker community is awesome they’ve already made "pre-made" environments for us. If you write FROM python:3.9, you are getting a room that already has a Linux Kernel and Python installed. You don't have to build the engine; you just start the car.

WORKDIR /code

WORKDIR: Making the Folder Your container is ready now, but it’s empty. What do you do on your local PC when you start a project? You make a folder, right?

WORKDIR /app tells Docker: "Make a folder called 'app' and stay inside it." This is the 2nd layer. From now on, everything we do happens inside this specific room.

COPY . .

COPY: Moving Day Usually, we write our code on our laptops before we ever touch Docker. Now we need to get that code from our laptop into the container’s folder.

How it works: It’s pretty simple you use COPY . .. This just means "Take everything from my current folder on my laptop and paste it into the container’s folder."

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

RUN: Installing the "Extras" Okay, we have the base Python and we have our code. But what if your code uses extra packages like fastapi or pandas? They aren't in the base room yet!

This is where we install our dependencies. You just tell Docker: RUN pip install fastapi. Now, your room has all the specific tools your app needs to breathe.

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]

CMD: The Grand Opening Everything is in the container now: the OS, the Python, your code, and your packages. Is it running yet? No. *

CMD is the final command. It’s like turning the key in the ignition. It tells the container: "Now that everything is set up, run this command to start the app!"

Why the weird --host 0.0.0.0?

Jack almost missed this! Remember our Namespaces from Episode 2?

  • If you run your app on 127.0.0.1 (localhost), the app thinks it’s only talking to itself inside its own "Secret Room."

  • By using 0.0.0.0, you are telling the app: "Listen to everyone in the building." This allows the "Apartment Building" (your laptop) to actually reach inside the room and see the app.

The Quest: Build Your First Image

Now that the scroll is written, it's time to turn it into a reality.

Your Task:

  1. Create a folder on your PC.

  2. Create a file named Dockerfile (no extension!) and paste the scroll above.

  3. Open your terminal in that folder and type: docker build -t my-first-hero .

The Challenge: When you run that command, watch your terminal closely. You will see it saying "Step 1/6", "Step 2/6"...

Why does it go one by one? (Hint: Think back to our "Sandwich Layers" from the last episode!)

Docker Chronicles: The Quest for the Universal Environment

Part 3 of 7

In a world cursed by 'It works on my machine,' one hero emerges. Join Jack and Rose as they navigate the chaotic seas of deployment, fight the VM giants, and master the art of the Great Docker. A humorous guide to mastering Docker from scratch.

Up next

Episode 4: The Time Loop (Layers & Caching)

So, Jack finally wrote his "Secret Scroll" (the Dockerfile). He changed one tiny typo in his code, hit build, and... he had to wait. And wait. Docker started from the very beginning, downloading Pytho