50

I've got a package that I want to build into a docker image which depends on an adjacent package on my system.

My requirements.txt looks something like this:

-e ../other_module
numpy==1.0.0
flask==0.12.5

When I call pip install -r requirements.txt in a virtualenv this works fine. However, if I call this in a Dockerfile, e.g.:

ADD requirements.txt /app
RUN pip install -r requirements.txt

and run using docker build . I get an error saying the following:

../other_module should either be a path to a local project or a VCS url beginning with svn+, git+, hg+, or bzr+

What, if anything, am I doing wrong here?

3
  • Do you have that other_module in the Docker image?
    – 9000
    Jun 22, 2017 at 20:10
  • Hade you added ../other_module to the docker image as well?
    – Cleared
    Jun 22, 2017 at 20:10
  • @9000 @Cleared I've tried to copy it using something like COPY ../other_module /app but that renders a different error: Forbidden path outside the build context
    – AnjoMan
    Jun 22, 2017 at 20:14

1 Answer 1

54

First of all, you need to add other_module to your Docker image. Without that, the pip install command will not be able to find it. However you can ADD a directory that is outside the directory of the Dockerfile according to the documentation:

The path must be inside the context of the build; you cannot ADD ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

So you have to move the other_module directory into the same directory as your Dockerfile, i.e. your structure should look something like

.
├── Dockerfile
├── requirements.txt
├── other_module
|   ├── modue_file.xyz
|   └── another_module_file.xyz

then add the following to the dockerfile:

ADD /other_module /other_module
ADD requirements.txt /app
WORKDIR /app
RUN pip install -r requirements.txt

The WORKDIR command moves you into /app so the next step, RUN pip install... will be executed inside the /app directory. And from the app-directory, you now have the directory../other_module avaliable

3
  • 1
    One thing I'd add to this is that with this approach, one does not need to edit their requirements.txt file -- when developing locally you can run pip install -e ../other_module/ to get your local copy of the library, and in the Docker file after copying you can just add the same command.
    – AnjoMan
    Jul 18, 2018 at 3:30
  • 20
    So if several projects have dependencies to other_module then all these projects need to make a copy of say ../other_module to ./other_module so it's available when building the docker image? Isn't there a better way (not involving copying for example)? Is a symlink sufficient for this to work?
    – cglacet
    Aug 28, 2020 at 8:16
  • 1
    @cglacet I'd be interested in knowing this too. At least, if you are working using Google Cloud Build or similars, you can publish the package in a registry and then pip install it remotely from the container.
    – Giuppox
    Jun 11 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.