Elegantly activating a virtualenv in a Dockerfile

When you’re packaging your Python application in a Docker image, you’ll often use a virtualenv. For example, you might be doing a multi-stage build in order to get smaller images. Since you’re using a virtualenv, you need to activate it—but if you’re just getting started with Dockerfiles, the naive way doesn’t work. And even if you do know how to do it, the usual method is repetitive and therefore error-prone. There is a simpler way of activating a virtualenv, which I’ll demonstrate in this article. But first, we’ll go over some of the other, less elegant (or broken!) ways you might do it. A note: outside the specific topic under discussion, the Dockerfiles in this article are not examples of best practices, since that would involve many extraneous details that would make the main point harder to understand.

Source : Elegantly activating a virtualenv in a Dockerfile