Using Docker containers on CoreOS with Vagrant
Vagrant can help in simulating environments, and Docker containers are not forgotten with Vagrant. We'll use one of the best platforms to run containers, the free and open source lightweight operating system CoreOS. Based on Linux, targeting easy container and clustered deployments, it also provides official Vagrant boxes. We'll deploy the official WordPress container with MariaDB on another container using the Vagrant Docker provisioner (and not the Vagrant Docker provider).
Getting ready
To step through this recipe, you will need the following:
- A working Vagrant installation
- A working hypervisor
- An Internet connection
How to do it…
CoreOS doesn't host its official images at the default location on Atlas, it hosts it itself. So, we have to specify the full URL to the Vagrant box in our Vagrantfile:
Vagrant.configure("2") do |config| config.vm.box = https://stable.release.core-os.net/amd64-usr/current/coreos_production_vagrant.box end
As CoreOS is a minimal OS, it doesn't support any of the VirtualBox guest addition tools, so we'll disable them, and don't try anything if we (most likely) have the vagrant-vbguest
plugin:
config.vm.provider :virtualbox do |vb| vb.check_guest_additions = false vb.functional_vboxsf = false end if Vagrant.has_plugin?("vagrant-vbguest") then config.vbguest.auto_update = false end
Let's create a new VM definition, using the CoreOS Vagrant box:
config.vm.define "core-1" do |config| config.vm.hostname = "core-1" config.vm.network "private_network", type: "dhcp" end
We now need to run the mariadb
and wordpress
official containers from the Docker Hub. Using Docker directly, we would have run the following:
$ docker run -d --name mariadb -e MYSQL_ROOT_PASSWORD=h4ckm3 mariadb $ docker run -d -e WORDPRESS_DB_HOST=mariadb -e 'WORDPRESS_DB_PASSWORD=h4ckm3 --link mariadb:mariadb -p 80:80 wordpress
Let's translate this into our Vagrantfile:
db_root_password = "h4ckm3" config.vm.provision "docker" do |docker| docker.run "mariadb", args: "--name 'mariadb' -e 'MYSQL_ROOT_PASSWORD=#{db_root_password}'" docker.run "wordpress", args: "-e 'WORDPRESS_DB_HOST=mariadb' -e 'WORDPRESS_DB_PASSWORD=#{db_root_password}' --link 'mariadb:mariadb' -p '80:80'" end
Vagrant this up ($ vagrant up
), and you'll access a ready-to-use WordPress installation running on CoreOS:
$ curl -IL http://172.28.128.3/wp-admin/install.php HTTP/1.1 200 OK Date: Thu, 25 Aug 2016 10:54:17 GMT Server: Apache/2.4.10 (Debian) X-Powered-By: PHP/5.6.25 Expires: Wed, 11 Jan 1984 05:00:00 GMT Cache-Control: no-cache, must-revalidate, max-age=0 Content-Type: text/html; charset=utf-8
There's more…
The CoreOS team proposes a full Vagrant environment to try and manipulate a CoreOS cluster https://github.com/coreos/coreos-vagrant. You'll then be able to try all CoreOS features and configuration options for all release channels (alpha, beta, or stable).
Other operating systems such as Ubuntu or CentOS are fully supported to provision Docker containers, even if Docker isn't installed at first on the base image. Vagrant will install Docker for you, so it will work transparently and run the containers as soon as it's installed.