๐ Beginner Friendly Guide: Deploy a Python Flask App with Docker on EC2 & Push to Docker Hub

Hello everyone! ๐
Today, Iโm sharing a simple, step-by-step hands-on guide to help you deploy a Python Flask app using Docker on an Amazon Linux EC2 instance โ and then push your Docker image to Docker Hub.
๐ ๏ธ Why this tutorial?
โ Get hands-on experience with Docker on AWS EC2
โ Learn how to containerize a Python Flask app
โ Push your image to Docker Hub for easy reuse
โ๏ธ Prerequisites
AWS EC2 instance running Amazon Linux
Docker installed
Docker Hub account
Basic SSH knowledge
๐ Step 1: SSH into your EC2 instance
ssh -i your-key.pem ec2-user@your-ec2-public-ip
๐ณ Step 2: Install Docker
sudo yum update -y
sudo yum install docker -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ec2-user
newgrp docker
docker --version
๐ฆ Step 3: Create Your Flask App
mkdir flask-docker-demo
cd flask-docker-demo
vi app.py
Paste the following code inside app.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello from Flask running in Docker on EC2!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
๐ Step 4: Add dependencies
vi requirements.txt
Add:
flask
๐งฑ Step 5: Create Dockerfile
vi Dockerfile
Paste:
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
๐ Dockerfile Explained (Simple Terms)
# Use official Python image
FROM python:3.11-slim
Starts with a lightweight Python 3.11 image as the base.
# Set working directory
WORKDIR /app
Sets the folder inside the container to /app
where your code will live.
# Copy files
COPY . .
Copies all your local project files into the containerโs /app
folder.
# Install dependencies
RUN pip install -r requirements.txt
Installs the Python packages your app needs (like Flask).
# Expose port
EXPOSE 5000
Tells Docker your app will use port 5000 inside the container.
# Run the app
CMD ["python", "app.py"]
Runs your Flask app when the container starts.
In short:
This Dockerfile builds a Python image, copies your app code, installs Flask, and runs your server on port 5000.
๐จ Step 6: Build the Docker Image
docker build -t flask-app .
๐ Step 7: Run Your Flask App
docker run -d -p 5000:5000 flask-app
Now open your browser:
http://<your-ec2-public-ip>:5000
You should see:
Hello from Flask running in Docker on EC2!
โ๏ธ Step 8: Push Your Image to Docker Hub
1๏ธโฃ Login to Docker Hub
docker login
Enter your Docker Hub credentials (username: sonica05
).
2๏ธโฃ Tag your image
docker tag flask-app sonica05/flask-app:latest
3๏ธโฃ Push to Docker Hub
docker push sonica05/flask-app:latest
โ What You Learned
How to run a Flask app in Docker
Deploy it on EC2 with Amazon Linux 2023
Push your Docker image to Docker Hub
Thanks for reading! ๐
Subscribe to my newsletter
Read articles from Sonica Sonawane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sonica Sonawane
Sonica Sonawane
Hi, I'm Sonica! ๐ Iโm currently diving into the world of DevOps, focusing on AWS, Docker, Kubernetes, Linux, and GitHub. My passion lies in automating systems, building cloud infrastructure, and optimizing workflows. Iโm committed to continuous learning, hands-on projects, and sharing my journey with others in the tech community. Before shifting to DevOps, I worked in IT Sales, where I gained valuable skills in client communication, requirement gathering, and problem-solving. This experience taught me how to connect technical solutions to business needs, which has been instrumental as I transition into DevOps, where technical expertise and problem-solving go hand in hand. Now, Iโm eager to apply my sales experience alongside my growing technical skills in cloud engineering and DevOps. Join me as I explore the latest trends, challenges, and solutions in the world of cloud computing!