Home > Software engineering >  Poetry is being overwritten in docker and I can't see why - can you spot problem?
Poetry is being overwritten in docker and I can't see why - can you spot problem?

Time:01-19

Here's the error after I try to run poetry directly (I gave up on PATH whilst troubleshooting) in a docker container:

Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "~/.local/share/pypoetry/venv/bin/poetry": stat ~/.local/share/pypoetry/venv/bin/poetry: no such file or directory: unknown

Dockerfile:

FROM python:3.10

# to run poetry directly as soon as it's installed
ENV PATH="/root/.local/bin:$PATH"

# INSTALL POETRY
RUN apt-get update \
    && apt-get install -y curl \
    && curl -sSL https://install.python-poetry.org | python3 - 

# set work directory
WORKDIR /app

# Install dependencies
COPY poetry.lock pyproject.toml /app/
RUN ~/.local/share/pypoetry/venv/bin/poetry install --no-root

# copy files to Container
COPY ./. /app
CMD ["~/.local/share/pypoetry/venv/bin/poetry", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

docker-compose.yml

# Docker compose file for nginx and letsencrypt
version: "3.10"
services:
  fastapi:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: fastapi
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - .:/app

What's strange is, poetry will actually build and install all those dependencies first, I watch them get instaled. Then when I try and use poetry at the cmd line, you the build errors out producing the error above. Suddently by that point in the build, the poetry installation is gone?

CodePudding user response:

The $HOME directory is the working directory that you set. So when you execute RUN ~/.local/share/pypoetry/venv/bin/poetry the full path is app/.local/share/pypoetry/venv/bin/poetry when it should be root/.local/share/pypoetry/venv/bin/poetry.

Either define the full path or set the root dir as working directory

CodePudding user response:

You're using the JSON-array form of CMD. This doesn't run a shell and doesn't to any sort of variable expansion. In particular, ~ referring to the current user's home directory is a convenience offered by the shell, but since you're not running a shell here, the system instead interprets it as looking for a subdirectory of the current directory literally named ~.

The shortest solution is to spell out the absolute path here

CMD ["/root/.local/share/pypoetry/venv/bin/poetry", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
#     ^^^^^ explicitly spell out user's home directory

You try to set $PATH earlier, and another approach here could be to add the Poetry virtual environment to $PATH.

ENV PATH="/root/.local/bin:/root/.local/share/pypoetry/venv/bin:$PATH"
#                      add ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

...

RUN poetry install --no-root
CMD ["poetry", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

It looks like it might be possible to install Poetry using pip in which case you could install it outside a virtual environment, or poetry export your project's dependencies to a requirements.txt file that you could more directly RUN pip install -r into the image's "system" Python.

  • Related