Categories
docker vagrant

Should I use Vagrant or Docker for creating an isolated environment? [closed]

2168

I use Ubuntu for development and deployment and have a need for creating an isolated environment.

I am considering either Vagrant or Docker for this purpose. What are the pros and cons, or how do these solutions compare?

4

  • 31

    Both are combinable now: docs.vagrantup.com/v2/provisioning/docker.html

    – Alp

    May 14, 2014 at 7:33

  • 93

    Your question is lucky enough to get both of the writers’ answers of the two services: Mitchell and Solomon Hykes

    – itsazzad

    Dec 19, 2015 at 10:40

  • 7

    I would like to give a new summary – the question is mostly wrong. The right question is: Should I use Vagrant or docker-compose for creating an isolated environment? The answer is that Vagrant and docker-compose perform the same task of describing environments, and you should rather compare Docker to Virtualbox instead. The difference is that Vagrant can use any virtualization such as Docker, VMWare, Virtualbox on Windows, Linux or OSX, but docker-compose can just use Linux- based Docker images.

    Oct 14, 2018 at 8:10


  • For me the the answer is “How important is speed for you in regular work activities”. I find Vagrant to be slower than Docker. On docker, especially after an initial pull, the cache and layers approach of docker makes it the easiest and fastest for me as a developer to use it

    May 9, 2019 at 11:40


1184

If your purpose is the isolation, I think Docker is what you want.

Vagrant is a virtual machine manager. It allows you to script the virtual machine configuration as well as the provisioning. However, it is still a virtual machine depending on VirtualBox (or others) with a huge overhead. It requires you to have a hard drive file that can be huge, it takes a lot of ram, and performance may be not very good.

Docker on the other hand uses kernel cgroup and namespacing via LXC. It means that you are using the same kernel as the host and the same file system.
You can use Dockerfile with the docker build command in order to handle the provisioning and configuration of your container. You have an example at docs.docker.com on how to make your Dockerfile; it is very intuitive.

The only reason you could want to use Vagrant is if you need to do BSD, Windows or other non-Linux development on your Ubuntu box. Otherwise, go for Docker.

10

  • 13

    Unfortunately, not yet. If you are in a 32 bit system, you will need a VM with a 64 bit guest system in order to run docker. However, with go1.1 the 32 bit support get better and it is possible that docker will support 32 bit arch soon

    – creack

    May 30, 2013 at 6:48

  • 8

    This is true for Mac and Windows, but since docker 0.7, any linux distro works fine. If you know of a non-working one, please let me know. Also, unless you have a Mac or Windows stack (which is unlikely but can happen), you do not want to run Docker anywhere but on linux. The docker client works fine on Mac, should work soon on BSD and the daemon will eventually support BSD, Solaris and Mac.

    – creack

    Jan 28, 2014 at 20:25


  • 9

    In case someone reads these comments, you should know that Docker hit ver1.0 just 12 days ago (blog.docker.com/2014/06/its-here-docker-1-0) and a whole lot of different platforms are stable & supported now (docs.docker.com/installation)

    Jun 22, 2014 at 2:22

  • 17

    vagrant has LXC and docker provisioners. However – Vagrant and docker are fundamentally different things. Vagrant is purely for development environments, docker is rather for production and Linux only.

    – Dannyboy

    Nov 8, 2014 at 13:46


  • 2

    Docker now works on Windows 10 Pro and up and Windows Server 2016. I just upgraded from Windows 10 Home to Windows 10 Pro and installed the docker app. I can now run Linux docker images on Windows 10. It’s brilliant!

    Nov 11, 2016 at 19:02

1468

I’m the author of Docker.

The short answer is that if you want to manage machines, you should use Vagrant. And if you want to build and run applications environments, you should use Docker.

Vagrant is a tool for managing virtual machines. Docker is a tool for building and deploying applications by packaging them into lightweight containers. A container can hold pretty much any software component along with its dependencies (executables, libraries, configuration files, etc.), and execute it in a guaranteed and repeatable runtime environment. This makes it very easy to build your app once and deploy it anywhere – on your laptop for testing, then on different servers for live deployment, etc.

