Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 650
Baily Avatar asked Mar 14 '18 20:03

Baily


People also ask

Do you use virtualenv in Docker?

While Docker provides an isolated environment for your Python application, you're better off by using virtualenv (or your tool of choice) nevertheless. It can help you to maintain control over your Python environment & dependencies.

Can you move a virtualenv?

Kindly be informed that yes, it is possible to move it on the same platform. You can simply use --relocatable on an existing environment. But it will get changed based on the new location in order to call python and pip, etc.

What is the difference between virtual environment and Docker?

VMs have the host OS and guest OS inside each VM. A guest OS can be any OS, like Linux or Windows, irrespective of the host OS. In contrast, Docker containers host on a single physical server with a host OS, which shares among them. Sharing the host OS between containers makes them light and increases the boot time.


1 Answers

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.

like image 168
Indradeep Biswas Avatar answered Nov 04 '22 12:11

Indradeep Biswas