https://github.com/abhixsh/dockerized-flask-app-ec2
This project is a simple Flask web application that demonstrates how to create a dynamic web page using Flask and Docker. The app allows users to customize the background color through an environment variable, showcasing the integration of Flask with Docker and deployment on AWS EC2.
https://github.com/abhixsh/dockerized-flask-app-ec2
aws docker ec2 flask python
Last synced: about 2 months ago
JSON representation
This project is a simple Flask web application that demonstrates how to create a dynamic web page using Flask and Docker. The app allows users to customize the background color through an environment variable, showcasing the integration of Flask with Docker and deployment on AWS EC2.
- Host: GitHub
- URL: https://github.com/abhixsh/dockerized-flask-app-ec2
- Owner: abhixsh
- Created: 2024-11-01T11:43:42.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-01T13:34:53.000Z (over 1 year ago)
- Last Synced: 2025-01-02T18:48:31.954Z (over 1 year ago)
- Topics: aws, docker, ec2, flask, python
- Language: HTML
- Homepage:
- Size: 823 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dockerized Flask App on EC2
## Step 1: Set Up Your Project
### Create a Project Folder:
1. Open your file explorer and create a new folder named `flask_app`.
### Open Command Prompt:
1. Press `Win + R`, type `cmd`, and hit Enter.
### Navigate to Your Project Folder:
```bash
cd path\to\flask_app
```
## Step 2: Install Python and Flask
### Check if Python is Installed:
1. Type `python --version` in Command Prompt. If you see a version number, Python is installed. If not, download and install it from [python.org](https://www.python.org/).
### Install Flask:
1. Run this command in Command Prompt:
```bash
pip install flask
```
## Step 3: Create Your Flask App
### Create a Python File:
1. In your `flask_app` folder, create a file named `app.py`.
### Write the Code:
1. Open `app.py` in a text editor (like Notepad or VS Code) and paste this code:
```python
from flask import Flask, render_template
import os
app = Flask(__name__)
@app.route("/")
def home():
bg_color = os.environ.get("BG_COLOR", "lightblue") # Default color
return render_template("index.html", bg_color=bg_color)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
```
## Step 4: Create the HTML Template
### Create a Folder for Templates:
1. Inside the `flask_app` folder, create a new folder named `templates`.
### Create an HTML File:
1. Inside the `templates` folder, create a file named `index.html`.
### Write the HTML Code:
1. Open `index.html` in a text editor and paste this code:
```html
Flask App
body {
background-color: {{ bg_color }}; /* Background color from environment variable */
color: black;
font-family: Arial, sans-serif;
text-align: center;
padding: 50px;
}
Hello, Flask!
Welcome to your simple Flask web app.
```
## Step 5: Test Your Flask App
### Run the App:
1. In Command Prompt, navigate to your project folder again:
```bash
cd path\to\flask_app
```
2. Run the app with:
```bash
python app.py
```
### Open a Web Browser:
1. Go to `http://127.0.0.1:5000`. You should see your Flask app!

## Step 6: Create a Dockerfile
### Create a Dockerfile:
1. In the `flask_app` folder, create a file named `Dockerfile` (no extension).
### Write the Dockerfile Code:
1. Open the `Dockerfile` in a text editor and paste this code:
```dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY . /app
RUN apt-get update && apt-get install -y python3-venv \
&& python3 -m venv venv
RUN . venv/bin/activate && pip install flask
EXPOSE 5000
ENV FLASK_APP=app.py
CMD ["venv/bin/python", "app.py"]
```
## Step 7: Build the Docker Image
### Install Docker:
1. Download and install Docker Desktop from Docker's website.
### Open Docker Command Line:
1. You can use the Command Prompt or PowerShell.
### Navigate to Your Project Folder:
```bash
cd path\to\flask_app
```
### Build the Docker Image:
```bash
docker build -t flask_app .
```
## Step 8: Push the Docker Image to Docker Hub
### Create a Docker Hub Account:
1. Sign up at Docker Hub.
### Log in to Docker Hub:
```bash
docker login
```
### Tag Your Image:
```bash
docker tag flask_app your_dockerhub_username/flask_app
```
### Push the Image:
```bash
docker push your_dockerhub_username/flask_app
```

## Docker Hub Repository
You can find the Docker image for this project on Docker Hub: [abhixsh/flask_app](https://hub.docker.com/r/abhixsh/flask_app)
## Step 9: Run Your Docker Container on EC2
### Launch an EC2 Instance:
1. Go to AWS Management Console and launch a new EC2 instance using an Ubuntu image.
### SSH into Your EC2 Instance:
1. Follow the instructions to connect via SSH.
### Install Docker on EC2:
```bash
sudo apt-get update
sudo apt-get install -y docker.io
```
### Pull Your Docker Image:
```bash
sudo docker pull your_dockerhub_username/flask_app
```
### Run Your Docker Container:
1. Replace `pink` with any color you want for the background:
```bash
sudo docker run -d -p 5000:5000 -e BG_COLOR=pink your_dockerhub_username/flask_app
```
### Access Your Flask App:
1. Open a web browser and go to `http://:5000`.

## Check EC2 Security Group Settings
Make sure the Security Group associated with your EC2 instance allows inbound traffic on port 5000 (or the port you specified in your Docker command). To do this, go to the EC2 Dashboard > Instances > select your instance > Security > Security Groups. Click on the Security Group, then Edit Inbound Rules, and add the following rule:
- **Type:** Custom TCP
- **Port Range:** 5000
- **Source:** Anywhere (0.0.0.0/0) if it’s public, or specify your IP for more security.
