Docker + Django in 10 Days: From Zero to Production [Day: 05]
![Docker + Django in 10 Days: From Zero to Production [Day: 05]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1770578634367%2F8708279a-574a-4250-b12b-22d01fe91841.png&w=3840&q=75)
π― Goal of Day 5
By the end of today, you will:
Understand why Docker Compose exists
Run Django + PostgreSQL together
Learn how services talk to each other
Stop rebuilding containers on every code change
Be very close to production-style setups
β Why Docker Compose?
Yesterdayβs problem:
One container is fine
Multiple containers = pain
Imagine running:
Django
PostgreSQL
Redis
Celery
Manually?
π΅βπ« No.
Docker Compose solves this:
One file β multiple services β one command
π What Is Docker Compose?
Docker Compose is a tool for defining and running multi-container applications.
You define everything in:
docker-compose.yml
Then run:
docker compose up
π§ Big Picture (Today)
Django Container β PostgreSQL Container
Important rule:
Containers talk using service names, not
localhost.
π Project Structure (Update)
Your project should now look like:
django-docker/
βββ core/
βββ manage.py
βββ requirements.txt
βββ Dockerfile
βββ docker-compose.yml
π Step 1: Update requirements.txt
Django>=4.2,<5.0
psycopg2-binary>=2.9
π³ Step 2: Update Dockerfile (Small Change)
FROM python:3.12-slim
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
(No big change β good sign π)
π§© Step 3: Create docker-compose.yml
version: "3.9"
services:
web:
build: .
container_name: django_app
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres:15
container_name: postgres_db
environment:
POSTGRES_DB: django_db
POSTGRES_USER: django_user
POSTGRES_PASSWORD: django_pass
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
postgres_data:
π§ Very Important Concepts Here
πΉ services
Each service = one container.
πΉ build: .
Uses your Dockerfile.
πΉ volumes
.:/app
Means:
Local code β container code
No rebuild needed on code change π
πΉ depends_on
Starts DB before Django.
π Step 4: Update Django Database Settings
In settings.py:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "django_db",
"USER": "django_user",
"PASSWORD": "django_pass",
"HOST": "db", # service name, NOT localhost
"PORT": "5432",
}
}
β οΈ HOST = "db" is CRITICAL.
βΆοΈ Step 5: Run Everything
Stop previous containers:
docker compose down
Start new setup:
docker compose up --build
π§ͺ Step 6: Run Migrations
Open a new terminal:
docker compose exec web python manage.py migrate
Create superuser:
docker compose exec web python manage.py createsuperuser
Visit:
http://127.0.0.1:8000/admin
π Django + PostgreSQL is LIVE in Docker π
π§ Networking Magic (Important)
Inside Docker:
localhostβ current container onlydbβ PostgreSQL container
Docker Compose creates an internal network automatically.
π Official Docs (Must Bookmark)
Docker Compose overview
https://docs.docker.com/compose/PostgreSQL image
https://hub.docker.com/_/postgresDjango PostgreSQL docs
https://docs.djangoproject.com/en/stable/ref/databases/#postgresql-notes
β οΈ Common Beginner Mistakes
Using
localhostfor DB βForgetting volumes β data loss
Forgetting to run migrations
Editing code but not using volumes
π Homework (Very Important)
Start containers with
docker compose upRun migrations
Access admin panel
Stop containers
Restart and confirm data persists
β End of Day 5 Summary
You learned Docker Compose
You ran multiple containers
Django talks to PostgreSQL correctly
You now have a real backend setup
![Docker + Django in 10 Days: From Zero to Production [Day: 07]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F696fcd472484e6d591510582%2F911a802d-c837-4cb6-b3fc-73966aff1020.png&w=3840&q=75)
![Docker + Django in 10 Days: From Zero to Production [Day: 06]](/_next/image?url=https%3A%2F%2Fcloudmate-test.s3.us-east-1.amazonaws.com%2Fuploads%2Fcovers%2F696fcd472484e6d591510582%2Fa53f784c-27e6-4611-8627-a7d76fe9875b.png&w=3840&q=75)
![Docker + Django in 10 Days: From Zero to Production [Day: 04]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1770151845620%2F77951891-5216-4440-aab8-7b848b6eba8f.png&w=3840&q=75)
![Docker + Django in 10 Days: From Zero to Production [Day: 03]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1769895181687%2Fdd60bdd1-f050-4f82-8e47-dde4a70f7798.png&w=3840&q=75)