- Open your file explorer and create a new folder named
flask_app
.
- Press
Win + R
, typecmd
, and hit Enter.
cd path\to\flask_app
- Type
python --version
in Command Prompt. If you see a version number, Python is installed. If not, download and install it from python.org.
- Run this command in Command Prompt:
pip install flask
- In your
flask_app
folder, create a file namedapp.py
.
- Open
app.py
in a text editor (like Notepad or VS Code) and paste this code:
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)
- Inside the
flask_app
folder, create a new folder namedtemplates
.
- Inside the
templates
folder, create a file namedindex.html
.
- Open
index.html
in a text editor and paste this code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flask App</title>
<style>
body {
background-color: {{ bg_color }}; /* Background color from environment variable */
color: black;
font-family: Arial, sans-serif;
text-align: center;
padding: 50px;
}
</style>
</head>
<body>
<h1>Hello, Flask!</h1>
<p>Welcome to your simple Flask web app.</p>
</body>
</html>
- In Command Prompt, navigate to your project folder again:
cd path\to\flask_app
- Run the app with:
python app.py
- Go to
http://127.0.0.1:5000
. You should see your Flask app!
- In the
flask_app
folder, create a file namedDockerfile
(no extension).
- Open the
Dockerfile
in a text editor and paste this code:
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"]
- Download and install Docker Desktop from Docker's website.
- You can use the Command Prompt or PowerShell.
cd path\to\flask_app
docker build -t flask_app .
- Sign up at Docker Hub.
docker login
docker tag flask_app your_dockerhub_username/flask_app
docker push your_dockerhub_username/flask_app
You can find the Docker image for this project on Docker Hub: abhixsh/flask_app
- Go to AWS Management Console and launch a new EC2 instance using an Ubuntu image.
- Follow the instructions to connect via SSH.
sudo apt-get update
sudo apt-get install -y docker.io
sudo docker pull your_dockerhub_username/flask_app
- Replace
pink
with any color you want for the background:
sudo docker run -d -p 5000:5000 -e BG_COLOR=pink your_dockerhub_username/flask_app
- Open a web browser and go to
http://<Your-EC2-Public-IP>:5000
.
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.