Skip to main content

Command Palette

Search for a command to run...

Docker + Django in 10 Days: From Zero to Production [Day: 06]

Updated
β€’3 min read
Docker + Django in 10 Days: From Zero to Production [Day: 06]

🎯 Goal of Day 6

By the end of today, you will:

  • Understand what environment variables are

  • Use a .env file with Docker Compose

  • Clean up Django settings.py

  • Understand volumes deeply

  • Know how Docker keeps your database data safe


❓ Why Environment Variables Matter

Right now, you have this in settings.py:

PASSWORD = "django_pass"

That’s bad because:

  • Secrets leak to GitHub

  • Hard to change per environment

  • Unprofessional ❌

Industry rule:

❝ Code is public. Secrets are not. ❞


🧠 What Are Environment Variables?

Environment variables are key-value pairs available to a running process.

Example:

DB_NAME=django_db
DB_PASSWORD=super_secret

Docker passes these safely into containers.


πŸ“ Step 1: Create .env File

Create a file named .env (same level as docker-compose.yml):

DEBUG=1

POSTGRES_DB=django_db
POSTGRES_USER=django_user
POSTGRES_PASSWORD=django_pass
POSTGRES_HOST=db
POSTGRES_PORT=5432

⚠️ Never commit .env to GitHub.

Add to .gitignore:

.env

🐳 Step 2: Update docker-compose.yml

Replace hard-coded values:

services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    env_file:
      - .env
    depends_on:
      - db

  db:
    image: postgres:15
    env_file:
      - .env
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Now:

  • Docker reads .env

  • Injects variables automatically


🧠 Important: How .env Works in Compose

  • .env β†’ loaded by Docker Compose

  • Variables available inside containers

  • Same file works for:

    • Django

    • PostgreSQL

Very powerful.


🐍 Step 3: Update Django settings.py

Use os.environ:

import os

DEBUG = os.environ.get("DEBUG") == "1"

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": os.environ.get("POSTGRES_DB"),
        "USER": os.environ.get("POSTGRES_USER"),
        "PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
        "HOST": os.environ.get("POSTGRES_HOST"),
        "PORT": os.environ.get("POSTGRES_PORT"),
    }
}

Now your settings are:

  • Clean

  • Secure

  • Environment-friendly


🧠 What Are Volumes? (Deep Understanding)

You already used:

volumes:
  - postgres_data:/var/lib/postgresql/data

What this means:

  • Database files stored outside container

  • Container can be deleted

  • Data survives

πŸ“Œ Without volumes:

Stop container β†’ data gone 😱


πŸ§ͺ Test Data Persistence (Do This)

  1. Start containers:

    docker compose up
    
  2. Create a superuser

  3. Stop everything:

    docker compose down
    
  4. Start again:

    docker compose up
    
  5. Login again

βœ… Data is still there β†’ volumes working.


🧠 Bind Mount vs Volume (Important Difference)

Bind Mount

Volume

.:/app

postgres_data

Local ↔ container

Managed by Docker

Code sync

Persistent data

Dev use

DB use


πŸ“š Official Docs (Read Today)


⚠️ Common Beginner Mistakes

  • Committing .env ❌

  • Using print(os.environ) (leaks secrets)

  • Forgetting env vars exist only at runtime

  • Mixing dev & prod secrets


πŸ“ Homework (Important)

  1. Move all secrets to .env

  2. Confirm Django reads from env

  3. Restart containers

  4. Test data persistence

  5. Explain (to yourself) why volumes exist


βœ… End of Day 6 Summary

  • You learned environment variables

  • You secured your Django project

  • You understand volumes clearly

  • Your setup looks professional now