Dockerizing a Python Flask App – Step-by-Step with Custom Dockerfile

Here’s how I built a custom Docker image for my Flask app using a lightweight Python base image. Let me walk you through my Dockerfile
, what each line means, and how it helps containerize the app.
Writing Python Image
I chose python:3.10-slim
for its smaller footprint while still supporting everything I need. It reduces image size and speeds up build time.
WORKDIR /app This sets the working directory inside the container.
Any future commands (like COPY
, RUN
, etc.) will now be run from /app
.
COPY requirements.txt .
Copy the Python dependency list into the container.
The dot (.
) means we’re placing it into the current working directory (/app
).
RUN pip install -r requirements.txt Install Python dependencies inside the container.
This ensures your app has everything it needs, isolated from your host system.
COPY app.py .
Copy your Flask app code into the container.
Now the container has access to the main Python file.
EXPOSE 500
Tell Docker to expose port 5000.
Flask apps typically run on port 5000, so this lets the host access it through that port.
CMD ["python", "app.py"] Set the default command that runs when the container starts.
Here it runs the Flask app with python
app.py
.
Defining Flask Version
This line pins the Flask version to 2.3.2
, ensuring consistent behavior across environments.
Writing Flask Script
This small Python script creates a simple web server that displays helpful runtime information.
from flask import Flask import socket from datetime import datetime
We import:
Flask
for creating the web app.socket
to get the container's hostname.datetime
to display the current server time.app = Flask(__name__)
We initialize a Flask application instance.
__name__
helps Flask determine the root path of the app for templates and static files.@app.route("/") def home(): return f""" <h1>Hello from Docker!</h1> <p><b>Hostname:</b> {socket.gethostname()}</p> <p><b>Current Time:</b> {datetime.now()}</p> """
This defines the root route
/
of our app.It returns:
A welcome heading
The hostname of the running Docker container
The current server time (real-time!)
This makes it easy to verify the app is running inside Docker and see each new container’s unique hostname when restarted.
if __name__ == "__main__": app.run(host="0.0.0.0", port=5000)
Ensures the app runs when executed directly.
host="0.0.0.0"
allows Flask to listen on all network interfaces (necessary for Docker).Runs on port
5000
, which we expose in our Dockerfile.
Exposing Output On Port 5000
Running Flask Inside Docker
This is the live output of the Flask application running inside a Docker container:
What it shows:
Welcome Message: Confirms the app is running.
Hostname (
3ae8d8edab9f
): This unique container ID proves the app is running in an isolated container environment.Current Time: Shows real-time server output — useful to verify hot reloading or container restarts.
Subscribe to my newsletter
Read articles from Yash Shankhwalkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
