🚀 Want to **deploy a Flask app using Docker and DockerHub on AWS EC2**? In this tutorial, I’ll guide you through the **step-by-step process** to containerize your Flask app, push it to **DockerHub**, and deploy it on an **AWS EC2 instance** using Docker. By the end of this video, you’ll learn: ✅ How to **containerize a Flask app using Docker** ✅ How to **push and pull Docker images from DockerHub** ✅ How to **deploy and run the Flask app on AWS EC2** ✅ How to **expose the Flask app to the internet using AWS security groups** — ## **🔹 What You’ll Learn in This Video:** ✔️ How to **write a Dockerfile for a Flask app** ✔️ How to **build and test a Docker image locally** ✔️ How to **upload the Docker image to DockerHub** ✔️ How to **install Docker on AWS EC2** ✔️ How to **run a Flask container on AWS EC2** — ## **🔹 Prerequisites** ✔️ **AWS Account** ([Sign Up Here](https://aws.amazon.com/free/)) ✔️ **Docker & DockerHub Account** ([Sign Up Here](https://hub.docker.com/)) ✔️ Basic knowledge of **Flask & Python** ✔️ A Flask app ready for deployment — ## **🔹 Step 1: Write a Dockerfile for Flask** In your Flask project folder, create a **Dockerfile**: “`dockerfile
# Use an official Python runtime as a parent image
FROM python:3.9 # Set the working directory
WORKDIR /app # Copy the current directory contents into the container
COPY . /app # Install dependencies
RUN pip install –no-cache-dir -r requirements.txt # Expose the port Flask runs on
EXPOSE 5000 # Define the command to run the Flask app
CMD ["python", "app.py"]
“` — ## **🔹 Step 2: Build & Test the Docker Image Locally** 1️⃣ Open your terminal and navigate to your Flask app directory. 2️⃣ Run the following command to build the Docker image: “`bash
docker build -t my-flask-app .
“` 3️⃣ Run the Flask container locally to test: “`bash
docker run -p 5000:5000 my-flask-app
“` 4️⃣ Open your browser and go to **http://localhost:5000** to see your Flask app running. — ## **🔹 Step 3: Push the Docker Image to DockerHub** 1️⃣ Log in to DockerHub from your terminal: “`bash
docker login
“` 2️⃣ Tag the Docker image with your DockerHub username: “`bash
docker tag my-flask-app your-dockerhub-username/my-flask-app
“` 3️⃣ Push the image to DockerHub: “`bash
docker push your-dockerhub-username/my-flask-app
“` ✅ Now your Flask Docker image is **hosted on DockerHub** and ready to be deployed on AWS EC2. — ## **🔹 Step 4: Launch an AWS EC2 Instance** 1️⃣ Log in to **AWS Console** → Go to **EC2** 2️⃣ Click **Launch Instance** 3️⃣ Select an **Amazon Machine Image (AMI)**: – **Ubuntu 22.04 LTS** (Recommended) – **Amazon Linux 2** 4️⃣ Choose **Instance Type** → Select **t2.micro** (Free Tier) 5️⃣ Configure **Security Group**: – Allow **SSH (port 22)** – Allow **HTTP (port 80)** – Allow **Custom TCP Rule (port 5000)** for Flask 6️⃣ Click **Launch** and **download the key pair (`.pem` file)** — ## **🔹 Step 5: Connect to AWS EC2 via SSH** 🔹 **For Mac/Linux Users:** “`bash
chmod 400 your-key.pem
ssh -i your-key.pem ubuntu@your-ec2-public-ip
“` 🔹 **For Windows Users (Using PuTTY):** 1️⃣ Convert `.pem` to `.ppk` using **PuTTYgen** 2️⃣ Open **PuTTY**, enter **EC2 Public IP**, and load the `.ppk` key 3️⃣ Click **Open** and log in as `ubuntu` (for Ubuntu) or `ec2-user` (for Amazon Linux) — ## **🔹 Step 6: Install Docker on AWS EC2** Once connected to EC2, run the following commands: 🔹 **Update system and install Docker:** “`bash
sudo apt update && sudo apt install docker.io -y
“` 🔹 **Enable & Start Docker Service:** “`bash
sudo systemctl enable docker
sudo systemctl start docker
“` 🔹 **Verify Docker installation:** “`bash
docker –version
“` — ## **🔹 Step 7: Pull & Run Flask Docker Image from DockerHub** 1️⃣ Pull your Flask app image from DockerHub: “`bash
docker pull your-dockerhub-username/my-flask-app
“` 2️⃣ Run the Flask container on EC2: “`bash
docker run -d -p 5000:5000 your-dockerhub-username/my-flask-app
“` ✅ Your Flask app is now running on port **5000** on your AWS EC2 instance. — ## **🔹 Step 8: Access Your Flask App on AWS** 1️⃣ Open your browser and go to: “`plaintext
http://your-ec2-public-ip:5000
“`
2️⃣ 🎉 Your Flask app is now live on AWS EC2! — ## **🔹 Optional: Run Flask Container in the Background (Restart on Reboot)** To keep your container running even after reboot, use: “`bash
docker run -d –restart unless-stopped -p 5000:5000 your-dockerhub-username/my-flask-app
“` ### **🔹 Hashtags:** #AWS #Docker #Flask #EC2 #DeployFlask #DockerHub #Python #CloudComputing #DevOps #DockerContainers #AWSDeployment