Docker Containerization Explained Simply (with Diagrams and Real Code)
Docker Containerization: The Essentials in One Article — Real Code, Diagrams and Concrete Steps, Excerpts from a 41-Lesson Course.
A straight-to-the-point guide: Docker Containerization broken down with diagrams, concrete examples and tested commands. All from a structured 12-chapter course — here are the best parts.
- Install an UBUNTU VM
- Discover docker
- Essential commands
- Dockerfile
- Containerize Flask
Anatomy of a Dockerfile
# Stop ALL running containers docker stop $(docker ps -a -q) # Remove ALL containers docker rm $(docker ps -a -q) # Remove ALL images (optional) docker rmi $(docker images -q) # Force removal if an image resists docker rmi -f $(docker images -q)
-f (force) option also removes images used by containers.Learning objectives
What is a Dockerfile?
A Dockerfile is a text file that contains a series of instructions to build a Docker image. It is like a recipe: each instruction is a step that transforms the base image into your custom image.
- FROM = Basic ingredients (flour, butter...)
- WORKDIR = Workspace (kitchen table)
- COPY = Add ingredients (sugar, eggs...)
- RUN = Prepare (mix, cook...)
- EXPOSE = Indicate presentation (serving dish)
- CMD = Serve the dish (final command)
Base image
The Dockerfile always starts from an existing image (FROM). We never start from scratch.
Instructions
Each line is an instruction that modifies the image: copy files, install packages, configure...
Final image
The result is a custom image ready to be launched with docker run.
Main instructions
FROM – Base image
First instruction of a Dockerfile. It defines the starting image on which we build.
FROM python:3.11-slim
slim version (lightweight version, without unnecessary tools). This gives us a Linux system with Python 3.11 already installed.WORKDIR – Working directory
Defines the working directory inside the container. All subsequent instructions (COPY, RUN, CMD) will execute in this folder.
WORKDIR /app
WORKDIR instead of RUN cd /app. WORKDIR creates the folder automatically if it does not exist and persists for all subsequent instructions.COPY – Copy files
Copies files from your machine (the "build context") into the container.
COPY . .
The first . = the current folder on your machine. The second . = the WORKDIR inside the container (/app).
Layers, cache and best practices
# Stop ALL running containers docker stop $(docker ps -a -q) # Remove ALL containers docker rm $(docker ps -a -q) # Remove ALL images (optional) docker rmi $(docker images -q) # Force removal if an image resists docker rmi -f $(docker images -q)
-f (force) option also removes images used by containers.Learning objectives
Each instruction = 1 layer
When Docker executes a Dockerfile, each instruction (FROM, RUN, COPY, etc.) creates a new layer. A Docker image is a stack of layers piled on top of each other.
FROM python:3.11-slim ← Layer 1 (base image) WORKDIR /app ← Layer 2 COPY requirements.txt . ← Layer 3 RUN pip install ... ← Layer 4 COPY . . ← Layer 5 CMD ["python", "app.py"] ← Layer 6 (metadata)
- Share common layers between multiple images (e.g.: all Python images share the same base layer)
- Cache layers to speed up builds
- Reduce disk space by storing each layer only once
The Docker cache
Docker caches each layer. During a rebuild, Docker checks if a layer has changed. If nothing has changed, it reuses the cached layer instead of rebuilding it.
Bad order vs good order
BAD: requirements after COPY . .
FROM python:3.11-slim WORKDIR /app COPY . . ← All source code RUN pip install -r requirements.txt ← Install dependencies CMD ["python", "app.py"]
app.py), the COPY . . layer changes. Because it is before RUN pip install, Docker must reinstall all dependencies on every build. On a project with many dependencies, this can take several minutes.GOOD: requirements before COPY . .
FROM python:3.11-slim WORKDIR /app COPY requirements.txt . ← Only the dependencies RUN pip install -r requirements.txt ← Installation (cached if requirements.txt has not changed) COPY . . ← Source code (changes often) CMD ["python", "app.py"]
COPY . . layer changes. Dependencies (pip install) are cached and are not reinstalled. The build time drops from several minutes to a few seconds.Bad order
Good order
Demonstration: observe the cache
Step 1: Create the project
mkdir ~/demo-cache cd ~/demo-cache
Create requirements.txt:
touch requirements.txt nano requirements.txt
Build and run your first image
.dockerignore file.# Stop ALL running containers docker stop $(docker ps -a -q) # Remove ALL containers docker rm $(docker ps -a -q) # Remove ALL images (optional) docker rmi $(docker images -q) # Force removal if an image resists docker rmi -f $(docker images -q)
-f (force) option also removes images used by containers.Learning objectives
Create the project
Step 1: Create the project folder
mkdir ~/mon-premier-docker cd ~/mon-premier-docker
Step 2: Create the Python script
Create a hello.py file:
touch hello.py nano hello.py
Paste this content, then save (Ctrl + O, Enter, Ctrl + X):
print("Hello from Docker!")echo 'print("Hello from Docker!")' > hello.pyCheck the file content:
cat hello.py
Step 3: Create the Dockerfile
Create a file named Dockerfile (no extension):
touch Dockerfile nano Dockerfile
Paste this content, then save (Ctrl + O, Enter, Ctrl + X):
FROM python:3.11-slim WORKDIR /app COPY hello.py . CMD ["python", "hello.py"]
This article covers the most useful excerpts — the complete Docker Containerization course (12 chapters, 41 lessons, corrected exercises and final project) takes you all the way.
./access-the-full-course free course: Mastering Claude CodeFAQ
How long does it take to learn Docker Containerization?
Are there any prerequisites?
Where to start concretely?
📬 Want to receive this type of guide every week? Subscribe for free — real code, zero fluff.