Skip to content

arumullayaswanth/python-flask-docker-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Python Flask App with Docker on EC2 & AWS Lambda

This is a simple Python Flask application that prints the hostname and IP address of the container it's running in.

🐳 Part 1: EC2

πŸ“¦ Prerequisites

  • AWS EC2 instance (Amazon Linux 2 or similar)

πŸš€ Setup Python Flask App with EC2

1. Install Git

sudo yum install git -y

2. Clone the Repository

git clone https://github.com/arumullayaswanth/python-flask-docker.git
cd python-flask-docker

3. Install Python3 and pip

sudo yum install python3 -y
sudo yum install python3-pip -y

4. Install Flask Requirements

pip3 install -r requirements.txt

5. Navigate to App Directory

cd src/
ls

6. Run the App

python3 app.py

πŸ” To check running processes:

ps aux | grep python

πŸŒ€ Run App in Background with nohup

nohup allows your app to continue running even after terminal disconnect:

nohup python3 app.py > flask.log 2>&1 &

πŸ’ͺ Check logs:

tail -f flask.log

❗ If port 5000 is in use:

lsof -i :5000
kill -9 <PID>

πŸš€ Python Flask App in Docker on AWS EC2

This guide walks you through setting up a Python Flask app inside a Docker container on an AWS EC2 instance.


πŸ“¦ Prerequisites

  • AWS EC2 instance (Amazon Linux 2 or similar)
  • Security group allowing inbound traffic on port 5000
  • SSH access to EC2 instance

🧰 Step-by-Step Setup

1. βœ… Install System Dependencies

sudo yum update -y
sudo yum install git -y
sudo yum install python3-pip -y

2. πŸ”„ Clone the GitHub Repository

git clone https://github.com/arumullayaswanth/python-flask-docker.git
cd python-flask-docker

3. πŸ“¦ Install Python Dependencies

pip3 install -r requirements.txt

4. 🐳 Install and Enable Docker

sudo yum install docker -y
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker

If


πŸ‹ Dockerize the Application

5. πŸ“„ Dockerfile (should already exist)

Ensure the file has the following content:

FROM python:3.13-alpine
LABEL maintainer="yaswanth@gmail.com"
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["src/app.py"]

6. πŸ—οΈ Build Docker Image

docker build -t flask-docker-app .
docker images

7. πŸš€ Run the Docker Container

docker run -d -p 5000:5000 flask-docker-app
docker ps

8. βœ… Verify

Open your browser and visit:

http://<your-ec2-public-ip>:5000

🧼 Useful Docker Commands

# List running containers
docker ps

# List all containers
docker ps -a

# Stop a container
docker stop <container-id>

# Kill a container
docker kill <container-id>

# Remove stopped containers
docker container prune

☁️ AWS Lambda (Optional Version)

You can’t run Flask directly on Lambda, but here’s what you can do:

βœ… If you need API-like behavior in Lambda:

  • Use AWS Lambda + API Gateway instead of Flask.
  • Convert Flask logic into a Lambda-compatible handler:
import json
import socket

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps({
            'hostname': socket.gethostname(),
            'ip': socket.gethostbyname(socket.gethostname())
        })
    }

πŸ› οΈ Package and Deploy

  1. Create a zip package of your code.
  2. Deploy it to Lambda.
  3. Use API Gateway to trigger it.

οΏ½οΏ½ Clean Up

ps aux | grep python
kill -9 <PID>  # If needed
docker ps
docker stop <container_id>

πŸ™Œ Done!

Your Flask app is now running in Docker on EC2, and optionally portable to Lambda.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published