AI Deploy - Tutorial - Build & use a Streamlit image

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

Last updated 31st January, 2023.

AI Deploy is in beta. During the beta-testing phase, the infrastructure’s availability and data longevity are not guaranteed. Please do not use this service for applications that are in production, as this phase is not complete.

AI Deploy is covered by OVHcloud Public Cloud Special Conditions.

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 - On the use a custom Docker image for a 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 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 app 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.

Please make sure that the docker image you will push in order to run containers using AI products respects the linux/AMD64 target architecture. You could, for instance, build your image using buildx as follows:

docker buildx build --platform linux/amd64 ...

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 Deploy apps. 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 Deploy should only be use for testing purposes. Please consider attaching your own docker registry. More information about this can be found here.

Find the address 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 AI Deploy app

The following command starts a new app running your Streamlit application:

ovhai app 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 app URL is the 8501.

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

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

Once the AI Deploy app is running you can access your Streamlit application directly from the app's URL.

image

Go further

  • Do you want to use Streamlit to deploy an AI model for audio classification task? Here it is.
  • You can imagine deploying an AI model with an other tool: Flask. Refer to this tutorial.

Feedback

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


Questa documentazione ti è stata utile?

Prima di inviare la valutazione, proponici dei suggerimenti per migliorare la documentazione.

Immagini, contenuti, struttura... Spiegaci perché, così possiamo migliorarla insieme!

Le richieste di assistenza non sono gestite con questo form. Se ti serve supporto, utilizza il form "Crea un ticket" .

Grazie per averci inviato il tuo feedback.


Potrebbero interessarti anche...

OVHcloud Community

Accedi al tuo spazio nella Community Fai domande, cerca informazioni, pubblica contenuti e interagisci con gli altri membri della Community OVHcloud

Discuss with the OVHcloud community

Conformemente alla Direttiva 2006/112/CE e successive modifiche, a partire dal 01/01/2015 i prezzi IVA inclusa possono variare in base al Paese di residenza del cliente
(i prezzi IVA inclusa pubblicati includono di default l'aliquota IVA attualmente in vigore in Italia).