Challenge - 5 Problems
Docker Django Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when you run this Dockerfile for a Django app?
Consider this Dockerfile for a Django project. What will be the output when you build and run the container?
FROM python:3.12-slim WORKDIR /app COPY requirements.txt ./ RUN pip install -r requirements.txt COPY . . CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Django
FROM python:3.12-slim WORKDIR /app COPY requirements.txt ./ RUN pip install -r requirements.txt COPY . . CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Attempts:
2 left
💡 Hint
Think about what the CMD command does and how the server binds to network interfaces.
✗ Incorrect
The CMD runs the Django development server binding to 0.0.0.0:8000, which means it listens on all network interfaces inside the container, making it accessible if the port is published.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Docker Compose service for Django
Look at this docker-compose.yml snippet for a Django service. Which option correctly identifies the syntax error?
version: '3.9'
services:
web:
build: .
ports:
- "8000:8000"
volumes:
- .:/app
command: python manage.py runserver 0.0.0.0:8000
environment:
- DEBUG=True
- SECRET_KEY='mysecret'
depends_on:
- db
db:
image: postgres:15
environment:
- POSTGRES_DB: mydb
- POSTGRES_USER: user
- POSTGRES_PASSWORD: passDjango
version: '3.9' services: web: build: . ports: - "8000:8000" volumes: - .:/app command: python manage.py runserver 0.0.0.0:8000 environment: - DEBUG=True - SECRET_KEY='mysecret' depends_on: - db db: image: postgres:15 environment: - POSTGRES_DB: mydb - POSTGRES_USER: user - POSTGRES_PASSWORD: pass
Attempts:
2 left
💡 Hint
Check how environment variables are defined in YAML for lists vs mappings.
✗ Incorrect
In docker-compose.yml, the 'environment' key expects either a list of strings ('- KEY=value') or a mapping ('KEY: value'). Under 'db', it uses a list of mappings ('- KEY: value'), which is invalid YAML structure for Docker Compose environment variables and causes a configuration parsing error.
❓ state_output
advanced2:00remaining
What is the value of the environment variable DEBUG inside the Django container?
Given this docker-compose.yml snippet, what will be the value of DEBUG inside the running Django container?
services:
web:
image: django:latest
environment:
DEBUG: 'False'
SECRET_KEY: 'abc123'
command: python manage.py runserver 0.0.0.0:8000Django
services:
web:
image: django:latest
environment:
DEBUG: 'False'
SECRET_KEY: 'abc123'
command: python manage.py runserver 0.0.0.0:8000Attempts:
2 left
💡 Hint
Remember environment variables are always strings inside containers.
✗ Incorrect
Environment variables in Docker are always strings. So DEBUG will be the string 'False', not a boolean false.
🔧 Debug
advanced2:00remaining
Why does the Django container fail to connect to the database?
You have this docker-compose.yml:
The Django app inside 'web' fails to connect to the database. What is the most likely cause?
services:
web:
build: .
ports:
- "8000:8000"
depends_on:
- db
environment:
DATABASE_URL: postgres://user:pass@db:5432/mydb
db:
image: postgres:15
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: passThe Django app inside 'web' fails to connect to the database. What is the most likely cause?
Django
services:
web:
build: .
ports:
- "8000:8000"
depends_on:
- db
environment:
DATABASE_URL: postgres://user:pass@db:5432/mydb
db:
image: postgres:15
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: passAttempts:
2 left
💡 Hint
Think about what 'depends_on' controls and what it does not guarantee.
✗ Incorrect
'depends_on' only controls container startup order but does not wait for the database service to be ready to accept connections. Django tries to connect before Postgres is ready, causing failure.
🧠 Conceptual
expert3:00remaining
Which Dockerfile instruction best improves build caching for a Django app?
You want to optimize Docker image build times for your Django app. Which Dockerfile instruction order helps maximize cache reuse?
1. COPY . . 2. RUN pip install -r requirements.txt 3. COPY requirements.txt ./ 4. WORKDIR /app
Django
COPY . . RUN pip install -r requirements.txt COPY requirements.txt ./ WORKDIR /app
Attempts:
2 left
💡 Hint
Think about which files change most often and how Docker caches layers.
✗ Incorrect
To maximize cache reuse, first set the working directory, then copy only requirements.txt, then install dependencies, then copy the rest of the app. This way, if only app code changes, Docker reuses the cached pip install layer.