Skip to content
EgyKodeAcademy
02 · The application, in containersLab 9 / 59
Guided labdocker

Production-Grade Multi-Stage Dockerfile for Django & Gunicorn

Turn a Django application into an image that starts fast, runs as a non-root user, and carries no build tooling.

Time
31 min
Level
Beginner
Objectives
4 objectives
Cost
Free

Where this fits in the platform

The scenario#

The image is 950 MB, ships gcc to production, and runs as root. It also takes four minutes to rebuild after a one-line code change, because every build reinstalls every dependency.

All three problems have the same fix, and it is the order of the instructions rather than any single one of them.

Hands-on environment

Run this lab in a real terminal, free and in your browser. The environment is temporary and yours alone — break it as much as you like.

Open the terminal

Opens in Killercoda, in a new tab — keep this page open for the steps.

Run it on your own machine

Run this lab on your own machine. One command starts the environment, with everything the lab needs already installed:

You will need:

  • docker
git clone https://github.com/EgyKode/EgyKode-lab.git
cd EgyKode-lab
./egykode start
./egykode shell

You need Docker and Git installed. Everything else runs inside the environment. The first start downloads it and takes a few minutes; later starts are seconds.

Not sure what you already have? Run: npm run doctor — it checks and changes nothing.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

The Dockerfile

Step 1 of 4

What you are building#

A multi-stage build uses several FROM instructions in one Dockerfile. Only the last stage becomes the image; the earlier ones are scratch space that is discarded.

text
  STAGE 1 — builder                    STAGE 2 — runtime
  python:3.13-slim                     python:3.13-slim
  + gcc, make, libpq-dev               copies ONLY the compiled wheels
  + compiles wheels        ══════▶     no compiler, no headers
  ~950 MB, thrown away                 runs as UID 10001
                                       ~160 MB, shipped

Three things this buys you, and the second is the one that matters most:

  • Size. ~950 MB to ~160 MB, so pulls are faster on every node.
  • Attack surface. An attacker who reaches a container with gcc in it can compile an exploit there. One without cannot.
  • Honesty. If the runtime stage is missing a library, the build fails on your machine rather than the container failing in production.

Build it#

Verify it worked#

Terminal
# Non-root, with the UID you chose
docker run --rm app:1.0 id
# uid=10001(appuser) gid=10001(appuser)
 
# No compiler in the shipped image
docker run --rm app:1.0 sh -c 'which gcc || echo "no compiler — correct"'
 
# Under 200 MB
docker images app:1.0 --format '{{.Size}}'
 
# The health check is wired up
docker run -d --name app -p 8000:8000 app:1.0
docker inspect --format '{{.State.Health.Status}}' app

Each of those checks an outcome. docker images showing the tag exists would prove only that a build ran.


Clean up#

Terminal
docker rm -f app
docker rmi app:1.0 app:1.1
docker builder prune -f

Cost of this lab: Free — everything runs locally.

Maintained by others, on Killercoda. Useful for extra repetition on one tool — it does not complete this lab or settle any criterion above.

Success criteria

0 of 4

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 9 of 59 on the project path

Docker Networking, Volumes & Health ChecksMake four containers find each other, keep data across a restart, and start in an order that actually works.Why next: A small, non-root container image of the application50 minBeginner

Previous: Professional Collaboration on GitHub