2

I'm looking to start creating proper isolated environments for django web apps. My first inclination is to use Docker. Also, it's usually recommended to use virtualenv with any python project to isolate dependencies.

Is virtualenv still necessary if I'm isolating projects via Docker images?

qarthandso
  • 2,100
  • 2
  • 24
  • 40

2 Answers2

2

If your Docker container is relatively long-lived or your project dependencies change, there is still value in using a Python virtual-environment. Beyond (relatively) isolating dependencies of a codebase from other projects and the underlying system (and notably, the project at a given state), it allows for a certain measure of denoting the state of requirements at a given time.

For example, say that you make a Docker image for your Django app today, and end up using it for the following three weeks. Do you see your requirements.txt file being modified between now and then? Can you imagine a scenario in which you put out a hotpatch that comes with environmental changes?

As of Python 3.3, virtual-env is stdlib, which means it's very cheap to use, so I'd continue using it, just in case the Docker container isn't as disposable as you originally planned. Stated another way, even if your Docker-image pipeline is quite mature and the version of Python and dependencies are "pre-baked", it's such low-hanging fruit that while not explicitly necessary, it's worth sticking with best-practices.

  • How does having a virtualenv help me for dependency changes anymore than not having one? – johnharris85 Sep 10 '16 at 02:58
  • By providing good isolation and declarative dependency management. If you make a fresh Docker image for each dependency change (e.g. some_dependency 0.1->0.2, or more importantly, with removing a package) and are sure your script's Python path will only follow the paths you expect, great. But that's not how I've things it in practice and virtualenv makes those little guarantees easy. I mean, one could just skip Docker and use LXC, but we're talking in shades of convenience. –  Sep 10 '16 at 10:48
  • virtualenv is to separate disparate apps. If each docker container is a thing, there's no use in virtualenv right? ie. MyDjangoApp will never need an isolated dependency tree inside the MyDjangoApp container. – Marc Young Sep 10 '16 at 18:24
  • virtualenv can be used for disparate apps, but also for dependencies within the lifetime of a single application. e.g. env_django_app_v1; env_django_app_v2. beyond that, basically the comment above and the answer still applies. –  Sep 11 '16 at 20:58
0

No not really if each Python / Django is going to live in it's own container.

johnharris85
  • 17,264
  • 5
  • 48
  • 52