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

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
- Create Project Directory:
mkdir flask-app
cd flask-app

Set Up Your Flask App:
Create
app.pyIn the project directory, create a file named
app.pywith 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)

- Create the Templates Directory
Create a templates directory to store the HTML file:
mkdir templates
- 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>

- Create
requirements.txt
List Flask as a dependency in a requirements.txt file:
Flask==2.3.0
2. Create a Dockerfile
Create Dockerfile
In the project directory, create a file named
Dockerfilewith 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
Build the Docker Image
From the project directory, build the Docker image:
docker build -t flask_app:latest .


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
- Web Page: Open your web browser and navigate to
http://34.220.185.7:5000/to see the greeting page.
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



