How to use Streamlit in Docker

For anyone else who may run into the same issue, here was my solution:

I was not able to get my conda environment working with Docker, so I resorted to using pip with the following requirements.txt file:

altair==3.3.0
joblib==0.13.2
pandas==0.25.1
numpy==1.17.2
scikit-learn==0.21.3
matplotlib==3.1.1
streamlit==0.51.0

Then, I created a simpler Dockerfile, inspired by this blog:

# base image
FROM python:3.7

# streamlit-specific commands
RUN mkdir -p /root/.streamlit
RUN bash -c 'echo -e "\
[general]\n\
email = \"\"\n\
" > /root/.streamlit/credentials.toml'
RUN bash -c 'echo -e "\
[server]\n\
enableCORS = false\n\
" > /root/.streamlit/config.toml'

# exposing default port for streamlit
EXPOSE 8501

# copy over and install packages
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt

# copying everything over
COPY . .

# run app
CMD streamlit run webapp/app.py

Finally, I built the image with docker image build -t st:app ., then ran the container with docker container run -p 8501:8501 st:app. After that, I could access my streamlit app at http://localhost:8501/

5 Likes