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

Sonica SonawaneSonica Sonawane
3 min read

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! ๐Ÿ™Œ

0
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!