Using Streamlit + Nginx + Docker to build and put in production dashboards in AWS Lightsail

Daniel Sierra
4 min readSep 11, 2020

--

During the last few months, Streamlit has becoming one of the most famous tools to build dashboards and make quick prototypes. In this post, I am going to show how to put in production a Streamlit application in AWS Lightsail, one of the last services launched by AWS in which deploying web applications will be a piece of cake.

Step 1: Create a Lightsail instance

First of all, you will need an AWS account. Once in the main dashboard of AWS, type “lightsail” in the search box and select the Lightsail service.

AWS LightSail main dashboard

Now click “Create Instance” and select the most suitable instance region, type and OS for you. In my case, I will choose a Linux/Unix instance with a preinstalled Ubuntu 20.04 distro.

Create your instance in AWS Lightsail

Now the instance can be accessed by SSH through the web terminal provided by Lighsail or with any SSH client like Putty or MobaXterm (Windows).

Step 2: Build your Streamlit application

Streamlit is a very versatile Python library to build fast prototypes entirely in Python programming language. Regarding the field of Data Science, Streamlit turns out especially attractive due to all its capabilities to build lightweight and professional-looking dashboards.

First of all, create the project structure by creating the root folder (helloworld-st-app in my case) and the subfolder app. Inside app folder, create the Python file app.py with the main Streamlit code and the requirements.txt file.

helloworld-st-app/
|-- app/
|-- app.py
|-- requirements.txt

The code in the app.py file is simply a Hello World! message when the application starts.

Now, to test this application locally, simply have to type on your terminal:

streamlit run app/app.py

Then, check your web browser on localhost:8501

Step 3: Add Nginx as our HTTP serve and reverse proxy

Nginx is a famous HTTP server and reverse proxy tool, and in this case, we’ll use it to serve our application through our public IP provided by AWS Lighsail service.

To configure Nginx just create a new folder called nginx in the project root folder and create a project.conf file inside.

helloworld-st-app/
|-- app/
|-- app.py
|-- requirements.txt
|-- nginx
|-- project.conf

The project.conf file contains all server configuration to publish our application and make it reachable through the Internet.

First, we are defining the listening port for the application (default HTTP port is 80) and the server name. The rest of the directives tells Nginx how to redirect queries to all specific locations in the application. In the case of a Streamlit app, all these locations must be established to let the application work in the remote server.

Step 4: Time to Dockerize

We are defining two containers, one with the Streamlit app and the other with the Nginx server. To do so we just create a proper Dockerfile to define all operations to build each container. The project structure is as follows:

helloworld-st-app/
|-- app/
|-- app.py
|-- requirements.txt
|-- Dockerfile
|-- nginx
|-- project.conf
|-- Dockerfile

Step 4.1: Dockerize Streamlit app

Just write the Dockerfile associated to the Streamlit app. The file should be as follows

First, a Python 3.6 image is imported and then all libraries defined in the requirements.txt file are installed. Finally, all files are copied into de container.

Step 4.2: Dockerize Nginx

In this case the Dockerfile is really simple. We just need to import the Nginx image and copy the configuration file project.conf into the container

Step 5: Merge all with docker-compose

Create a new file in the root folder called docker-compose.yml

In this file, we define the two services included previously (Streamlit app + Nginx server). In the former, we are defining output ports (default for Streamlit is 8501) and the command to execute the application. In the latter, we are telling Docker to open port 80 to receive queries to our web app. Pay attention to the depends_on directive that tells Docker to wait until the app container is up. Also we include the restart: always option to keep containers running in case of eventual failure.

Here is the final structure

helloworld-st-app/
|-- app/
|-- app.py
|-- requirements.txt
|-- Dockerfile
|-- nginx
|-- project.conf
|-- Dockerfile
|-- docker-compose.yml

Step 6: Running application in AWS Lightsail

Now copy the code to AWS server in Lightsail and install Docker and docker-compose . Then run the following command to deploy your app.

docker-compose up --build -d

Then navigate to your public IP provided by AWS and the Streamlit app should appear.

Final considerations

Here I show the benefits of combining a powerful tool like Streamlit to build fast prototypes with the versatility od Docker to deploy your application everywhere. Once your application is dockerized it’s extremely easy to deploy in other cloud providers like Azure or GPC. Just copy the code and run docker-compose.

I hope you like this post!

--

--

Daniel Sierra

Artificial Intelligence expert, DeFi enthusiast and Avid Learner