Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does virtualenv serve a purpose (in production) when using docker?

For development we use virtualenv to have an isolated development when it comes to dependencies. From this question it seems deploying Python applications in a virtualenv is recommended.

Now we're starting to use docker for deployment. This provides a more isolated environment so I'm questioning the use of virtualenv inside a docker container. In the case of a single application I do not think virtualenv has a purpose as docker already provides isolation. In the case where multiple applications are deployed on a single docker container, I do think virtualenv has a purpose as the applications can have conflicting dependencies.

Should virtualenv be used when a single application is deployed in a docker container?

Should docker contain multiple applications or only one application per container?

If so, should virtualenv be used when deploying a container with multiple applications?

like image 658
siebz0r Avatar asked Nov 19 '14 13:11

siebz0r


People also ask

Is virtual environment necessary for 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.

Is Docker a production environment?

They're not suitable for production usage! Therefore, it will require some tweaking so that your Docker engine can handle the load once in production environment. Moreover, your engine will be in charge of running the containers and nothing more.

How does Docker help in production?

In a production environment, Docker makes it easy to create, deploy, and run applications inside of containers. Containers let developers gather applications and all their core necessities and dependencies into a single package that you can turn into a Docker image and replicate.

Is a Docker container a virtual environment?

Docker is popular virtualization software that helps its users in developing, deploying, monitoring, and running applications in a Docker Container with all their dependencies.


2 Answers

Virtualenv was created long before docker. Today, I lean towards docker instead of virtualenv for these reasons:

  • Virtualenv still means people consuming your product need to download eggs. With docker, they get something which is "known to work". No strings attached.
  • Docker can do much more than virtualenv (like create a clean environment when you have products that need different Python versions).

The main drawback for Docker was the poor Windows support. That changed with the version for Windows 10.

As for "how many apps per container", the usual policy is 1.

like image 104
Aaron Digulla Avatar answered Sep 29 '22 18:09

Aaron Digulla


Yes. You should still use virtualenv. Also, you should be building wheels instead of eggs now. Finally, you should make sure that you keep your Docker image lean and efficient by building your wheels in a container with the full build tools and installing no build tools into your application container.

You should read this excellent article. https://glyph.twistedmatrix.com/2015/03/docker-deploy-double-dutch.html

The key take away is

It’s true that in many cases, perhaps even most, simply installing stuff into the system Python with Pip works fine; however, for more elaborate applications, you may end up wanting to invoke a tool provided by your base container that is implemented in Python, but which requires dependencies managed by the host. By putting things into a virtualenv regardless, we keep the things set up by the base image’s package system tidily separated from the things our application is building, which means that there should be no unforeseen interactions, regardless of how complex the application’s usage of Python might be.

like image 31
Bruno Bronosky Avatar answered Sep 29 '22 20:09

Bruno Bronosky