Search code examples
pythonpython-3.xdockerfilestreamlitpython-poetry

Streamlit with Poetry is not found when run my docker container


Solved

According to this: https://stackoverflow.com/a/57886655/15537469

and to this: https://stackoverflow.com/a/74918400/15537469

I make a Multi-stage Docker build with Poetry and venv

FROM python:3.10-buster as py-build

RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    software-properties-common \
    && rm -rf /var/lib/apt/lists/*

RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python3 -

COPY . /app
WORKDIR /app
ENV PATH=/opt/poetry/bin:$PATH
RUN poetry config virtualenvs.in-project true && poetry install

FROM python:3.10-slim-buster

EXPOSE 8501
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
COPY --from=py-build /app /app
WORKDIR /app
CMD ./.venv/bin/python
ENTRYPOINT ["streamlit", "run", "mtcc/app.py", "--server.port=8501", "--server.address=0.0.0.0"]

My build is going well but when I run my docker container I get this error: docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "streamlit": executable file not found in $PATH: unknown.

I don't know if this is really useful but here is my file structure

mtcc
├── mtcc
│   └── app.py
└── Dockerfile                    

Solution

  • With poetry config virtualenvs.in-project true poetry will create a virtual environment in the .venv directory and install all it's dependencies in it.

    The typical approach is to activate a virtual environment before using it. Typically this is done with the .venv/bin/activate (or with poetry run / poetry shell). E.g. you could do something like:

    CMD source /app/.venv/bin/activate && exec streamlit run ...
    

    Alternatively you can also activate a virtual environment by manipulating the path environment variable. If you prepend the path to the bin directory of the virtual env, the Docker environment will find all binaries:

    ENV VIRTUAL_ENV=/app/.venv
    ENV PATH="$VIRTUAL_ENV/bin:$PATH"
    
    CMD ["streamlit", "run", ...]
    

    You can find these methods, and extended explanations at https://pythonspeed.com/articles/activate-virtualenv-dockerfile/.