Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Docker and Python virtualenv?

From what I understand about Docker, it's a tool used for virtual environments. In their lingo, its called "containerization". This is more or less what Python's virtualenv does. However, you can use virtualenv in Docker. So, is it a virtual environment inside a virtual environment? I'm confused as to how this would even work, so could someone please clarify?

like image 388
danielschnoll Avatar asked Sep 28 '22 16:09

danielschnoll


People also ask

Should I use virtualenv or Docker?

In Conclusion. 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.

Is Docker like Python virtual environment?

If you want to install and use with more agility as many virtual environments and different versions of Python then Docker should be a serious alternative. Advantage : You don't need to use binaries for install your new virtual environment Python, any images and versions for Python are available under GitHub.

Is Docker same as virtual environment?

Docker vs VM: Architecture A virtual machine requires multiple or different kernels to run applications across servers. But, with Docker, a single operating system kernel is effectively used to run multiple applications across all containers.

What is the difference between Docker and Virtual Machine?

In Docker, the containers running share the host OS kernel. A Virtual Machine, on the other hand, is not based on container technology. They are made up of user space plus kernel space of an operating system. Under VMs, server hardware is virtualized.


3 Answers

A virtualenv only encapsulates Python dependencies. A Docker container encapsulates an entire OS.

With a Python virtualenv, you can easily switch between Python versions and dependencies, but you're stuck with your host OS.

With a Docker image, you can swap out the entire OS - install and run Python on Ubuntu, Debian, Alpine, even Windows Server Core.

There are Docker images out there with every combination of OS and Python versions you can think of, ready to pull down and use on any system with Docker installed.

like image 193
sp0gg Avatar answered Oct 08 '22 01:10

sp0gg


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.

like image 37
jil Avatar answered Oct 07 '22 23:10

jil


Adding to the above: there is a case for combining docker and venv: some OSs ship with python installed to provide 'OS-near' apps, e.g., to my knowledge, apt on debian (and its derivatives). The python venv enables a developer to ship a python app which requires a different interpreter version without affecting the shipped-with-the-OS python. Now, since Docker 'isolates the whole OS' as stated above, the same applies to a Docker image. Hence, in my view, if a Docker image is required/desired, it is best practice to create a venv inside the Docker image for your python app.

like image 16
Blindfreddy Avatar answered Oct 08 '22 01:10

Blindfreddy