Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install local packages using pip as part of a docker build?

Tags:

python

docker

pip

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?

like image 979
AnjoMan Avatar asked Jun 22 '17 20:06

AnjoMan


People also ask

Can pip install local packages?

Install the downloaded package into a local directory : python get-pip.py --user This will install pip to your local directory (. local/bin) . Now you may navigate to this directory (cd . local/bin) and then use pip or better set your $PATH variable this directory to use pip anywhere : PATH=$PATH:~/.

How do I install packages inside a container?

After you have installed docker on your linux machine, the next step is to create an image and run a container. You need to create a base image of an OS distribution and after that you can add and modify the base image by installing packages and dependencies and committing the changes to it.


1 Answers

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 cant 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

like image 192
Cleared Avatar answered Oct 08 '22 16:10

Cleared