Skip to content

Deploy multiple streamlit app containers on ec2 instance and load balance them using nginx

Notifications You must be signed in to change notification settings

tharuntejad/Streamlit-Nginx-Docker

Repository files navigation

Deploying a Streamlit Application with Docker Containers and Nginx Load Balancing on EC2

Introduction:

Modern application deployment techniques have revolutionized the way we bring software to life. In this guide, we'll explore a powerful deployment strategy that combines the flexibility of Docker containers with the efficiency of Nginx load balancing on an EC2 instance. By the end of this article, you'll be well-equipped to launch your Streamlit application with confidence, ensuring reliability and scalability.


Section 1: Configuring EC2 for Docker Deployment

SSH into the EC2 Instance

  1. Open your terminal.
  2. Establish an SSH connection to your EC2 instance:
ssh -i /path/to/your/keyfile.pem ubuntu@your_ec2_public_ip

Updating Ubuntu

  1. Begin by updating the package list and upgrading existing packages:
sudo apt update
sudo apt upgrade -y

Installing Docker Engine

The following steps will guide you through setting up the Docker engine and Docker Compose on your Ubuntu server. You can also find these steps in Docker's official documentation here.

  1. Remove any conflicting packages:
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
  1. Update the apt package index and install necessary packages for accessing repositories over HTTPS:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
  1. Add Docker's official GPG key:
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
  1. Set up the Docker repository:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo $VERSION_CODENAME) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. Update the apt package index:
sudo apt-get update
  1. Install Docker and Docker Compose:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  1. Confirm the successful installation of Docker Engine by running the hello-world image:
sudo docker run hello-world

Section 2: Moving Code to the Remote Server

Using FileZilla (Windows)

  1. Download and install FileZilla.
  2. Launch FileZilla and input your EC2 instance details (hostname, username, and keyfile).
  3. Connect to the EC2 instance and navigate to the desired transfer directory.
  4. Drag and drop your project folder into the remote directory.

Using SFTP (Linux/MacOS)

  1. Open your terminal.
  2. Connect to your EC2 instance using the sftp command:
sftp -i /path/to/your/keyfile.pem ubuntu@your_ec2_public_ip
  1. Navigate to the remote directory:
cd /path/to/remote/directory
  1. Upload your project folder using the put command:
put -r /path/to/your/local/project

(Note: If you encounter file access issues during transfer, ensure the appropriate permissions by running the below commands)

sudo chown ubuntu: /home/ubuntu/path_to_project
sudo chmod u+w /home/ubuntu/path_to_project

Section 3: Building Docker Image

Building the Docker Image

  1. SSH into your EC2 instance as previously demonstrated.
  2. Navigate to your project directory:
cd /path/to/your/project
  1. Build the Docker image using the provided Dockerfile:
docker build -t your_app_image_name .

Section 4: Running Docker Compose

Creating docker-compose.yml

  1. Create a docker-compose.yml file in your project directory.
  2. Copy the contents of the provided docker-compose.yml into this file.

Creating an nginx.conf

  1. Create an nginx.conf file in your project directory (adjacent to docker-compose.yml).
  2. Copy the contents of the provided nginx.conf into this file.

Running Docker Compose

  1. SSH into your EC2 instance.
  2. Navigate to your project directory:
cd /path/to/your/project
  1. Run the Docker Compose command:
docker-compose up -d

Section 5: Viewing the Application

Modifying Inbound Security Rules of the Instance

To view our application, we need to open a TCP port (port 80 in our case) in our instance's firewall:

  1. Navigate to the instance's Security Group Settings.
  2. Add an inbound rule of type TCP over port 80 and select the CIDR range to provide access to (typically '0.0.0.0/0').

Viewing Our App

Once the inbound rules of our instance have been adjusted, you can directly access the app using the URL: http://instance_public_ipv4.


By following this comprehensive guide, you've learned how to set up an EC2 instance, transfer your code, create Docker images, and deploy your Streamlit application using Docker containers and Nginx load balancing. Make sure to replace placeholders with actual values relevant to your setup, and ensure proper permissions and security configurations are in place for your EC2 instance. This deployment technique offers isolation, scalability, and portability, streamlining your application deployment process.

Notes:

  • If you're using PowerShell 7 or above, you can take advantage of SSH and SFTP directly in your terminal, eliminating the need for external tools like FileZilla(for SFTP) or Putty(for SSH).

Read the readme2.md for serving this application over https(using ssl ) rather than http

About

Deploy multiple streamlit app containers on ec2 instance and load balance them using nginx

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published