Writing a Django Dockerfile
Build a slim, layered image for your app.
Writing a Django Dockerfile is a free Django Academy lesson on CoddyKit — lesson 1 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 Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why a Dockerfile
A Dockerfile is a recipe that builds your app into a portable image, so it runs the same on your laptop and any server. 🐳
Pick a Base Image
Start every Dockerfile with FROM, choosing a slim Python base so your image stays small and fast to ship.
FROM python:3.12-slimSet the Work Directory
Use WORKDIR to set the folder where your code lives, so later commands run from a clean, predictable place.
WORKDIR /appTame Python Output
Two ENV flags help in containers: unbuffered logs and no .pyc files, keeping output instant and images tidy.
ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1Copy Requirements First
Copy requirements.txt before your code. Docker caches this layer, so installs are skipped when only your code changes.
COPY requirements.txt .Install Dependencies
Run pip install with --no-cache-dir so the package cache never bloats your final image.
RUN pip install --no-cache-dir -r requirements.txtCopy Your Project
Now COPY the rest of your project in. Doing this after the install keeps the cached dependency layer reusable.
COPY . .Expose the Port
Use EXPOSE to document the port your app listens on. It is a hint to readers and tools, not a firewall.
EXPOSE 8000Define the Start Command
End with CMD, the default command that launches your app when the container starts.
CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]Add a .dockerignore
A .dockerignore keeps junk like venvs and .git out of the build context, making builds faster and images leaner.
.git
__pycache__
.venv
*.sqlite3Build Your Image
Run docker build with a tag to turn your Dockerfile into a reusable, named image you can run anywhere.
docker build -t myapp:latest .Quick Check
Think about Docker's layer cache and build order.
Recap
You built a Django Dockerfile: pick a slim base, set env, install deps before code for caching, then define CMD. One recipe, runs anywhere. 🎉
Frequently asked questions
Is the “Writing a Django Dockerfile” lesson free?
Yes — the full text of “Writing a Django Dockerfile” is free to read here on the web, and the Django 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 Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Writing a Django Dockerfile”?
Build a slim, layered image for your app. You practise Django 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 Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing a Django 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 Django Academy lesson?
Yes. Every Django 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.