Install Docker via a Vagrantfile

Tons of ways of doing this on the internet – many that don’t work for various reasons – e.g. older versions of Ubuntu, etc… – and ranging from complex to very complex.

Here’s two simple ways:

  1. Most simple

Paste into a Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.define "vm1"
  config.vm.provision "docker" 
end
vagrant up 
vagrant ssh vm1
docker version

2. A little more manual using Docker’s convenience script:

Paste into a Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.define "vm1"
end

Then:

vagrant up
vagrant ssh vm1
sudo su
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Here’s what didn’t work for me:

https://stackoverflow.com/questions/52498892/install-docker-via-a-vagrantfile

I guess it doesn’t help that precise64 is quite an old version of Ubuntu but it’s what was given on the Vagrant website for provisioning Docker.

and

https://www.vagrantup.com/docs/provisioning/docker.html

which I basically ignored ‘cos it didn’t have a correct concrete example as of the time of writing this blog post (even though it’s Hashicorp’s own website!).

Vagrant: Provisioning

Provisioning a VM with your Vagrantfile.

e.g. to install dockercreate a file bootstrap.sh :

It’s a little convoluted. Why didn’t I use `config.vm.provision “docker”`? ‘cos it didn’t work for me: https://stackoverflow.com/questions/52498892/install-docker-via-a-vagrantfile

#!/usr/bin/env bash

apt-get update
apt-get install -y docker-ce

Note: don’t use apt-get install -y docker as this installs the System tray!

https://gist.github.com/kjellski/6158747 and https://unix.stackexchange.com/questions/363048/unable-to-locate-package-docker-ce-on-a-64bit-ubuntu/363058#363058

and add it to your Vagrantfile, e.g.:

Vagrant.configure('2') do |config|
    config.vm.box = "hashicorp/precise64"
    config.vm.define "vm2"
    config.vm.hostname = "vm2"
    config.vm.provision :shell, path: "bootstrap.sh"
end

and force Vagrant to reprovision with:

vagrant reload --provision

https://www.vagrantup.com/intro/getting-started/provisioning.html

https://www.vagrantup.com/docs/vagrantfile/machine_settings.html