Best way to install docker on Vagrant

pkaramol picture pkaramol · Nov 21, 2017 · Viewed 11.1k times · Source

I want to create several VMs that have docker pre-installed.

What is the best/recommended way to go about this?

a) Have Docker provisioner do something dummy, just so that Docker gets installed, e.g.

  mymachine.vm.provision "docker" do |docknode|
      # do something pointless
  end

b) run docker installation via a shell provisioner script?

mymachine.vm.provision "shell", path: "docker-installation-script.sh"

c) use a Vagrant image that comes with Docker pre-installed?

Answer

mloskot picture mloskot · Oct 21, 2019

If you are using fairly recent Vagrant with Docker provisioner support (e.g. the steps below were tested using 2.2.6), then you can install Docker with a very simple one or two one-liners, without the d.run or similar hacks:

Vagrant.configure(2) do |config|
  config.vm.box = "generic/ubuntu1904"

  # Install Docker
  config.vm.provision :docker

  # Install Docker Compose
  # First, install required plugin https://github.com/leighmcculloch/vagrant-docker-compose:
  # vagrant plugin install vagrant-docker-compose
  config.vm.provision :docker_compose

end

Run vagrant provision or vagrant up and observe this output:

==> default: Running provisioner: docker...
    default: Installing Docker onto machine...
==> default: Running provisioner: docker_compose...
    default: Checking for Docker Compose installation...
    default: Getting machine and kernel name from guest machine...
    default: Downloading Docker Compose 1.24.1 for Linux x86_64
    default: Uploading Docker Compose 1.24.1 to guest machine...
    default: Installing Docker Compose 1.24.1 in guest machine...
    default: Symlinking Docker Compose 1.24.1 in guest machine...

Finally, vagrant ssh to the VM and check versions of the deployed Docker infrastructure:

$ docker --version
Client: Docker Engine - Community
 Version:           19.03.3
 API version:       1.40
 Go version:        go1.12.10
 Git commit:        a872fc2f86
 Built:             Tue Oct  8 01:00:44 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.3
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.10
  Git commit:       a872fc2f86
  Built:            Tue Oct  8 00:59:17 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683