0
0
Djangoframework~30 mins

Docker containerization in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker containerization
📖 Scenario: You are creating a simple Django web application that you want to run inside a Docker container. Docker helps package your app with all its settings and dependencies so it runs the same everywhere.
🎯 Goal: Build a Docker setup for a Django app by creating a Dockerfile, setting a port configuration, writing the command to run the app inside the container, and finally adding a docker-compose.yml file to manage the container easily.
📋 What You'll Learn
Create a Dockerfile with Python base image and Django setup
Add a port variable to expose Django's default port 8000
Write the command to run Django's development server inside the container
Create a docker-compose.yml file to run the container with port mapping
💡 Why This Matters
🌍 Real World
Docker helps developers package their Django apps with all dependencies so they run the same on any computer or server. This avoids the 'it works on my machine' problem.
💼 Career
Many companies use Docker to deploy web apps. Knowing how to containerize Django apps is a valuable skill for backend and DevOps roles.
Progress0 / 4 steps
1
Create the Dockerfile with Python base and Django install
Create a file named Dockerfile with these exact lines:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
Django
Need a hint?

Use the official Python 3.12 slim image. Set working directory to /app. Copy requirements.txt and install dependencies. Then copy the rest of the app files.

2
Add a port variable to expose Django's port
In the same Dockerfile, add a line to expose port 8000 by writing EXPOSE 8000 after the last COPY command.
Django
Need a hint?

This tells Docker which port the container will listen on. Django's default development server uses port 8000.

3
Add the command to run Django development server
In the Dockerfile, add a line at the end to run the Django server with CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"].
Django
Need a hint?

This command starts Django's development server and makes it listen on all network interfaces inside the container.

4
Create docker-compose.yml to run the container with port mapping
Create a file named docker-compose.yml with these exact lines:
version: '3.9'
services:
web:
build: .
ports:
- '8000:8000'
Django
Need a hint?

This file tells Docker Compose to build the image from the current folder and map port 8000 on your computer to port 8000 in the container.