Say one of your dependencies has no pre-built wheels available, so you need gcc to build it from source in your docker image.
You can apt install gcc in a build stage, install your dependency and then you don't need to bother clearing apt lists cache, pruning unneeded packages such as gcc and the packages that were installed alongside it, etc.
Then, you create another stage for the final production runtime, copy the entire virtual environment over and that's it.
In short, the virtual environment is easier to transport from one stage to another, compared to a non-isolated environment -- and that makes it easy to trim down the image size.
Comments
Say one of your dependencies has no pre-built wheels available, so you need gcc to build it from source in your docker image.
You can apt install gcc in a build stage, install your dependency and then you don't need to bother clearing apt lists cache, pruning unneeded packages such as gcc and the packages that were installed alongside it, etc.
Then, you create another stage for the final production runtime, copy the entire virtual environment over and that's it.
In short, the virtual environment is easier to transport from one stage to another, compared to a non-isolated environment -- and that makes it easy to trim down the image size.