Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good reason for setting up virtualenv for python in Docker containers?

Almost all python tutorials suggest that virutalenv be setup as step one to maintain consistency . In working with Docker containers, why or why not should this standard be maintained?

like image 708
zhqiat Avatar asked Dec 11 '15 14:12

zhqiat


People also ask

Is Python virtual environment necessary?

Virtual Environment should be used whenever you work on any Python based project. It is generally good to have one new virtual environment for every Python based project you work on. So the dependencies of every project are isolated from the system and each other.

Should Docker use for development environment?

Docker helps to ensure that all the developers have access to all the necessary bits and pieces of the software they work on. So if someone adds software dependencies, everyone has them when needed. If it is just one developer, there is no such need.

Is Docker like Python virtual environment?

Python virtual environment will "containerize" only Python runtime i.e. python interpreter and python libraries whereas Docker isolates the whole system (the whole file-system, all user-space libraries, network interfaces) . Therefore Docker is much closer to a Virtual Machine than virtual environment.

Why do we install virtual environment in Python?

Python virtual environments give you the ability to isolate your Python development projects from your system installed Python and other Python environments. This gives you full control of your project and makes it easily reproducible.


2 Answers

If you intend to run only one version on the container and it is the container's system version, there's no technical reason to use virtualenv in a container. But there could still be non-technical reasons. For example, if your team is used to finding python libraries in ~/some-env, or understands the virtualenv structure better than the container's libs, then you may want to keep using virtualenv anyway.

On the "cons" side, virtualenv on top of an existing system python may make your images slightly larger, too.

like image 162
kojiro Avatar answered Oct 11 '22 07:10

kojiro


When using docker it makes sense to adopt the microservices concept. With microservices each microservice is aligned with a specific business function, and only defines the operations necessary to that business function. This means that each application runs in one or more separate docker images with their specific dependencies (python modules). This makes the use of virtualenv unnecessary.

like image 32
Rob van Laarhoven Avatar answered Oct 11 '22 08:10

Rob van Laarhoven