What is Vagrant?
Vagrant is a simplified and portable way to create virtual development environments. It works with multiple virtualization software such as VirtualBox, VMWare, AWS, and more. It also works with multiple configuration software such as Ansible, Chef, Puppet, or Salt.
No more “works on my machine”!
The usual providers are, well, usual. Starting with version 1.6, Docker containers can be used as one of the backend providers as well. This allows your development environment to be based on Docker containers as opposed to full Virtual Machines. Read more about this at docs.vagrantup.com/v2/docker/index.html.
The complete development environment definition such as the type of machine, software that needs to be installed, networking, and other configuration information is defined in a text file, typically called as Vagrantfile
. Based upon the provider, it creates the virtual development environment.
Read more about what can be defined in the file, and how, at docs.vagrantup.com/v2/vagrantfile/index.html.
Getting Started with Vagrant
Getting Started Guide is really simple and easy to follow to get your feet wet with Vagrant. Once your basic definition is created, the environment can be started with a simple command:
1
2
3
|
vagrant up
|
The complete set of commands are defined at docs.vagrantup.com/v2/cli/index.html.
Default provider for Vagrant is VirtualBox. An alternate provider can be specified at the CLI as:
1
2
3
|
vagrant up --provider=docker
|
This will spin up the Docker container based upon the image specified in the Vagrantfile.
Packaging Format
Vagrant environments are packaged as Boxes. You can search from the publicly available list of boxes to find the box of your choice. Or even create your own box, and add them to the central repository using the following command:
1
2
3
|
vagrant box add USER/BOX
|
Vagrant with WildFly Docker image
After learning the basic commands, lets see what does it take to start a WildFly Docker image using Vagrant.
The Vagrantfile is defined at github.com/arun-gupta/vagrant-images/blob/master/docker-wildfly/Vagrantfile and shown in line:
1
2
3
4
5
6
7
8
|
Vagrant.configure(2) do |config|
config.vm.provider "docker" do |d|
# Define the Docker image
d.image = "jboss/wildfly:latest"
end
end
|
Clone the git repo and change to docker-wildfly
directory. Vagrant image can be started using the following command:
1
2
3
|
vagrant up --provider=docker
|
and shows the output as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
docker-wildfly> vagrant up
Bringing machine 'default' up with 'docker' provider...
==> default: Docker host is required. One will be created if necessary...
default: Vagrant will now create or start a local VM to act as the Docker
default: host. You'll see the output of the `vagrant up` for this VM below.
default:
default: Box 'mitchellh/boot2docker' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
default: Loading metadata for box 'mitchellh/boot2docker'
default: URL: https://atlas.hashicorp.com/mitchellh/boot2docker
default: Adding box 'mitchellh/boot2docker' (v1.2.0) for provider: virtualbox
default: Downloading: https://atlas.hashicorp.com/mitchellh/boxes/boot2docker/versions/1.2.0/providers/virtualbox.box
default: Successfully added box 'mitchellh/boot2docker' (v1.2.0) for 'virtualbox'!
default: Importing base box 'mitchellh/boot2docker'...
default: Matching MAC address for NAT networking...
default: Checking if box 'mitchellh/boot2docker' is up to date...
default: Setting the name of the VM: docker-host_default_1421277252359_12510
default: Fixed port collision for 22 => 2222. Now on port 2203.
default: Clearing any previously set network interfaces...
default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Forwarding ports...
default: 2375 => 2375 (adapter 1)
default: 22 => 2203 (adapter 1)
default: Running 'pre-boot' VM customizations...
default: Booting VM...
default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2203
default: SSH username: docker
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if its present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
default: Machine booted and ready!
==> default: Syncing folders to the host VM...
default: Installing rsync to the VM...
default: Rsyncing folder: /Users/arungupta/workspaces/vagrant-images/docker-wildfly/ => /var/lib/docker/docker_1421277277_78698
==> default: Warning: When using a remote Docker host, forwarded ports will NOT be
==> default: immediately available on your machine. They will still be forwarded on
==> default: the remote machine, however, so if you have a way to access the remote
==> default: machine, then you should be able to access those ports there. This is
==> default: not an error, it is only an informational message.
==> default: Creating the container...
|
This will not work until #5187 is fixed. But at least this blog explained the main concepts of Vagrant.