📖 Scenario: You are building a simple Flask web app that shows a welcome message. You want to package this app inside a Docker container so it can run anywhere easily.
🎯 Goal: Create a basic Flask app, add a Dockerfile to containerize it, and configure the container to run the app.
📋 What You'll Learn
Create a Flask app with one route that returns 'Hello from Flask in Docker!'
Create a Dockerfile that uses the official Python image
Install Flask inside the Docker container
Set the container to run the Flask app on port 5000
💡 Why This Matters
🌍 Real World
Docker lets developers package apps with all needed parts so they run the same anywhere. This is useful for sharing apps or deploying to servers.
💼 Career
Many software jobs require containerizing apps with Docker to simplify deployment and scaling in cloud or production environments.
Progress0 / 4 steps
1
Create the Flask app
Create a file called app.py with a Flask app. Import Flask from flask, create an app variable called app, and add a route / that returns the string 'Hello from Flask in Docker!'.
Flask
Hint
Remember to import Flask, create the app, and use @app.route('/') to define the home page.
2
Add a Dockerfile
Create a file called Dockerfile. Start with the base image python:3.12-slim. Set the working directory to /app. Copy app.py into the container.
Flask
Hint
Use FROM python:3.12-slim to start. Then set WORKDIR /app and copy app.py into it.
3
Install Flask and expose port
In the Dockerfile, add a line to install Flask using pip. Also, expose port 5000 for the Flask app.
Flask
Hint
Use RUN pip install flask to install Flask. Use EXPOSE 5000 to open the port.
4
Set the container command to run the app
In the Dockerfile, add a CMD instruction to run the Flask app with python. Use app.py and set the host to 0.0.0.0 and port to 5000 so it listens outside the container.
Flask
Hint
Use CMD ["python", "-m", "flask", "run", "--host=0.0.0.0", "--port=5000"] to start the app inside the container.
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
Step 1: Understand Docker's role
Docker packages an app with all its dependencies into a container.
Step 2: Connect Docker to Flask app usage
This packaging ensures the Flask app runs the same on any machine without setup issues.
Final Answer:
To package the Flask app and its environment so it runs consistently anywhere -> Option A
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
Step 1: Identify Dockerfile base image syntax
The Dockerfile must start with a FROM statement specifying the base image.
Step 2: Choose correct Python version image
"FROM python:3.12-slim" correctly sets Python 3.12 as base.
Final Answer:
FROM python:3.12-slim -> Option D
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
Step 1: Analyze Dockerfile commands
It sets Python 3.12 base, copies requirements.txt, installs dependencies, copies app files, and runs app.py.
Step 2: Understand build and run effects
docker build creates image with dependencies; docker run starts container running Flask app.
Final Answer:
The Flask app runs inside a container using Python 3.12 with dependencies installed -> Option B
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
Step 1: Check Dockerfile for dependency installation
No RUN command installs Flask or dependencies inside container.
Step 2: Understand error cause
ModuleNotFoundError means Flask package missing in container environment.
Final Answer:
Flask is not installed inside the container -> Option A
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
Step 1: Understand environment variable usage
Flask app reads variables at runtime from OS environment.
Step 2: Identify Docker method to pass runtime variables
Using docker run -e VAR=value passes variables dynamically to container.
Final Answer:
Pass variables with docker run -e VAR=value and access via os.environ in Flask -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.