Bird
Raised Fist0
Flaskframework~20 mins

Docker containerization in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker Mastery for Flask
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 Flask app?
Consider this Dockerfile for a simple Flask app. What will be the output when you run the container and access the root URL?
Flask
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
ENV FLASK_APP=app.py
CMD ["flask", "run", "--host=0.0.0.0"]
AThe Flask app starts and is accessible on port 5000 inside the container.
BThe container fails to start because the CMD syntax is incorrect.
CThe Flask app starts but is only accessible on localhost inside the container, not from outside.
DThe container starts but Flask raises an error because FLASK_APP is not set.
Attempts:
2 left
💡 Hint
Think about how Flask's default port and host settings work inside Docker.
📝 Syntax
intermediate
1:30remaining
Which Dockerfile command correctly copies only Python files to /app?
You want to copy only .py files from your project root to /app in the container. Which COPY command is correct?
ACOPY /root/*.py /app/
BCOPY /app/*.py /app/
CCOPY ./*.py /app/
DCOPY *.py /app/
Attempts:
2 left
💡 Hint
Remember COPY paths are relative to the build context.
🔧 Debug
advanced
2:30remaining
Why does this Flask app inside Docker fail to connect to the database?
You have a Flask app in Docker that connects to a database at 'localhost:5432'. The app fails to connect. Why?
Flask
DATABASE_URL = 'postgresql://user:pass@localhost:5432/dbname'
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL
ABecause 'localhost' inside the container points to the container itself, not the host machine.
BBecause the database URL is missing the port number.
CBecause Flask cannot connect to databases inside Docker containers.
DBecause the database URL must use '127.0.0.1' instead of 'localhost'.
Attempts:
2 left
💡 Hint
Think about network isolation in Docker containers.
❓ state_output
advanced
2:30remaining
What is the effect of this Docker Compose service configuration on Flask app state?
Given this docker-compose.yml snippet, what happens to the Flask app's code changes on the host during container runtime?
Flask
services:
  web:
    build: .
    volumes:
      - .:/app
    ports:
      - "5000:5000"
AThe container crashes because volumes cannot be used with Flask apps.
BCode changes on the host immediately reflect inside the container without rebuilding.
CCode changes require rebuilding the image to take effect inside the container.
DThe container ignores the volume and uses the image code only.
Attempts:
2 left
💡 Hint
Volumes link host files to container files in real time.
🧠 Conceptual
expert
3:00remaining
Why use multi-stage builds in Docker for Flask apps?
What is the main benefit of using multi-stage Docker builds when containerizing a Flask application?
ATo allow running multiple Flask apps in one container.
BTo enable Flask apps to automatically restart on code changes.
CTo reduce the final image size by separating build dependencies from runtime environment.
DTo make the container run faster by using multiple CPU cores.
Attempts:
2 left
💡 Hint
Think about what stays in the final image after building.

Practice

(1/5)
1. What is the main purpose of using Docker with a Flask application?
easy
A. To package the Flask app and its environment so it runs consistently anywhere
B. To write Flask application code faster
C. To replace Flask's routing system
D. To automatically generate HTML templates

Solution

  1. Step 1: Understand Docker's role

    Docker packages an app with all its dependencies into a container.
  2. Step 2: Connect Docker to Flask app usage

    This packaging ensures the Flask app runs the same on any machine without setup issues.
  3. Final Answer:

    To package the Flask app and its environment so it runs consistently anywhere -> Option A
  4. Quick Check:

    Docker packages app + environment = B [OK]
Hint: Docker ensures app runs same everywhere by packaging environment [OK]
Common Mistakes:
  • Thinking Docker speeds up coding
  • Confusing Docker with Flask features
  • Assuming Docker changes app logic
2. Which of the following is the correct first line in a Dockerfile for a Flask app using Python 3.12?
easy
A. EXPOSE 5000
B. RUN python3.12 install flask
C. COPY app.py /app
D. FROM python:3.12-slim

Solution

  1. Step 1: Identify Dockerfile base image syntax

    The Dockerfile must start with a FROM statement specifying the base image.
  2. Step 2: Choose correct Python version image

    "FROM python:3.12-slim" correctly sets Python 3.12 as base.
  3. Final Answer:

    FROM python:3.12-slim -> Option D
  4. Quick Check:

    Dockerfile starts with FROM base image = D [OK]
Hint: Dockerfile always starts with FROM base image line [OK]
Common Mistakes:
  • Using RUN before FROM
  • Copying files before setting base image
  • Exposing ports before base image
3. Given this Dockerfile snippet for a Flask app:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

What happens when you run docker build . and then docker run?
medium
A. The app fails because requirements.txt is not copied
B. The Flask app runs inside a container using Python 3.12 with dependencies installed
C. The container runs but does not start the Flask app
D. The build command installs Flask globally on the host machine

Solution

  1. Step 1: Analyze Dockerfile commands

    It sets Python 3.12 base, copies requirements.txt, installs dependencies, copies app files, and runs app.py.
  2. Step 2: Understand build and run effects

    docker build creates image with dependencies; docker run starts container running Flask app.
  3. Final Answer:

    The Flask app runs inside a container using Python 3.12 with dependencies installed -> Option B
  4. Quick Check:

    Build installs deps, run starts app = A [OK]
Hint: COPY then RUN pip install ensures dependencies inside container [OK]
Common Mistakes:
  • Thinking dependencies install on host
  • Assuming app.py is not copied
  • Believing container runs without CMD
4. You wrote this Dockerfile for your Flask app:
FROM python:3.12-slim
COPY app.py /app/
CMD ["python", "app.py"]

When you run the container, the app crashes with "ModuleNotFoundError: flask". What is the problem?
medium
A. Flask is not installed inside the container
B. app.py is not copied to the container
C. CMD syntax is incorrect
D. Python version 3.12-slim does not support Flask

Solution

  1. Step 1: Check Dockerfile for dependency installation

    No RUN command installs Flask or dependencies inside container.
  2. Step 2: Understand error cause

    ModuleNotFoundError means Flask package missing in container environment.
  3. Final Answer:

    Flask is not installed inside the container -> Option A
  4. Quick Check:

    Missing Flask install causes ModuleNotFoundError = C [OK]
Hint: Always RUN pip install for dependencies inside Dockerfile [OK]
Common Mistakes:
  • Assuming Flask is preinstalled in base image
  • Thinking app.py copy is missing
  • Believing CMD syntax causes import errors
5. You want to containerize a Flask app that reads environment variables for configuration. Which Dockerfile addition ensures environment variables set at runtime are accessible inside the Flask app?
hard
A. Hardcode variables inside app.py before building image
B. Use ENV in Dockerfile to set variables permanently
C. Pass variables with docker run -e VAR=value and access via os.environ in Flask
D. Add EXPOSE for environment variables

Solution

  1. Step 1: Understand environment variable usage

    Flask app reads variables at runtime from OS environment.
  2. Step 2: Identify Docker method to pass runtime variables

    Using docker run -e VAR=value passes variables dynamically to container.
  3. Final Answer:

    Pass variables with docker run -e VAR=value and access via os.environ in Flask -> Option C
  4. Quick Check:

    Runtime env vars via docker run -e = A [OK]
Hint: Use docker run -e to pass env vars dynamically to container [OK]
Common Mistakes:
  • Confusing ENV for runtime variable passing
  • Hardcoding vars loses flexibility
  • Thinking EXPOSE relates to env vars
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard