Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Running a Streamlit App in Docker on AWS EC2

📜 Table of Contents

  1. 🛠 Setting Up a VPC, Subnet, Route Table, and Internet Gateway
  2. 🖥️ Launching and Configuring an EC2 Instance
  3. 🔗 Connecting to EC2 Instance via EC2 Instance Connect
  4. 🔑 Setting Permissions for the PEM Key
  5. 🐳 Installing and Configuring Docker on EC2
  6. 📂 Copying Project Files to EC2
  7. 🏗️ Building and Running the Docker Container
  8. 🌐 Accessing the Streamlit App
  9. 🔄 Managing the Docker Container

1️⃣ Setting Up a VPC, Subnet, Route Table, and Internet Gateway

To ensure a well-structured networking setup, follow these steps in the AWS Management Console.

1.1 Create a New VPC

  1. Navigate to AWS Console → VPC Dashboard.
  2. Click Create VPC.
  3. Enter:
    • Name: MyCustomVPC
    • IPv4 CIDR block: 10.0.0.0/16
  4. Click Create VPC.

Streamlit on AWS EC2

1.2 Create a Subnet

  1. Go to VPC Dashboard → Subnets → Create Subnet.
  2. Select MyCustomVPC.
  3. Enter:
    • Subnet name: MyPublicSubnet
    • CIDR block: 10.0.1.0/24
    • Availability Zone: Select any zone.
  4. Click Create Subnet.

Streamlit on AWS EC2

1.3 Enable Auto-Assign Public IPv4

  1. Go to Subnets.
  2. Select MyPublicSubnet.
  3. Click Actions → Edit Subnet Settings.
  4. Enable Auto-assign public IPv4 address.
  5. Click Save changes.

1.4 Create an Internet Gateway

  1. Go to VPC Dashboard → Internet Gateways.
  2. Click Create Internet Gateway.
  3. Name it MyIGW and click Create.
  4. Attach it to MyCustomVPC:
    • Select MyIGW.
    • Click Actions → Attach to VPC → Select MyCustomVPC.
    • Click Attach.

Streamlit on AWS EC2

1.5 Create a Route Table

  1. Go to VPC Dashboard → Route Tables.
  2. Click Create Route Table.
  3. Name it MyPublicRouteTable, select MyCustomVPC, and click Create.
  4. Select MyPublicRouteTable, go to the Routes tab.
  5. Click Edit routes → Add route:
    • Destination: 0.0.0.0/0
    • Target: Select MyIGW.
  6. Click Save changes.

1.6 Associate the Subnet with the Route Table

  1. Go to Route Tables.
  2. Select MyPublicRouteTable.
  3. Click Subnet Associations → Edit Subnet Associations.
  4. Select MyPublicSubnet and click Save.

2️⃣ Launching and Configuring an EC2 Instance

2.1 Create an EC2 Instance

  1. Go to EC2 Dashboard → Instances → Launch Instance.
  2. Configure:
    • Name: Streamlit-EC2
    • AMI: Select Amazon Linux 2023
    • Instance Type: t2.micro (Free Tier)
    • Key Pair: Create or use an existing key pair.
    • Network Settings:
      • VPC: Select MyCustomVPC.
      • Subnet: Select MyPublicSubnet.
      • Auto-assign public IP: Enabled.
    • Security Group:
      • Allow SSH (port 22).
      • Allow HTTP (port 80, optional).
      • Allow Streamlit (port 8501).
  3. Click Launch Instance.

Streamlit on AWS EC2

---

3️⃣ Connecting to EC2 Using EC2 Instance Connect

  1. Go to EC2 Dashboard → Instances.
  2. Select Streamlit-EC2.
  3. Click Connect.
  4. Choose EC2 Instance Connect.
  5. Click Connect (Opens a browser-based terminal).

4️⃣ Setting Permissions for the PEM Key

Move your .pem key to your work directory:

mv /path/to/your-key.pem ~/your-work-directory/

Or copy-paste it into your work directory.

Set the correct permissions:

chmod 600 your-key.pem

Streamlit on AWS EC2

5️⃣ Installing and Configuring Docker on EC2

Update packages:

sudo yum update -y

Install Docker:

sudo yum install -y docker

Streamlit on AWS EC2 - Step 2

Enable and start Docker:

sudo systemctl enable docker
sudo systemctl start docker

Streamlit on AWS EC2 - Step 3

6️⃣ Copying Project Files to EC2

Transfer files using SCP:

scp -i your-key.pem app.py Dockerfile requirements.txt mushroom.cv ec2-user@your-ec2-public-ip:/home/ec2-user/

7️⃣ Building and Running the Docker Container

Navigate to the directory:

cd /home/ec2-user/
<p align="center">
  <img src="https://github.com/anisha992/Docker-Projects/blob/main/12-Streamlit-App-AWS-EC2/images/Screenshot%202025-03-26%20001748.png" />
</p>

Build the Docker image:

sudo docker build -t streamlit-app .

Run the container:

sudo docker run -d -p 8501:8501 --name streamlit_container streamlit-app

Streamlit on AWS EC2 - Step 5

Streamlit on AWS EC2 - Step 5

8️⃣ Accessing the Streamlit App

Open your browser and go to:

http://your-ec2-public-ip:8501

The Streamlit app should now be accessible.

Streamlit on AWS EC2 - Step 6

9️⃣ Managing the Docker Container

Check running containers:

sudo docker ps

Stop the container:

sudo docker stop streamlit_container

Remove the container:

sudo docker rm streamlit_container

Restart the container:

sudo docker start streamlit_container

🎯 Conclusion

This documentation provides a step-by-step guide to:

✅ Set up a custom VPC, subnet, and Internet Gateway

✅ Launch an EC2 instance and enable IPv4 assignment

✅ Install Docker and deploy the Streamlit app in a Docker container

✅ Access the app via a public IP

This setup ensures scalability, security, and reliability for your project. 🚀

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages