glubokov
glubokov

Reputation: 59

No module error after docker-compose up with Python package from git

I have a problem with install python package (I will call this 'GITPACKAGENAME') from gitlab after running docker-compose up app.

Here is an example error log:

...
app_1        | ModuleNotFoundError: No module named 'GITPACKAGENAME'

Dockerfile

FROM python:3.8

RUN mkdir /app
WORKDIR /app
COPY . /app

RUN pip install pipenv
COPY Pipfile /app
COPY Pipfile.lock /app
RUN pipenv install --deploy --system

CMD python ./src/main.py

part of docker-compose.yml

...
services:
  app:
    build: .
    restart: always
    command: python ./src/main.py
    volumes:
      - .:/app
...

part of Pipfile:

[packages]
...
GITPACKAGENAME = {editable = true,git = "https://LOGIN:PASSWORD@gitlab/path/to/GITPACKAGENAME.git",ref = "develop"}
...

Also I tried to install package using pip, for example I added to Dockerfile:

...
RUN pip install git+https://LOGIN:PASSWORD@gitlab/path/to/GITPACKAGENAME.git@develop#egg=GITPACKAGENAME
...

I got the same error.

After this I tried cloning repo using Dockerfile commands:

...
RUN git clone https://LOGIN:PASSWORD@gitlab/path/to/GITPACKAGENAME.git
RUN git checkout develop
...

aaaaand I got same :'))

Every time I checked GITPACKAGENAME package in container bash using docker-compose run app /bin/bash, checked pip list and checked WORKDIR.

I have next staff:

Docker version 19.03.8-ce, build afacb8b7f0
docker-compose version 1.25.5, build unknown

  Operating System: Arch Linux
            Kernel: Linux 5.6.11-arch1-1
      Architecture: x86-64

Upvotes: 1

Views: 2278

Answers (1)

larsks
larsks

Reputation: 311238

The problem is here:

    volumes:
      - .:/app

When you run pipenv install --system --deploy, the pipenv command installs your modules into src/<modulename>. When you bind-mount your local directory on top of /app, you mask the underlying files so the module is no longer available.

If you want to keep my bind mount, you can restructure things like this:

  • Stop installing your module from git with editable = True.
  • This means you can't install with --system anymore, which means
  • You probably want to use pipenv run in your Dockerfile.

In other words, modify your Pipfile so that it looks like this:

[packages]
...
GITPACKAGENAME = {git = "https://LOGIN:PASSWORD@gitlab/path/to/GITPACKAGENAME.git",ref = "develop"}
...

And make your Dockerfile look like this:

FROM python:3.8

RUN pip install pipenv

COPY . /app
WORKDIR /app
RUN pipenv install --deploy

CMD pipenv run python ./src/main.py

With those changes, I'm able to run something that looks like your docker-compose.yml file.


Update

This also works fine if I just use pip instead of pipenv. That is, if I have a requirements.txt that looks like this:

git+https://example.com/my/module

And a Dockerfile that looks like this:

FROM python:3.8

COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt

CMD python ./src/main.py

Then your docker-compose.yml works just fine, because pip doesn't require any store in the local directory, so the fact that you're masking it with a bind mount at runtime doesn't cause an issue.

(You can find my complete test in the using-pip branch of that example repository.)

Upvotes: 1

Related Questions