The Dockerfile — A Recipe Card
LEVEL 0
The Problem
You know you need to write instructions for building your image. But what format? What language? What syntax?
Docker needs a standardized way to understand your instructions.
LEVEL 1
The Concept — The Recipe Card Format
The Concept
Think of a recipe card. It has a specific format:
- Name at the top
- Ingredients list
- Step-by-step instructions
- Serving suggestions
Chefs understand this format because recipe cards are standardized.
A Dockerfile is Docker’s recipe card format. It has specific keywords (instructions) that Docker understands:
FROM— the base ingredientRUN— preparation stepsCOPY— adding ingredientsCMD— how to serve
Each instruction is on its own line. Docker reads top to bottom, executing each step.
LEVEL 2
The Mechanics — Dockerfile Anatomy
The Mechanics
# This is a comment
# Base image — where we start
FROM python:3.11-slim
# Set working directory
WORKDIR /app
# Copy and install dependencies first (for caching)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Set environment variable
ENV FLASK_APP=app.py
# Expose port (documentation)
EXPOSE 5000
# Default command
CMD ["flask", "run", "--host=0.0.0.0"]
The Flow:
- Start with a base image (FROM)
- Set up the environment (WORKDIR, ENV)
- Install dependencies (COPY files, RUN commands)
- Add application code (COPY)
- Define how to run it (CMD)
LEVEL 3
Key Dockerfile Rules
Each instruction creates a layer
Every FROM, RUN, COPY, etc. creates a new image layer. Understanding this helps with optimization (Module 6).
Instructions are executed in order
Top to bottom. Earlier instructions can’t reference later ones.
The file must be named Dockerfile
Capital D, no extension. Or specify with -f flag.
There’s one CMD (or ENTRYPOINT)
The last CMD wins. It defines what runs when the container starts.
LEVEL 4
Dockerfile vs Containerfile
Some tools (like Podman) use Containerfile instead of Dockerfile. The syntax is identical. Docker accepts both names.