0
0
Djangoframework~20 mins

Docker containerization in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Docker Django Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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"]
AThe Django development server starts inside the container and listens on port 8000 on all interfaces.
BThe Django server will start but only listen on localhost inside the container, so it won't be accessible externally.
CThe container will fail because the requirements.txt file is not copied before pip install.
DThe container will run but exit immediately because CMD is missing the entrypoint.
Attempts:
2 left
💡 Hint
Think about what the CMD command does and how the server binds to network interfaces.
📝 Syntax
intermediate
2: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: pass
Django
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
AThe 'depends_on' field must be a string, not a list.
BThe environment variables under 'db' use colon syntax instead of dash list syntax, causing a syntax error.
CThe 'command' field must be a list, not a string.
DThe 'volumes' mapping is invalid because it uses relative path without quotes.
Attempts:
2 left
💡 Hint
Check how environment variables are defined in YAML for lists vs mappings.
state_output
advanced
2: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:8000
Django
services:
  web:
    image: django:latest
    environment:
      DEBUG: 'False'
      SECRET_KEY: 'abc123'
    command: python manage.py runserver 0.0.0.0:8000
AThe boolean value False.
BThe value will be undefined because environment variables cannot be set this way.
CThe string 'False' (not a boolean false).
DAn empty string because environment variables are always strings.
Attempts:
2 left
💡 Hint
Remember environment variables are always strings inside containers.
🔧 Debug
advanced
2:00remaining
Why does the Django container fail to connect to the database?
You have this docker-compose.yml:
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: pass

The 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: pass
AThe 'depends_on' does not wait for the database to be ready, so Django connects too early.
BThe DATABASE_URL environment variable is incorrectly formatted and missing the password.
CThe database container 'db' is not ready when Django tries to connect, causing connection failure.
DThe port 5432 is not exposed on the 'db' container, so Django cannot reach it.
Attempts:
2 left
💡 Hint
Think about what 'depends_on' controls and what it does not guarantee.
🧠 Conceptual
expert
3: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
A1,2,4,3
B4,3,2,1
C1,3,2,4
D1,2,3,4
Attempts:
2 left
💡 Hint
Think about which files change most often and how Docker caches layers.