-2

I program primarily in python and have some experience with virtual environments. I am new to the software and started looking at docker to run my code. I would like insight on what it does and how it works.

From my understanding docker containers are like virtual environments that run a set of instructions when executed and can treat that everything contained within it as a single entity (so it or something else wouldn't be conditional on each other?). As I read more about containers, they sound pretty perfect and would eliminate any need for virtual environments but again unsure. Would much appreciate some clarification on this because I haven't been able to find anything online.

Adiii
  • 54,482
  • 7
  • 145
  • 148
zeeb
  • 1
  • 2
  • Check their 101 series, it's pretty good https://www.docker.com/101-tutorial. Also, if you want to understand at a deeper level, watch this https://www.youtube.com/watch?v=8fi7uSYlOdc – sal Jul 30 '20 at 02:54
  • Does this answer your question? [Docker, what is it and what is the purpose?](https://stackoverflow.com/questions/28089344/docker-what-is-it-and-what-is-the-purpose) – kesarling He-Him Jul 30 '20 at 02:58
  • Docker's isolation environment also separates the host's local filesystem from the container's filesystem; this is a problem for many typical applications. If you're actively developing, this isolation also means the container has a separate filesystem from your IDE, which gets inconvenient. – David Maze Jul 30 '20 at 12:07

3 Answers3

1

The main purpose of the python virtual environment is the isolation of the environment for each project, it's mean that each project can have its own dependencies, regardless of what dependencies every other project has.

enter image description here

what-is-a-virtual-environment

But when it comes to docker, you can treat each docker image as an isolated environment, you do not need to create or maintain a virtual environment in Dockerfile, as Dockerfile should be base on a particular version of python and should run single project.

enter image description here

python-versions-docker

So in short, if you have 3 projects that require

  • Project A requires Python 3.6
  • Project B requires Python 3.7
  • Project C requires Python 3.8

All need to chose base image for each project

  • Project A FROM python:3.6
  • Project B FROM python:3.7
  • Project C FROM python:3.8
Adiii
  • 54,482
  • 7
  • 145
  • 148
0

I prefer to think of containers as a OS on top of your OS. You can google lots of info about docker, but if talking in simple language it is a thin layer that runs on top of your OS, uses your OS's resources (unlike VM), and runs its own enclosed environment.

Artem
  • 61
  • 8
0

Not any of the two can replace the other. It depends on what you are doing.

Python virtual environment is a way to encapsulate all app dependencies inside a single environment (actually a directory). These dependencies are other apps and packages that support the OS version you are using.

Docker container is a way to run a virtual machine with low resource consumption by sharing a lot of your OS files (more details in docker docs)

So,

if you need to create a development environment; it is recommended to use docker because you can douplicate the exact development experience for all developpers. Everything will be in a virtual machine that has its own OS version and its own files (virtually). Python virtual envirounment will not 100% help other developers unless they are using the same OS version of yours and they can duplicate your exact steps to deploy your app

But, if you are creating a package that will deploy a remote server (let say by using ansible), docker will be an extra un-needed step. Python environment will do the job just fine without any issue.

Also, it is very common to have dockers that include many python virtual environments; one environment for each service. So, even a docker image can include a python venv

Shadi Naif
  • 184
  • 9