6 votes

Are Python virtual environments comparable to Docker containers?

Tags: python, docker

I've been trying to understand Docker and while also learning Python it occurred to me that virtual environments seem to be the same thing. They're probably not, but can anyone shed some light on this?

6 comments

  1. [5]
    Comment deleted by author
    Link
    1. acr
      Link Parent
      Yeah, this is a good way of putting it. I'd add, virtualenv is more for programming things and Docker is more network resource stuff aimed to essentially do what traditional VMs do. So a Docker...

      Yeah, this is a good way of putting it. I'd add, virtualenv is more for programming things and Docker is more network resource stuff aimed to essentially do what traditional VMs do. So a Docker image is going to be running something like postgres, nginx, mysql, etc.

      2 votes
    2. [3]
      fkr637
      Link Parent
      Thank you. This definitely helps. One more question, if you don't mind... If changes inside the container aren't persistent, are you just not supposed to use them in situations where you need...

      Thank you. This definitely helps. One more question, if you don't mind...

      If changes inside the container aren't persistent, are you just not supposed to use them in situations where you need persistent data?

      1. [2]
        Luca
        (edited )
        Link Parent
        The container is persistent so long as it doesn’t get recreated. There are several ways to persist data: If you need access to the data outside the container, mount a path from the host machine....

        The container is persistent so long as it doesn’t get recreated. There are several ways to persist data:

        • If you need access to the data outside the container, mount a path from the host machine.

        • If the data only needs to be persisted between docker containers, Docker Volukes Volumes are a thing.

        • If you want to distribute the changes you made, you need to create a new image based off the container

        2 votes
        1. fkr637
          Link Parent
          This makes sense. Thank you!

          This makes sense. Thank you!

  2. [2]
    nathan
    Link
    Not the same thing, but they are based on the same software design principles so I can see how you would think they’re similar. I’m not super familiar with virtualenv, but as I understand it,...

    Not the same thing, but they are based on the same software design principles so I can see how you would think they’re similar.

    I’m not super familiar with virtualenv, but as I understand it, virtualenv just creates a directory structure so you can manage dependencies of different projects in their respective project directory. So when you run your python commands in that project’s directory, you’re running that version of python with the correct versions of the dependencies. But it’s all managed within the filesystem.

    Docker on the other hand is about creating images and containers. Think of an image as an executable, and a container as a like a process. You can run the same executable (image) multiple times and each executable process (container) will be isolated from the other instances of that process.

    An image is made up of layers that tell docker how to construct that container (basically think, install python, copy the scripts from this directory into the image, install this dependency) and then when your have docker run the image it uses features built into the Linux kernel to create a “fake” filesystem for the container, an isolated network stack, and limit the resources (CPU, memory, bandwidth etc) that it has access to. When you “exec in” to a container to get a bash shell, it look like it’s running it’s own operating system, but really it’s just using features of the Linux kernel to isolate the container down to the filesystem level.

    My advice is to just try both of these tools out. Write a small python program using virtual env (a tildes scraper even :)) and then run it inside a docker container. These things always make much more sense when you’re actually using them.

    1 vote
    1. fkr637
      Link Parent
      Thanks for the info and the scraper idea : ) I definitely agree about things making more sense when actually using them. I wonder if this is a matter of learning style or the nature of the technology.

      Thanks for the info and the scraper idea : ) I definitely agree about things making more sense when actually using them. I wonder if this is a matter of learning style or the nature of the technology.