Virtual Development Server: Creating Virtualbox machine including docker containers with Vagrant

Posted by Torsten Kleiber on August 12, 2016 Tags: Virtual-Development-Server Continous-Integration Docker Infrastructure-as-Code Linux Oracle Vagrant VirtualBox

As I want to decouple my development server from my computer as much as possible I want to create first a virtual machine with linux.

Because I have used Oracle Virtualbox in the past successfully, I will use it here too.

As I want have later a reproducible environment via Infrastructure as code, I give Vagrant a try for this. As source code management system I use GIT, my remote repository is located at GitHub.

As I don’t want create a new linux from scratch, I have looked for a ready system at Vagrantboxes, which contains a new Oracle Linux version, and find a Oracle Linux 7.1 x86_64 system with Chef and Puppet preinstalled.

Before I could start, I had to install on my Windows 10 computer Vagrant 1.8.5 and Oracle Virtualbox 5.0.16. Don’t use the 5.1 Version, as at this is not yet supported by vagrant 1.8.5.

For automatic update of the Oracle VM VirtualBox Extension Pack, which is delivered with the ready box and is required for use of shared folders and more, you should install following plugin as described here:

vagrant plugin install vagrant vbguest

Next I have created a directory of your choice and init there a Vagrantfile by calling

vagrant init

Then I changed the Vagrantfile as following, see the inline comments for documentation:

Vagrant.configure(2) do |config|

  # Use the mentioned ready OEL 7 linux box
  config.vm.box = "oraclelinux-7-x86_64.box"
  config.vm.box_url = "http://cloud.terry.im/vagrant/oraclelinux-7-x86_64.box"

  # Create a private network
  config.vm.network "private_network", type: "dhcp"

  # persistant storage for all docker container
  config.vm.synced_folder "C:\\shared\\virtual_storage", "/virtual_storage", :mount_options => ["dmode=777","fmode=777"]

  # virtualbox provider
  config.vm.provider "virtualbox" do |vb|
    # name in VirtualBox
    vb.name = "Development Server"
	end

  # Docker Private Registry container for storing later builded docker images, which are not in the Docker Public Registry at https://hub.docker.com/
  config.vm.provision "docker" do |d|
    d.run "registry", image: "registry", daemonize: true, args: "-d -p 5000:5000 -v /virtual_storage/docker_registry:/var/lib/registry"
  end

end

Run next from the created directory

vagrant up

Following you see the output

Bringing machine 'default' up with 'virtualbox' provider... (1)
==> default: Importing base box 'oraclelinux-7-x86_64.box'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: Development Server
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Remote connection disconnect. 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 it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
[default] GuestAdditions versions on your host (5.0.26) and guest (5.0.12) do not match.(2)
Loaded plugins: ulninfo
Package kernel-uek-devel-4.1.12-32.el7uek.x86_64 already installed and latest version
Package gcc-4.8.5-4.el7.x86_64 already installed and latest version
Package 1:make-3.82-21.el7.x86_64 already installed and latest version
Package 4:perl-5.16.3-286.el7.x86_64 already installed and latest version
Package bzip2-1.0.6-13.el7.x86_64 already installed and latest version
Nothing to do
Copy iso file C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
mount: /dev/loop0 is write-protected, mounting read-only
Installing Virtualbox Guest Additions 5.0.26 - guest version is 5.0.12
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.26 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 5.0.12 of VirtualBox Guest Additions...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module[  OK  ]
Building the shared folder support module[  OK  ]
Building the shared folder support module[  OK  ]
Building the graphics driver module[  OK  ]
Doing non-kernel setup of the Guest Additions[  OK  ]
You should restart your guest to make sure the new modules are actually used

==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => C:/shared/scmlocal/blog
default: /virtual_storage => C:/shared/virtual_storage
==> default: Running provisioner: docker...(3)
default: Installing Docker onto machine...
==> default: Starting Docker containers...(4)
==> default: -- Container: registry
1 you see the loading of the ready box and booting with the defined network
2 the version of the VirtualBox GuestAdditions of the box is checked and after that replaced with the actual installed VirtualBox version. This happens only on the first "vagrant up" call.
3 Docker is installed as a Vagrant Docker provider is called in the Vagrantfile line 20 and it does not exist in the box before.
4 a Docker Private Registry container is started via loading docker image from the Docker Public Registry. In a later blog we will use this Docker Private Registry container to hold our own created docker images like database and development tools. The pushed images are saved to a mapped host volume of my windows 10 computer.

You can connect to the virtualbox directly via

vagrant ssh

The you can see the downloaded Docker Private Registry images and the started container from this images:

[vagrant@oraclelinux7 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
registry            latest              c6c14b3960bd        13 days ago         33.28 MB
[vagrant@oraclelinux7 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
739c1c9926b1        registry            "/entrypoint.sh /etc/"   5 minutes ago       Up 5 minutes        0.0.0.0:5000->5000/tcp   registry

You can now stop the Development Server via

vagrant halt

If you startup again the server via

vagrant up

the provision steps are not replayed until you destroy the server or explicitly call the provision again via

vagrant provision

Here you find the source code for this blog.

Here you find more about the topic "Virtual Development Server".

That’s it!