0
0
Flaskframework~30 mins

Docker containerization in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker containerization
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use CMD ["python", "-m", "flask", "run", "--host=0.0.0.0", "--port=5000"] to start the app inside the container.