How to deploy Streamlit applications using Docker & AWS?

Let’s learn to deploy your Streamlit App using Docker, AWS ECR and EC2.

Dhruv Tyagi
The Streamlit Teacher
6 min readJan 14, 2024

--

Introduction

So you finally made an amazing streamlit app and you make the decision to launch your application. This blog explains how to deploy your app using Docker and AWS.

But Why to use Docker?

Docker is a platform that helps you build, run, and ship applications in a seamless and error-free way.

You’ve likely come across a scenario where the code is running on your machine, but is somehow throwing errors on someone else’s machine.

Well, Docker was created to solve this very problem.

What are the various Steps?

The main idea is to deploy the application using Amazon Web Services (AWS) and its cloud based products. In order to have a fully functioning application on the cloud, these are the steps to be done:

Create a Docker Image:

  • Make a Dockerfile to define your application’s environment and dependencies.
  • Build the Docker image using the file and test it locally.

Store the Docker Image on Amazon Elastic Container Registry (ECR):

  • Set up an Amazon ECR repository to store your Docker image.
  • Authenticate Docker to your ECR and push the image to the repository.

Deploy my container image to Amazon EC2 for everyone to use and enjoy:

  • Launch an EC2 instance with the desired AMI and required IAM role permissions.
  • Install Docker on the EC2 instance.
  • Pull your Docker image from ECR and run it on the EC2 instance.
  • Configure security groups, expose necessary ports, and optionally set up auto-restart.

Let’s do these steps.

Creating a Docker Image

A Docker Image is a highly efficient, and convenient way to package everything you need to run your software: from code, libraries, configuration files, an other variables — and then distribute that package, and run the software in a containerized environment. It saves up space, a lot of it. And unnecessary error handling headaches.

Creating a Docker Image is preceded by creating a set of instructions in a .txt file called the Dockerfile. The steps in creating a Dockerfile are very easy to follow, and can also be found on the Streamlit website: deploy docker. By following those instructions, we got the following Dockerfile:

FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt ./requirements.txt

RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
netbase \
&& rm -rf /var/lib/apt/lists/*

RUN pip3 install -r requirements.txt

EXPOSE 8501

COPY . .

ENTRYPOINT ["streamlit", "run"]

CMD ["main.py"]

After creating the Dockerfile, we run the following command on the terminal in order to create a Docker Image (make sure you run the command from the app folder). The name of the app is optional.

docker build -t my_streamlit_app .

After a minute a two, it is done! Your Docker Image is created, you can check the image by going to Docker Desktop, or just running the following command in the terminal:

docker image ls

You can now containerize it, and run it locally to check if everything is in order:

docker run -p 8501:8501 <your_streamlit_app>

Check out your Docker Desktop, there you will find the newly created image of your app, and it says it is in use. Click on it, there you will see the port 8501:8501 which then will take you to your application.

Pushing the Docker Image to AWS ECR

This part technically requires 2 steps:

  1. Creating a repository for your application
  2. Pushing the Docker Image to that repository
AWS ECR Repository

Creating a repository for your application is simple. Login to your AWS account, go to ECR, and create a private repository.

However, pushing your Docker Image to the repository requires additional steps:

  1. Install AWS CLI on your machine. With this handy tool you will be able to control multiple AWS services from the terminal
  2. Go to your IAM management, and create a new user which will only be used for out project

3. In the Permission section choose Attach existing policies directly and in the below list select

AmazonEC2ContainerRegistryFullAccess.

4. Once the user has been created, select it and go to Access Key. There you will be able to retrieve your Access Key and Secret Access Key — which are not to be shared. Keep these at a safe place.

5. In the terminal type the following:

aws configure

It will prompt you with four questions:

  • Enter the Access Key you just got
  • Enter the Secret Access Key
  • Enter your region name
  • Default output format (left it blank)

6. Login to your user through the terminal. There is a handy little shortcut in the upper right corner of your repository View Push Commands — it shows you already created links which you can just copy and paste to your terminal:

View Push Commands for your AWS Repo for macOS / Linux
View Push Commands for your AWS Repo for Windows

First one is authentication. We already have our image so we can skip the second command. The third one tags your Docker Image, and the fourth command pushes your image to the repository.

There we have it. Let’s now proceed to deploy our app.

Deploying the application

The application will be deployed using the Amazon ECS. These are steps to follow:

  1. Create an ECS Cluster which represents the group of your container instances which will be used to run tasks we will define. I created a cluster using the EC2 Linux + Networking Template.
  2. In the Cluster Configuration section you need to define your cluster name, and select your instance type (I chose the t2.micro). Use the default VPC from the drop down menu, and add a subnet (I used us-east-1a). Make sure to enable the Auto assign public IP, and choose a default security group. Now just create the cluster.
  3. Once your cluster has been created, choose the Create Task Definition option, and select EC2. Enter the name of the task you wish. The following options are to select the task memory and task CPU (my choices were 100MiB, and 1 vcpu)
  4. Add a container to your task: select a name you wish, and paste the Image URI into the Image box. It’s important to select the port mapping as the ones you used in your Dockerfile. Once finished, click add, and create task.
  5. Now we need to run the task by going to our cluster, and selecting the Run Task option. Launch tpe should be set to EC2, and task definition as previously created, as well as the cluster you created. Now we just run the task.
  6. Check the EC2 instances that are running.
  7. In order to run the application, copy the Public IPv4 address, and add the :port_number you entered when creating the task definition.
  8. If the application is now showing, you can adjust the Security inbound settings of your instance, the option next to Details.

Add your own custom rules, with the port you previously selected, and add both IPv4 and IPv6.

9. Run the application just as before, and enjoy!

Happy shipping! 🐳🚢

--

--