Streamlit with Poetry is not found when run my docker container

huangapple go评论62阅读模式
英文:

Streamlit with Poetry is not found when run my docker container

问题

根据以下链接提供的信息:https://stackoverflow.com/a/57886655/15537469 和 https://stackoverflow.com/a/74918400/15537469,我使用Poetry和venv进行了多阶段的Docker构建。

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"]

尽管我的构建进展顺利,但在运行Docker容器时,我遇到了以下错误: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.

我不确定这是否真的有用,但这是我的文件结构:

mtcc
├── mtcc
│   └── app.py
└── Dockerfile                    
英文:

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                    

答案1

得分: 2

使用 poetry config virtualenvs.in-project true,poetry 将在 .venv 目录中创建一个虚拟环境,并安装所有依赖项。

通常的方法是在使用之前激活虚拟环境。通常使用 .venv/bin/activate(或使用 poetry run / poetry shell)来实现。例如,您可以执行以下操作:

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

另外,您也可以通过操作路径环境变量来激活虚拟环境。如果将虚拟环境的 bin 目录路径添加到路径中,Docker 环境将找到所有二进制文件:

ENV VIRTUAL_ENV=/app/.venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

CMD ["streamlit", "run", ...]

您可以在 https://pythonspeed.com/articles/activate-virtualenv-dockerfile/ 找到这些方法以及详细的解释。

英文:

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/.

答案2

得分: 1

Here is the translated content from the provided text:

这是我找到的解决方案:

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
ENTRYPOINT [".venv/bin/python", "-m", "streamlit", "run", "mtcc/app.py", "--server.port=8501", "--server.address=0.0.0.0"]

我觉得我在我发布的示例帖子中误导了自己。
但是当我重新阅读Docker文档时
> CMD的主要目的是为正在运行的容器提供默认值。这些默认值可以包括可执行文件,也可以省略可执行文件,此时您必须同时指定ENTRYPOINT指令。

这就是我想做的:提供默认值

之后
> 如果使用CMD为ENTRYPOINT指令提供默认参数,则应以JSON数组格式同时指定CMD和ENTRYPOINT指令。

CMD用于为ENTRYPOINT提供默认参数

在我的情况下,这不是一个参数,我认为这是

英文:

This is the solution what I found:

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
ENTRYPOINT [".venv/bin/python", "-m", "streamlit", "run", "mtcc/app.py", "--server.port=8501", "--server.address=0.0.0.0"]

I think I misled myself with the example posts I put up.
However when I reread the docker documentation
> The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

That's what I want to do: provide default values

and after
> If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.

CMD is used to provide default arguments for the ENTRYPOINT

And in my case it is not an argument, I think

huangapple
  • 本文由 发表于 2023年5月15日 05:03:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定