How to create a Dockerfile for streamlit app

Question:

I am aiming to deploy a web-app written with Sreamlit,

File Structure

I can run this on my local machine by running streamlit run Home.py in my command line.

However, I’m not sure how to create the docker file.

Any advice?

Asked By: Fariya

||

Answers:

Try something like this.

FROM python:3.7

# Expose port you want your app on
EXPOSE 8080

# Upgrade pip and install requirements
COPY requirements.txt requirements.txt
RUN pip install -U pip
RUN pip install -r requirements.txt

# Copy app code and set working directory
COPY . .
WORKDIR /app

# Run
ENTRYPOINT [“streamlit”, “run”, “Home.py”, “–server.port=8080”, “–server.address=0.0.0.0”]
Answered By: Javier Roger

This is a docker file that would work for you:

FROM python:3.9
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
EXPOSE 8201
ENTRYPOINT ["streamlit", "run"]
CMD ["app/Home.py"]

This is a series of instructions and each instruction creates a layer. The base image here is python:3.9 where you create a directory called /app, then copy and install everything in your requirements.txt. Then copy your entire folder over and finally specify the command to run within the container. When you run the container it will be exposed on port 8201.

Build and run the container with the following if you are going for a local set up. Run the commands from within the directory your Dockerfile is.

docker build -f Dockerfile -t app:latest .
docker run -p 8201:8201 app:latest

Now you will be able to access the container on your browser at http://localhost:8201/

Answered By: viggnah

In order to Dockerise your app you need two files:

  1. Dockerfile : describes the image structure,
  2. docker-compose.yml : describes how to make a container from that image (Dockerfile)

The answer of Javier Roger is providing a very minimal Dockerfile that seems to work:

FROM python:3.7

# Expose port you want your app on
EXPOSE 8080

# Upgrade pip and install requirements
COPY requirements.txt requirements.txt
RUN pip install -U pip
RUN pip install -r requirements.txt

# Copy app code and set working directory
COPY . .
WORKDIR /app

# Run
ENTRYPOINT [“streamlit”, “run”, “Home.py”, “–server.port=8080”, “–server.address=0.0.0.0”]

If you combine this answer with the following docker-compose then you have a nice container containing your app:

services:
  streamlit:
    container_name: "The name you want your container to have"
    build:
      dockerfile: ./Dockerfile
      context: ./
    ports:
      - 'The port you want your app to have:8080'

Please note that both Dockerfile and docker-compose.yml should be placed in the root of your app.

To run the app just cd to the root dir and run:

docker compose up -d

if docker compose is not installed please use:

 sudo apt-get update
 sudo apt-get install docker-compose-plugin

In case that you cannot install it so easily use this link