0Pricing
Flask Academy · Lesson

Write a Production Dockerfile

Build a lean, reproducible container image.

Write a Production Dockerfile is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Containerize

A Docker image packs your code, Python, and every dependency into one unit that runs the same on any machine. No more works on my laptop. 🐳

The Dockerfile

A Dockerfile is a recipe. Each line is a step Docker runs to build your image, from base layer to final run command.

Pick a Slim Base

Start from an official python image. The slim variant keeps the image small while still giving you a working Python runtime.

FROM python:3.12-slim

Set a Working Directory

Use WORKDIR to choose where your code lives inside the image. Every later command runs relative to this folder.

WORKDIR /app

Copy Requirements First

Copy requirements.txt before your code. This lets Docker cache the install layer and skip it when only your code changes.

COPY requirements.txt .

Install Dependencies

Run pip install with no cache to keep the layer lean. This bakes Flask and Gunicorn right into the image.

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

Copy the Rest of the Code

Now COPY the rest of your project in. Because it comes after the install, edits to your code do not bust the dependency cache.

COPY . .

Document the Port

Use EXPOSE to declare which port your app listens on. It is documentation plus a hint for tooling that wires things up.

EXPOSE 8000

Define the Start Command

The CMD line is what runs when the container starts. Point it at Gunicorn so the container boots straight into your server.

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"]

Ignore the Junk

Add a .dockerignore file so caches, venvs, and git data never enter the image. Smaller builds are faster and safer.

__pycache__/
.venv/
.git/

Build and Run It

Build with docker build, then run the image and map a port. Your packaged Flask app is now portable to any host.

docker build -t myapp .
docker run -p 8000:8000 myapp

Quick Check

Why copy requirements.txt before the rest of your code?

Recap

Your Dockerfile builds from a slim base, installs deps, copies code, and launches Gunicorn. One image runs anywhere. Next: put Nginx in front. 🔧

Frequently asked questions

Is the “Write a Production Dockerfile” lesson free?

Yes — the full text of “Write a Production Dockerfile” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Write a Production Dockerfile”?

Build a lean, reproducible container image. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Write a Production Dockerfile” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Not the Built-in Dev Server
  2. Run Flask Under Gunicorn
  3. Write a Production Dockerfile
  4. Front It with Nginx and Compose
← Back to Flask Academy