How to Build a Docker Image for a Flask App and Run It on Your System | Step-by-Step Guide (2025)



Want to containerize your Flask application with Docker? 🐳 In this tutorial, we’ll walk you through the process of building a **Docker image** for a Flask app and running it seamlessly on your system. This is perfect for developers looking to deploy Flask applications in a **consistent and portable** environment. — ### **What You’ll Learn in This Video:** βœ… What is Docker & Why Use It for Flask? βœ… Setting Up a Flask App for Dockerization βœ… Writing a Dockerfile for Flask βœ… Building and Running a Docker Image βœ… Exposing Flask to the Host System βœ… Debugging and Managing Docker Containers — ## **Why Use Docker for Flask?** – πŸ“¦ Creates an isolated, reproducible environment – πŸš€ Eliminates "works on my machine" issues – ⚑ Simplifies deployment across multiple environments – πŸ”„ Works seamlessly with cloud platforms — ## **Step-by-Step Guide** ### **1️⃣ Install Prerequisites** Before you start, ensure you have: βœ”οΈ Python & Flask installed βœ”οΈ Docker installed ([Download Docker](https://www.docker.com/get-started)) — ### **2️⃣ Create a Flask App** If you don’t have a Flask project, create a simple app: “`bash
mkdir flask-docker && cd flask-docker
python -m venv venv
source venv/bin/activate # For Windows: venv\Scripts\activate
pip install flask
“` Create a **`app.py`** file with the following content: “`python
from flask import Flask app = Flask(__name__) @app.route("/")
def home(): return "Hello, Dockerized Flask!" if __name__ == "__main__": app.run(host="0.0.0.0", port=5000)
“` — ### **3️⃣ Create a `requirements.txt` File** “`bash
flask
“`
Save this file in the project folder. — ### **4️⃣ Create a `Dockerfile`** Inside your **flask-docker** directory, create a file named **Dockerfile** and add the following content: “`dockerfile
# Use the official Python image as the base image
FROM python:3.9 # Set the working directory in the container
WORKDIR /app # Copy the current directory contents into the container at /app
COPY . /app # Install dependencies
RUN pip install –no-cache-dir -r requirements.txt # Expose the Flask port
EXPOSE 5000 # Run the Flask app
CMD ["python", "app.py"]
“` — ### **5️⃣ Create a `.dockerignore` File** To exclude unnecessary files, create a **`.dockerignore`** file: “`
__pycache__/
*.pyc
*.pyo
venv/
“` — ### **6️⃣ Build the Docker Image** Run the following command in your project directory: “`bash
docker build -t flask-app .
“` — ### **7️⃣ Run the Docker Container** To run the Flask app inside a container, use: “`bash
docker run -p 5000:5000 flask-app
“` Now, open your browser and visit: πŸ”— [http://localhost:5000](http://localhost:5000) — ### **8️⃣ (Optional) Run in Detached Mode** To keep the container running in the background: “`bash
docker run -d -p 5000:5000 flask-app
“` — ### **9️⃣ (Optional) View Running Containers** List all running containers: “`bash
docker ps
“` Stop a container: “`bash
docker stop container_id
“` — ## **Who Is This Tutorial For?** – πŸš€ Developers looking to deploy Flask apps using Docker – πŸ› οΈ Beginners who want to learn containerization – πŸ’» Anyone interested in streamlining app deployment — ## **Resources Mentioned in This Video:** – πŸ”— Docker Docs: [https://docs.docker.com/](https://docs.docker.com/) – πŸ“œ Flask Docs: [https://flask.palletsprojects.com/](https://flask.palletsprojects.com/) — ## **Pro Tips for a Smooth Setup:** βœ… Use a **`.dockerignore`** file to exclude unnecessary files. βœ… **Expose the correct ports** so Flask is accessible outside the container. βœ… Bind mount a volume during development to **avoid rebuilding the image** every time. “`bash
docker run -v $(pwd):/app -p 5000:5000 flask-app
“` — ## **Don’t Forget to Subscribe!** If this tutorial helped you, give it a **thumbs up πŸ‘, subscribe** for more Docker and Python content, and leave a comment if you have any questions! πŸš€ ### **Hashtags:** #Docker #Flask #Python #DockerTutorial #Containerization #WebDevelopment #DevOps #FlaskDocker #PythonDevelopment #DeployFlask Start running your Flask apps with Docker today! 🐳πŸ”₯