Skip to main content

Command Palette

Search for a command to run...

Flask app

Creating a Flask Web Application with Docker: A Step-by-Step Guide

Published
3 min read
Flask app

In this blog post, we'll walk through how to create a simple Flask web application using Python 3.11, and then containerize it using Docker. Our application will include a web page with a greeting message and an API endpoint that indicates the server is up and running. This is a great exercise to understand the basics of Flask and Docker.

1. Create the Flask Application

a. Project Structure

  1. Create Project Directory:
mkdir flask-app
cd flask-app

  1. Set Up Your Flask App:

    Create app.py

    In the project directory, create a file named app.py with the following content:

from flask import Flask, jsonify, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/api/status', methods=['GET'])
def status():
    return jsonify({'message': 'Server is up and running'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

  1. Create the Templates Directory

Create a templates directory to store the HTML file:

mkdir templates
  1. Create templates/index.html

Inside the templates directory, create a file named index.html with the following content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask App</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
            margin: 0;
        }
        .container {
            text-align: center;
            padding: 20px;
            border: 2px solid #ddd;
            border-radius: 10px;
            background-color: #ffffff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            margin-bottom: 10px;
        }
        p {
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Hello, Dosto!</h1>
        <p>Welcome to our Flask application.</p>
    </div>
</body>
</html>

  1. Create requirements.txt

List Flask as a dependency in a requirements.txt file:

Flask==2.3.0

2. Create a Dockerfile

  1. Create Dockerfile

    In the project directory, create a file named Dockerfile with the following content:

#base image
FROM python:3.11-slim

#working directory
WORKDIR /app

#copy requirement files into containers
COPY requirements.txt requirements.txt

#Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

#Copy application into container
COPY . .

#Expose the port Flask is running on
EXPOSE 5000

#Command to run the application
CMD ["python", "app.py"]

3. Build and run the Docker Container

  1. Build the Docker Image

    From the project directory, build the Docker image:

docker build -t flask_app:latest .

  1. Run the Docker Container

    Run the container, mapping port 5000 in the container to port 5000 on your host:

     docker run -d -p 5000:5000 flask_app:latest
    

4. Verify the Application

Note: here <http://34.220.185.7\> is Public IP

Summary

  • app.py: Contains the Flask application code with routes for the web page and API.

  • templates/index.html: Provides a styled HTML page with a greeting message.

  • Dockerfile: Defines how to build the Docker image for the Flask app.

  • requirements.txt: Lists the Flask dependency for installation in Docker.

This setup should provide a fully functional Flask application that’s containerized with Docker

More from this blog

DevOps journey

34 posts

In this DevOps journey, we’ll explore a range of DevOps tools and related projects.