🎓 A1Learner
🔍
0 of 50 lessons completed

Project: Containerise a Python Web App

This project containerises a small Python web app. A Flask app serves one page, a Dockerfile builds it into an image, and docker run serves it on the host.

The app

Flask serves one page that names the project.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello from Docker!"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

The requirements

One line names the Flask version to install.

Flask==3.1.0

The Dockerfile

The build installs Flask and runs the app.

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
CMD ["python", "app.py"]

Example

docker build -t g6-flaskapp:1.0 . >/dev/null
docker run -d --name g6-flaskapp -p 8112:5000 g6-flaskapp:1.0 >/dev/null
sleep 3
curl -s http://localhost:8112/

Key points

  • Flask: a small Python web framework, used here for one page.
  • The app is three files: app.py, requirements.txt, and a Dockerfile.
  • requirements.txt names Flask, so the build installs it.
  • The Dockerfile copies the app, installs Flask, and runs it.
  • Run it with docker run -p, and open the page in a browser.

In this example, the Flask app is built into an image and run in a container. curl fetches the page through the mapped port. The greeting proves the app, the image and the port mapping all work together.

Note

The app listens on port 5000 inside the container. The -p flag maps it to the host.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What does the Dockerfile do for the Flask app?