0

I have a docker-image in which I want to install my own package, mypackage, using the .toml and .lock file from poetry - inspired by this SO answer.

It seems to work fine, all the dependencies in the .toml are being installed but the package itself is not.

If I add an RUN poetry add mypackage I get an error Package 'mypackage' is listed as a dependency of itself. If I don't I get a ModuleError: No module named 'mypackage'. If I spawn er terminal in the container, I can pip list the dependencies, but not mypackage

I have also tried copying mypackage/ into my workdir (scripts/) in the Dockerfile but the same issue persists. Maybe there's some file-structure that's wrong? I'm not very experience with Docker, thus some of the files might not be where I think they are.

# pyproject.toml

[tool.poetry]
name = "mypackage"
version = "1"
description = ""
authors = ["me"]
readme = "README.md"

[tool.poetry.dependencies]
python = "~3.10"
waitress = "2.1.2"
flask = "2.2.2"
scikit-learn = "1.2.1"
cython = "^0.29.33"
gensim = "^4.3.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

and

FROM python:3.10-slim-bullseye

RUN apt update && apt install
RUN apt install python3-pip -y

RUN pip install "poetry==1.3.1"

RUN mkdir /scripts

COPY poetry.lock pyproject.toml /scripts/
# COPY mypackage/ /scripts/ 
WORKDIR /scripts

RUN poetry config virtualenvs.create false \
  && poetry install --no-dev --no-interaction --no-ansi

# RUN poetry add mypackage

CMD poetry run python -c "import mypackage;"
CutePoison
  • 4,679
  • 5
  • 28
  • 63
  • You do not copy the source code of your package to the docker container, do you? From the dockerfile, it seems that you are only copying the pyproject.toml and poetry.lock file. I.e. the information about the dependencies of your package. Not the package itself. – MSpiller Feb 27 '23 at 11:53
  • I have tried to do it ( the `# COPY mypackage/ /scripts/`) but it does not seem to make any difference (of course after removing the "#") – CutePoison Feb 27 '23 at 11:57
  • 1
    AFAIK `COPY mypackage/ ...`will copy the contents of the directory, not the directory itself. Could you verify that the directory layout in the container is as expected? I.e. that directory `mypackage` is not missing. – MSpiller Feb 27 '23 at 12:02
  • Argh, you are 100% correct! What a stupid typo; `COPY mypackage/ /scripts/mypackage ` is the correct command – CutePoison Feb 27 '23 at 12:08

0 Answers0