My customer accountContact commercialWebmailOVHcloud Blog

Welcome to OVHcloud.

Log in to order, manage your products and services, and track your orders

Log in

Build & use a Streamlit image

How to build and use a custom Docker image containing a Streamlit application

Last updated 5th of June, 2021.

Objective

Streamlit is a python framework that turns scripts into shareable web application.

The purpose of this tutorial is to provide a concrete example on how to build and use a custom Docker image for Streamlit applications.

Requirements

Instructions

Write a simple Streamlit application

Create a simple python file with name simple_app.py.

Inside that file, import your required modules:

import streamlit as st
import pandas as pd

Display all information you want on your Streamlit application:

st.title('My first app')
st.write("Here's our first attempt at using data to create a table:")
st.write(pd.DataFrame({
    'first column': [1, 2, 3, 4],
    'second column': [10, 20, 30, 40]
}))
  • More information about Streamlit capabilities can be found here
  • Direct link to the full python file can be found here here

Write the the Dockerfile for your application

Your Dockerfile should start with the the FROM instruction indicating the parent image to use. In our case we choose to start from a classic python image.

FROM python:3.8

Install your needed python module using a pip install ... command. In our case we only need these 2 modules:

  • streamlit
  • pandas
RUN pip install streamlit pandas

Install your application inside your image. In our case, we just copy our python file inside the /opt directory.

COPY simple_app.py /opt/simple_app.py

Define your default launching command to start the application:

CMD [ "streamlit" , "run" , "/opt/simple_app.py", "--server.address=0.0.0.0" ]

In order to access the job from the outside world, don't forget to add the --server.address=0.0.0.0 instruction on your streamlit run ... command. By doing this you indicate to the process that it have to bind on all network interfaces and not only the localhost.

Create the home directory of the ovhcloud user (42420:42420) and give it correct access rights:

RUN mkdir /workspace && chown -R 42420:42420 /workspace
ENV HOME /workspace
WORKDIR /workspace

This last step is mandatory because streamit needs to be able to write inside the HOME directory of the owner of the process in order to work properly.

  • More information about Dockerfiles can be found here
  • Direct link to the full Dockerfile can be found here here

Build the docker image from the dockerfile

Launch the following command from the Dockerfile directory to build your application image.

docker build . -t streamlit-example:latest

The dot . argument indicates that your build context (place of the Dockerfile and other needed files) is the current directory.

The -t argument allow you to choose the identifier to give to your image. Usually image identifiers are composed of a name and a version tag <name>:<version>. For this example we chose streamlit-example:latest.

Test it locally (optional)

Launch the following docker command to launch your application locally on your computer:

docker run --rm -it -p 8501:8501 --user=42420:42420 streamlit-example:latest

The -p 8501:8501 argument indicates that you want to execute a port rediction from the port 8501 of your local machine into the port 8501 of the docker container. The port 8501 is the default port used by streamlit applications.

Don't forget the --user=42420:42420 argument if you want to simulate the exact same behavior that will occur on AI TRAINING jobs. It executes the docker container as the specific OVHcloud user (user 42420:42420).

Once started, your application should be available on http://localhost:8501.

Push the image into the shared registry

The shared registry of AI Training should only be use for testing purposes. Please consider attaching your own docker registry. More information about this can be found here.

Find the adress of your shared registry by launching this command:

ovhai registry list

Login on the shared registry with your usual openstack credentials:

docker login -u <user-password> -p <user-password> <shared-registry-address>

Push the compiled image into the shared registry:

docker tag streamlit-example:latest <shared-registry-address>/streamlit-example:latest
docker push <shared-registry-address>/streamlit-example:latest

Launch the job

The following command starts a new job running your streamit application:

ovhai job run --default-http-port 8501 --cpu 1 <shared-registry-address>/streamlit-example:latest

--default-http-port 8501 indicates that the port to reach on the job URL is the 8501.

--cpu 1 indicates that we only request 1 CPU for that job.

Consider adding the --unsecure-http attribute if you want your application to be reachable without any authentication.

Once the job is running you can access your streamlit application directly from the job's URL.

image

Feedback

Please send us your questions, feedback and suggestions to improve the service:


Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.


These guides might also interest you...

OVHcloud Community

Access your community space. Ask questions, search for information, post content, and interact with other OVHcloud Community members.

Discuss with the OVHcloud community

In accordance with the 2006/112/CE Directive, modified on 01/01/2015, prices exclude VAT. VAT may vary according to the customer's country of residence.