Reputation: 19312
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?
Upvotes: 14
Views: 22049
Reputation: 3715
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.
Upvotes: 0
Reputation: 11924
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
Upvotes: 15
Reputation: 38872
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
Upvotes: 8
Reputation: 816
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.
Upvotes: 3
Reputation: 2365
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
Upvotes: 0
Reputation: 613
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.
Upvotes: 0