How to use Streamlit in Docker

For anyone who wants to host streamlit from a container, I believe that the following code must be included in every Dockerfile (not sure about the environment variables, that may have been specific to my app):

ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8

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'

EXPOSE 8501
2 Likes

Hi @timforr, thanks for posting. I have been using the same configuration flags in my cloud deployments:
config.toml:

[server]
enableCORS = false

credentials.toml:

[general]
email = $EMAIL

I think you can either use EXPOSE <port> in the Dockerfile or tell docker command line via the -p option.

Matteo

And for some docker files in production see

Hi @timforr and @Marc, I have a clarifying question for either of you. I am new to Docker and am trying to host my streamlit app in a container. I am having an issue, where when I try to run my docker container locally, it begins running, then almost immediately exits.

Here is my Dockerfile

# base of miniconda image
FROM continuumio/miniconda3

# 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

# copying all analysis code to image
COPY . .

# activate conda environment
SHELL ["/bin/bash", "-c", "source activate ./env"]

# run app
CMD streamlit run webapp/app.py

I build the image with docker image build -t st:app ., then attempt to run the container with docker run -p 8501:8501 st:app.

What steps would I need to change in order to then access my streamlit app at http://localhost:8501/? Thank you!

For the last line, try

CMD [ "streamlit", "run", "webapp/app.py" ]

Thanks for the response! I made that change and it gave me a more helpful error message:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"streamlit\": executable file not found in $PATH": unknown.
ERRO[0001] error waiting for container: context canceled

I suppose this means that my conda environment is not getting installed/activated properly. From what I understand, they can be tricky to configure with Docker. I’ll keep tinkering with it. Thank you!

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/

2 Likes