Best way to install docker on Vagrant
Asked Answered
T

6

14

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?

Teressaterete answered 21/11, 2017 at 14:37 Comment(0)
P
15

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
Prefab answered 17/4, 2020 at 17:46 Comment(3)
Excellent stuff, many thanks. Only thing I would add is that on an already provisioned box, reload (which I think is the same as halt + up) did not run the new provisioning commands; and as in my case provisioning takes a long time, and I'm not sure if --provision would take similarly long time, I added :run => 'always' eg config.vm.provision :docker, :run => 'always' to force the docker provisioning to run.Haldis
It worked for me thanks: Install plugin. $ vagrant plugin install vagrant-docker-compose Check plugin installation $ vagrant plugin listSec
mmm got this error: docker provider: * One of "build_dir", "git_repo" or "image" must be set also, why are you using config.vm.provider "virtualbox" ? the purpose of docker is to avoid virtualboxNarcolepsy
F
8

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
Famed answered 21/10, 2019 at 12:28 Comment(0)
S
3

I was looking for an answer to this question too, and found this answer on StackOverflow. It looks like you were correct. Running a dummy image is the best way to install THE LATEST VERSION OF DOCKER.

  config.vm.provision "docker" do |d|
      d.run "hello-world"
  end

From this StackOverflow answer: if you want to install a specific version of docker, you will have to run a shell provisioner before your docker provisioner (provisioners are run in order) to install a specific version of Docker.

Sacking answered 6/3, 2019 at 18:48 Comment(0)
S
0

I'd use docker-machine as "You can use Machine to create Docker hosts on your local Mac or Windows box, on your company network, in your data center, or on cloud providers like Azure, AWS, or Digital Ocean". It's an easy and quick way to launch VMs with Docker inside.

Sampson answered 21/11, 2017 at 17:47 Comment(1)
it's use boot2docker nor vagrantJermaine
C
0

Offical instruction is here: https://www.vagrantup.com/docs/provisioning/docker.html

For example:

Vagrant.configure("2") do |config|
  config.vm.provision "docker" do |d|
    d.build_image "/vagrant/app"
  end
end

Or

Vagrant.configure("2") do |config|
  config.vm.provision "docker" do |d|
    d.run "rabbitmq"
  end
end
Corves answered 27/2, 2018 at 8:58 Comment(0)
G
0

I think (b) is the best way to deploy, by this way you can know what happen during that. It means that, you can solve all things when bugs are found or some features you want.

And someday, maybe you need to deploy docker to another place, the script will help you a lot.

Goof answered 7/10, 2020 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.