Skip to main content

Command Palette

Search for a command to run...

Day 31: Deploying a Flask App Using Docker Container on AWS – Hands-on

Published
3 min read
Day 31: Deploying a Flask App Using Docker Container on AWS – Hands-on

✅ Step 1: Clone Your Flask Repository from GitHub onto Your AWS Terminal

We begin by going to GitHub and cloning our Flask app repository into the Amazon EC2 terminal using git clone.

bashCopyEditgit clone <your-github-repo-url>


✅ Step 2: Open the Repository Using Vim Editor

Once the repo is cloned, go inside the repository and open the files using the vim command.

bashCopyEditcd <your-repo-name>
vim <filename>


✅ Step 3: Create a Dockerfile in Your Repository

Now we’ll write our Dockerfile, which will be responsible for building our Docker image step-by-step.

Here are the six layers of the Dockerfile we’ll create:

🔹 1. Environment Setup

We’ll start from a base image. In this case, we’ll use a Bash-compatible image which provides access to all the packages and libraries required.

🔹 2. Set Working Directory

This sets a working directory inside the container.

🔹 3. Copy Source Code

This command copies all the files from the source directory into the container’s working directory.

🔹 4. Install Dependencies

We’ll use the RUN command to install all the required packages listed in the requirements.txt file.

🔹 5. Expose Port

Expose the port number the app will run on. This makes it accessible publicly or privately, depending on configuration.

🔹 6. Start the Application

We use ENTRYPOINT or CMD to run the containerized Flask app.

📌 Note: These 6 steps (layers) make up the Dockerfile. When we build the image, these layers are executed sequentially.


✅ Step 4: Build the Docker Image

Now use the Dockerfile to build your image with the following command:

bashCopyEditdocker build -t flask-app .


✅ Step 5: Understand the Flask App and Port

Before running the container, keep in mind that the Flask app is a web application that runs on a specific port. In this project, the Flask app is configured to run on port 80, which means it should be publicly accessible.

So, to run it:

bashCopyEditdocker run -d -p 80:80 flask-app

This command means:

  • -p 80:80 → Maps host port 80 to container port 80

  • -d → Runs the container in detached (background) mode

You can confirm the container is running using:

bashCopyEditdocker ps


✅ Step 6: Access Your Application via IP

Now, go to your EC2 instance page and copy the IPv4 public IP of your instance.

Paste this IP into your browser with HTTP (since it's on port 80):

textCopyEdithttp://<your-ip-address>


⚠️ Debugging: What If the Site Doesn’t Open?

If the site doesn’t open, check the container logs to find the issue:

bashCopyEditdocker logs <container-id>

After checking the logs, you might notice that port 80 is not publicly accessible. To solve this:


✅ Step 7: Make Port 80 Public via Security Group Settings

  1. Go to the EC2 Dashboard → Select your instance

  2. Go to Security > Security Groups

  1. Click on Edit Inbound Rules

  2. Add a rule:

    • Type: HTTP

    • Port range: 80

    • Source: Anywhere (0.0.0.0/0)

  3. Click Save rules

Now port 80 will be open for public access.


🎉 Final Result

Visit your EC2 instance IP again in the browser:

textCopyEdithttp://<your-ip-address>

Your Flask web application should now be running perfectly inside a Docker container on an AWS EC2 instance!