I have a fastapi app that is built from two repos using Poetry. Here is an example of how it runs within the terminal. Note data_sources is a dependency for api. The api .toml references the dist/*.whl in data_sources.
$ cd data_sources
$ poetry install
$ poetry build
$ cd ..
$ cd api
$ poetry install
$ poetry build
$ uvicorn api:app
I am wanting to create a Dockerfile that produces the same procedure, but am receiving this error during docker build .
0.597 File "/opt/poetry/venv/lib/python3.12/site-packages/poetry/core/utils/_compat.py", line 8, in <module>
0.597 import six.moves.urllib.parse as urllib_parse
0.597 ModuleNotFoundError: No module named 'six.moves'
------
Dockerfile:35
--------------------
33 | WORKDIR /app/api
34 | COPY api/pyproject.toml api/poetry.lock ./
35 | >>> RUN poetry install
36 | RUN poetry build
37 |
--------------------
I have tried may ways to dockerize, but here is my latest Dockerfile.
FROM python:3.12-slim AS data_sources_builder
ENV POETRY_HOME=/opt/poetry \
POETRY_VERSION=1.1.11 \
PATH="/opt/poetry/bin:$PATH"
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl \
build-essential \
&& rm -rf /var/lib/apt/lists/*
RUN curl -sSL https://install.python-poetry.org | python3 -
WORKDIR /app/data_sources
COPY data_sources/pyproject.toml data_sources/poetry.lock ./
RUN poetry install
RUN poetry build
FROM python:3.12-slim AS api_builder
ENV POETRY_HOME=/opt/poetry \
POETRY_VERSION=1.8.2 \
PATH="/opt/poetry/bin:$PATH"
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl \
build-essential \
&& rm -rf /var/lib/apt/lists/*
RUN curl -sSL https://install.python-poetry.org | python3 -
WORKDIR /app/api
COPY api/pyproject.toml api/poetry.lock ./
RUN poetry install
RUN poetry build
FROM python:3.12-slim
COPY --from=data_sources_builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=api_builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
WORKDIR /app/api
COPY . .
EXPOSE 8000
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]
It's not clear where the dependency on
six.movesis coming from. Presumably it's listed as a dependency somewhere in your project. Thesixpackage is to facilitate compatibility between Python 2 and Python 3. Where are you using it?Assuming that your project looks something like this:
Then this
Dockerfileshould work.The
pyproject.tomlfile inapi/has: