Baily
Baily

Reputation: 1380

Transfer virtualenv to docker image

Is it possible to transfer virtual environment data from a local host to a docker image via the ADD command?

Rather than doing pip installs inside the container, I would rather the user have all of that done locally and simply transfer the virtual environment into the container. Granted all of the files are the same name locally as in the docker container, along with all directories being nested properly.

This would save minutes to hours if it was possible to transfer virtual environment settings into a docker image. Maybe I am thinking about this in the wrong abstract.

It just feels very inefficient doing pip installs via a requirements.txt that was passed into the container, as opposed to doing it all locally, otherwise each time the image is started up it has to re-install the same dependencies that have not changed from each image's build.

Upvotes: 11

Views: 8557

Answers (2)

Dmitry Platonov
Dmitry Platonov

Reputation: 121

While possible, it's not recommended.

  • Dependencies (library versions, globally installed packages) can be different on host machine and container.
  • Image builds will not be 100% reproducible on other hosts.
  • Impact of pip install is not big. Each RUN command creates it's own layer, which are cached locally and also in repository, so pip install will be re-run only when requirements.txt is changed (or previous layers are rebuilt).

To trigger pip install only on requirements.txt changes, Dockerfile should start this way:

... COPY requirements.txt ./ RUN pip install -r requirements.txt COPY src/ ./ ...

Also, it will be run only on image build, not container startup.

If you have multiple containers with same dependencies, you can build intermediate image with all the dependencies and build other images FROM it.

Upvotes: 2

Indradeep Biswas
Indradeep Biswas

Reputation: 86

We had run into this problem earlier and here are a few things we considered:

  1. Consider building base images that have common packages installed. The app containers can then use the one of these base containers and install the differential.
  2. Cache the Pip packages on a local path that can be mounted on the container. That'll save the time to download the packages.

Depending on the complexity of your project one may suit better than the other - you may also consider a hybrid approach to find maximum optimization.

Upvotes: 7

Related Questions