Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to install docker on Vagrant

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?

like image 450
pkaramol Avatar asked Nov 21 '17 14:11

pkaramol


People also ask

Can I install Docker in Vagrant?

The Vagrant Docker provisioner can automatically install Docker, pull Docker containers, and configure certain containers to run on boot. The docker provisioner is ideal for organizations that are using Docker as a means to distribute things like their application or services.

Is Docker faster than Vagrant?

Vagrant allows you to isolate all the necessary resources completely. However, compared to Docker, it requires more resources initially. Compared to Vagrant, Docker wins on this criterion because it spends fewer resources, and you can create Docker images faster than Vagrant virtual machines.

Do you need Vagrant with Docker?

Essentially, Docker is a technology for creating and running Linux containers, and Vagrant is a machine provisioning tool used to create VMs and then populate them with applications. In other words, you use Vagrant to create a VM and install Docker.


2 Answers

Here is somewhat more user-friendly Vagrantfile (tested on Vagrant 2.2.7)

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/bionic64"

  # require plugin https://github.com/leighmcculloch/vagrant-docker-compose
  config.vagrant.plugins = "vagrant-docker-compose"

  # install docker and docker-compose
  config.vm.provision :docker
  config.vm.provision :docker_compose

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--ioapic", "on"]
    vb.customize ["modifyvm", :id, "--memory", "2048"]
    vb.customize ["modifyvm", :id, "--cpus", "2"]
  end

end

As described here you can require plugin installation from within Vagrantfile

And here are the steps

$ vagrant up
Vagrant has detected project local plugins configured for this
project which are not installed.

  vagrant-docker-compose
Install local plugins (Y/N) [N]: y
Installing the 'vagrant-docker-compose' plugin. This can take a few minutes...
Fetching: vagrant-docker-compose-1.5.1.gem (100%)
Installed the plugin 'vagrant-docker-compose (1.5.1)'!

Vagrant has completed installing local plugins for the current Vagrant
project directory. Please run the requested command again.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/bionic64'...
...
==> 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: Downloaded Docker Compose 1.24.1 has SHA256 signature cfb3...
    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...

$ vagrant ssh
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-88-generic x86_64)
vagrant@ubuntu-bionic:~$ docker -v
Docker version 19.03.8, build afacb8b7f0
vagrant@ubuntu-bionic:~$ docker-compose -v
docker-compose version 1.24.1, build 4667896b
like image 52
Levon Avatar answered Nov 07 '22 21:11

Levon


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
like image 33
mloskot Avatar answered Nov 07 '22 20:11

mloskot