It’s a common misconception that you can only use Docker on Linux. That’s incorrect; you can also install Docker on Mac, and Windows. When installed on Mac, Docker bundles a tiny Linux VM (25 MB on disk!) which acts as a wrapper for your container. Once installed this is completely transparent; you can use the Docker command-line in exactly the same way. This gives you the best of both worlds: you can test and develop your application using containers, which are very lightweight, easy to test and easy to move around (see for example https://hub.docker.com for sharing reusable containers with the Docker community), and you don’t need to worry about the nitty-gritty details of managing virtual machines, which are just a means to an end anyway.

In theory it’s possible to use Vagrant as an abstraction layer for Docker. I recommend against this for two reasons:

  • First, Vagrant is not a good abstraction for Docker. Vagrant was designed to manage virtual machines. Docker was designed to manage an application runtime. This means that Docker, by design, can interact with an application in richer ways, and has more information about the application runtime. The primitives in Docker are processes, log streams, environment variables, and network links between components. The primitives in Vagrant are machines, block devices, and ssh keys. Vagrant simply sits lower in the stack, and the only way it can interact with a container is by pretending it’s just another kind of machine, that you can “boot” and “log into”. So, sure, you can type “vagrant up” with a Docker plugin and something pretty will happen. Is it a substitute for the full breadth of what Docker can do? Try native Docker for a couple days and see for yourself 🙂

  • Second, the lock-in argument. “If you use Vagrant as an abstraction, you will not be locked into Docker!”. From the point of view of Vagrant, which is designed to manage machines, this makes perfect sense: aren’t containers just another kind of machine? Just like Amazon EC2 and VMware, we must be careful not to tie our provisioning tools to any particular vendor! This would create lock-in – better to abstract it all away with Vagrant. Except this misses the point of Docker entirely. Docker doesn’t provision machines; it wraps your application in a lightweight portable runtime which can be dropped anywhere.

What runtime you choose for your application has nothing to do with how you provision your machines! For example it’s pretty frequent to deploy applications to machines which are provisioned by someone else (for example an EC2 instance deployed by your system administrator, perhaps using Vagrant), or to bare metal machines which Vagrant can’t provision at all. Conversely, you may use Vagrant to provision machines which have nothing to do with developing your application – for example a ready-to-use Windows IIS box or something. Or you may use Vagrant to provision machines for projects which don’t use Docker – perhaps they use a combination of rubygems and rvm for dependency management and sandboxing for example.

In summary: Vagrant is for managing machines, and Docker is for building and running application environments.

11

  • 405

    Just wanted to note that the Vagrant aspects of this answer are incorrect. Vagrant is not for managing machines, Vagrant is for managing development environments. The fact Vagrant spins up machines is mostly historic. The next version of Vagrant has first class support to spin up dev environment using Docker as a provider directly on the host or any VM (Mac, Win). It can also spin up raw LXC if thats what someone wants (again, on the host or VM). Vagrant is interested in doing what is best to create a portable development environment, whether that means creating a VM or not.

    – Mitchell

    Mar 13, 2014 at 17:10


  • 8

    @Davide This covers it in more detail: vagrantup.com/blog/…

    – Mitchell

    Apr 29, 2014 at 16:59

  • 50

    “It’s a common misconception that you can only use Docker on Linux” While thats true, it is accurate to say that you can only use Linux on Docker. If I want to set up a test runner that exercises my application in a wide variety of envionment configs (different databases, php versions, caching backends etc), then I can easily do that with docker containers. But I cant see if my application will run properly in a windows IIS env, or on BSD or OSX.

    – Mixologic

    May 6, 2014 at 16:00

  • 10

    Your first point is outdated since Vagrant has built-in provider support for docker: docs.vagrantup.com/v2/provisioning/docker.html

    – Alp

    May 14, 2014 at 7:34

  • 20

    The post is outdated. Vagrant supports Docker as a provider now. And there are some videos demonstrating how you can use Vagrant and Docker in unison on their blog.

    – sargas

    Dec 18, 2014 at 15:52

1184

If your purpose is the isolation, I think Docker is what you want.

Vagrant is a virtual machine manager. It allows you to script the virtual machine configuration as well as the provisioning. However, it is still a virtual machine depending on VirtualBox (or others) with a huge overhead. It requires you to have a hard drive file that can be huge, it takes a lot of ram, and performance may be not very good.

Docker on the other hand uses kernel cgroup and namespacing via LXC. It means that you are using the same kernel as the host and the same file system.
You can use Dockerfile with the docker build command in order to handle the provisioning and configuration of your container. You have an example at docs.docker.com on how to make your Dockerfile; it is very intuitive.

The only reason you could want to use Vagrant is if you need to do BSD, Windows or other non-Linux development on your Ubuntu box. Otherwise, go for Docker.

10

  • 13

    Unfortunately, not yet. If you are in a 32 bit system, you will need a VM with a 64 bit guest system in order to run docker. However, with go1.1 the 32 bit support get better and it is possible that docker will support 32 bit arch soon

    – creack

    May 30, 2013 at 6:48

  • 8

    This is true for Mac and Windows, but since docker 0.7, any linux distro works fine. If you know of a non-working one, please let me know. Also, unless you have a Mac or Windows stack (which is unlikely but can happen), you do not want to run Docker anywhere but on linux. The docker client works fine on Mac, should work soon on BSD and the daemon will eventually support BSD, Solaris and Mac.

    – creack

    Jan 28, 2014 at 20:25


  • 9

    In case someone reads these comments, you should know that Docker hit ver1.0 just 12 days ago (blog.docker.com/2014/06/its-here-docker-1-0) and a whole lot of different platforms are stable & supported now (docs.docker.com/installation)

    Jun 22, 2014 at 2:22

  • 17

    vagrant has LXC and docker provisioners. However – Vagrant and docker are fundamentally different things. Vagrant is purely for development environments, docker is rather for production and Linux only.

    – Dannyboy

    Nov 8, 2014 at 13:46


  • 2

    Docker now works on Windows 10 Pro and up and Windows Server 2016. I just upgraded from Windows 10 Home to Windows 10 Pro and installed the docker app. I can now run Linux docker images on Windows 10. It’s brilliant!

    Nov 11, 2016 at 19:02