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.
- Open your terminal.
- Establish an SSH connection to your EC2 instance:
ssh -i /path/to/your/keyfile.pem ubuntu@your_ec2_public_ip
- Begin by updating the package list and upgrading existing packages:
sudo apt update
sudo apt upgrade -y
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.
- Remove any conflicting packages:
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
- 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
- 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
- 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
- Update the apt package index:
sudo apt-get update
- Install Docker and Docker Compose:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- Confirm the successful installation of Docker Engine by running the hello-world image:
sudo docker run hello-world
- Download and install FileZilla.
- Launch FileZilla and input your EC2 instance details (hostname, username, and keyfile).
- Connect to the EC2 instance and navigate to the desired transfer directory.
- Drag and drop your project folder into the remote directory.
- Open your terminal.
- Connect to your EC2 instance using the
sftp
command:
sftp -i /path/to/your/keyfile.pem ubuntu@your_ec2_public_ip
- Navigate to the remote directory:
cd /path/to/remote/directory
- 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
- SSH into your EC2 instance as previously demonstrated.
- Navigate to your project directory:
cd /path/to/your/project
- Build the Docker image using the provided Dockerfile:
docker build -t your_app_image_name .
- Create a
docker-compose.yml
file in your project directory. - Copy the contents of the provided
docker-compose.yml
into this file.
- Create an
nginx.conf
file in your project directory (adjacent to docker-compose.yml). - Copy the contents of the provided
nginx.conf
into this file.
- SSH into your EC2 instance.
- Navigate to your project directory:
cd /path/to/your/project
- Run the Docker Compose command:
docker-compose up -d
To view our application, we need to open a TCP port (port 80 in our case) in our instance's firewall:
- Navigate to the instance's
Security Group Settings
. - 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').
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).