Dockerfile:
FROM python:3.12-bookworm
# Configure Poetry
ENV POETRY_VERSION=1.8.1
ENV POETRY_HOME=/opt/poetry
ENV POETRY_VENV=/opt/poetry-venv
ENV POETRY_CACHE_DIR=/opt/.cache
# Install Poetry isolated from the system
RUN python -m venv $POETRY_VENV \
&& $POETRY_VENV/bin/pip install -U pip setuptools \
&& $POETRY_VENV/bin/pip install poetry==${POETRY_VERSION}
# Add Poetry to PATH
ENV PATH="${PATH}:${POETRY_VENV}/bin"
WORKDIR /usr/src/app
COPY poetry.lock pyproject.toml ./
# Configure Poetry to install project dependencies system-wide
RUN poetry env use system && poetry install
COPY . .
EXPOSE 8000
CMD ["uvicorn", "djadja.asgi:application"]
When I do docker run -it bash ... and inspect the container, everything looks and works exactly as expected (pip list, poetry env info, which uvicorn).
But when I run with a simple compose.yml (nothing there, just build .) it outputs the error:
web-1 | Creating virtualenv djadja-VA82Wl8V-py3.12 in /opt/.cache/virtualenvs
web-1 | Command not found: uvicorn
I've tried:
poetry config virtualenvs.create false
poetry config virtualenvs.in-project false
CMD ["/usr/local/bin/python", "-m", "uvicorn", ...]
CMD ["poetry", "run", "uvicorn", ...]
Please don't suggest workarounds like installing poetry directly to the system, using venv for the project dependencies, or using multi-stage builds.
Assuming that your project looks like this:
Dockerfile(Not convinced that we need a virtual environment in the image, so going without! See below for implementation using virtual environment.)Not sure what your
pyproject.tomlhas in it, so using--no-rootoption topoetryto install dependencies.pyproject.tomlDockerfile(Using virtual environment.)