Enable Dark Mode!
how-to-run-a-simple-flask-app-in-docker.jpg
By: Swaraj R

How to Run a Simple Flask app in Docker

Technical

Ever wanted to take your Python Flask app, wrap it up in a neat little box, and run it anywhere without worrying about dependencies?

That’s exactly what Docker lets you do!

Think of Docker as a magic lunchbox — you put your app, its ingredients (dependencies), and the kitchen instructions (setup code) inside, close the lid, and it’ll taste the same no matter where you open it.

In this blog, we’ll walk through building a simple Flask app, containerizing it with Docker, and running it like a pro.

Step 1 – Create the Flask App

Let’s start with the most basic Flask app that serves a styled HTML page.

app.py

from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def home():
    return render_template("index.html")
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Step 2 – Add a Styled HTML Template

Create a folder called templates in the same directory as app.py.

Inside it, create index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask in Docker</title>
    <style>
        body {
            background: linear-gradient(135deg, #4facfe, #00f2fe);
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            color: white;
            text-align: center;
        }
        .container {
            background: rgba(0, 0, 0, 0.3);
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0px 8px 20px rgba(0,0,0,0.3);
        }
        h1 {
            font-size: 2.5rem;
            margin-bottom: 15px;
        }
        p {
            font-size: 1.2rem;
            opacity: 0.9;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>?? Flask App is Running in Docker! ??</h1>
        <p>Your application is live inside a Docker container.</p>
    </div>
</body>
</html>

Step 3 – Add Dependencies

Create a requirements.txt file:

flask==3.0.3

Step 4 – Write the Dockerfile

The Dockerfile is basically your app’s recipe card for Docker, it tells Docker which ingredients (base image, dependencies) to use and what steps to follow to get your app running.

First, create an empty file named Dockerfile in your project directory:

touch Dockerfile

Dockerfile

# Base image
FROM python:3.11-slim
# Set working directory
WORKDIR /app
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy app code
COPY . .
# Expose Flask port
EXPOSE 5000
# Start the app
CMD ["python", "app.py"]

Step 5 – Build the Docker Image

In your terminal, navigate to the app’s directory and run:

docker build -t flask-docker-app

Step 6 – Run the Docker Container

docker run -p 5000:5000 flask-docker-app

Now visit:

http://localhost:5000

You should see your styled “Flask App is Running in Docker” page.

Step 7 – Stopping the Container

Press CTRL + C in the terminal running your container.

Or list and stop containers manually:

docker ps   # List running containers

docker stop <container_id>

That's it! We’ve successfully built a Flask app, packaged it into a Docker image, and run it inside a container, all without installing Flask directly on your computer.

With Docker, you can:

  • Deploy the same setup on development, staging, and production
  • Save yourself from dependency headaches.

Now it’s your turn; go ahead and Dockerize more apps!

To read more about What is Flask Python & It's Key Features, refer to our blog What is Flask Python & It's Key Features.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message