Hacker Spaces

VM images of Ubuntu Server 14.04 LTS (Trusty Tahr) with Vagrant and Docker Containers

This article is an update to https://ochronus.com/docker-primer-django/ post for Ubuntu Server 14.04 LTS (Trusty Tahr).

TL;DR if you’re lucky and everything is installed right and you don’t want to understand deeper, try:

vagrant init ubuntu/trusty64
vagrant up

First, let’s install Vagrant on the host computer (a MacOS X Mavericks here, using Homebrew Cask that can install binary install packages):

brew cask install vagrant

Then let’s create a directory where we’ll store the Vagrant related file for this host:

mkdir -p ~/dev/vagrant_trusty

cd ~/dev/vagrant_trusty

Create the shared folder

mkdir vagrant-docker

And let’s create this “Vagrantfile” file to describe our new VM in the Vagrant language:

Vagrant.configure(“2”) do |config|
config.vm.box = “trusty”
config.vm.box_url = “https://cloud-images.ubuntu.com/vagrant/trusty/trusty-server-cloudimg-amd64-juju-vagrant-disk1.box”
# we’ll forward the port 8000 from the VM to the port 8000 on the host (OS X)
config.vm.network :forwarded_port, host: 8000, guest: 8000
config.vm.synced_folder(“vagrant-docker”, “/vagrant”)

# add a bit more memory, it never hurts. It’s VM specific and we’re using Virtualbox here.
config.vm.provider :virtualbox do |vb|
vb.customize [“modifyvm”, :id, “–memory”, 2048]
end
end

Now it’s done, we can create the VM:

$ vagrant up
Bringing machine ‘default’ up with ‘virtualbox’ provider…
==> default: Box ‘trusty’ could not be found. Attempting to find and install…
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Adding box ‘trusty’ (v0) for provider: virtualbox
default: Downloading: https://cloud-images.ubuntu.com/vagrant/trusty/trusty-server-cloudimg-amd64-juju-vagrant-disk1.box
default: Progress: 0% (Rate: 1271k/s, Estimated time remaining: 0:08:32)
default: Progress: 1% (Rate: 1532k/s, Estimated time remaining: 0:07:15)
default: Progress: 41% (Rate: 1520k/s, Estimated time remaining: 0:04:10)
default: Progress: 81% (Rate: 1532k/s, Estimated time remaining: 0:01:18)
default: Progress: 84% (Rate: 1545k/s, Estimated time remaining: 0:01:06)
==> default: Successfully added box ‘trusty’ (v0) for ‘virtualbox’!
==> default: Importing base box ‘trusty’…
==> default: Matching MAC address for NAT networking…
==> default: Setting the name of the VM: vagrant_default_1407250776484_38665

==> default: Clearing any previously set forwarded ports…
==> 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 => 2122 (adapter 1)
default: 80 => 6080 (adapter 1)
default: 8001 => 18123 (adapter 1)
default: 22 => 2222 (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:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying…
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM…
==> default: Configuring and enabling network interfaces…
==> default: Mounting shared folders…
default: /vagrant => /Users/user/dev/vagrant/vagrant-docker
==> default: Running provisioner: shell…
default: Running: inline script
==> default: You have not informed bzr of your Launchpad ID, and you must do this to
==> default: write to Launchpad or access private data. See “bzr help launchpad-login”.
==> default: Branched 13 revisions.
==> default: Bootstrapping Juju…
==> default: uploading tools for series [precise trusty]
==> default: Logging to /home/vagrant/.juju/local/cloud-init-output.log on remote host
==> default: Bootstrapping Juju machine agent
==> default: Starting Juju machine agent (juju-agent-vagrant-local)
==> default: Taking a nap to let state server come up…
==> default: Added charm “cs:trusty/juju-gui-3” to the environment.
==> default: Setting up Juju GUI dependencies…this may take a minute.
==> default: You can find the log in /var/log/juju-setup.log
==> default: You have not informed bzr of your Launchpad ID, and you must do this to
==> default: write to Launchpad or access private data. See “bzr help launchpad-login”.
==> default: Branched 25 revisions.
==> default: Taking a nap to let Juju Gui get setup
==> default:
==> default: Setting up transparent redirect for Juju-Gui
==> default: Redirecting localhost:80 to 10.0.3.77:80
==> default:
==> default: Executing: sudo iptables -t nat -A PREROUTING -p tcp –dport 8001 -j DNAT –to 10.0.3.77:80
==> default: Executing: sudo iptables -t nat -A POSTROUTING -j MASQUERADE
==> default:
==> default: From your browser on your hosts, you should be able to
==> default: access the GUI by going to: http://127.0.0.1:6080
==> default:
==> default: Your password is: XXXXXXXX
==> default: juju-gui start/running, process 13405
==> default: juju-gui start/running, process 13405

$

If you have some problems about conflicting local host and guest ports, it’s ONE of the different Vagrantfile being used that may be the cause of your trouble: https://stackoverflow.com/questions/10953070/how-to-debug-vagrant-cannot-forward-the-specified-ports-on-this-vm-message/25142933#25142933

Watch out, your Vagrantfile is **not the only one** being used when bringing up a Vagrant box/instance.

When you get this:

~/dev/vagrant user$ vagrant reload
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 8001 is already in use
on the host machine.

To fix this, modify your current projects Vagrantfile to use another
port. Example, where ‘1234’ would be replaced by a unique host port:

config.vm.network :forwarded_port, guest: 8001, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn’t allow modifying port forwarding.
~/dev/vagrant user$

You are actually not only using the Vagrantfile from ~/dev/vagrant but also the one from your “box” distribution .box file which is typically located here:

~/.vagrant.d/boxes/trusty/0/virtualbox/Vagrantfile

And if you have a look at it you’ll see it has plenty of **default** port mappings:

$ cat ~/.vagrant.d/boxes//trusty/0/virtualbox/Vagrantfile
$script = <<SCRIPT
bzr branch lp:jujuredirector/quickstart /tmp/jujuredir
bash /tmp/jujuredir/setup-juju.sh
SCRIPT

Vagrant.configure(“2”) do |config|
# This Vagrantfile is auto-generated by ‘vagrant package’ to contain
# the MAC address of the box. Custom configuration should be placed in
# the actual ‘Vagrantfile’ in this box.

config.vm.base_mac = “080027DFD2C4”
config.vm.network :forwarded_port, guest: 22, host: 2122, host_ip: “127.0.0.1”
config.vm.network :forwarded_port, guest: 80, host: 6080, host_ip: “127.0.0.1”
config.vm.network :forwarded_port, guest: 8001, host: 8001, host_ip: “127.0.0.1”
config.vm.network “private_network”, ip: “172.16.250.15”
config.vm.provision “shell”, inline: $script

end

# Load include vagrant file if it exists after the auto-generated
# so it can override any of the settings
include_vagrantfile = File.expand_path(“../include/_Vagrantfile”, __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)

So, go ahead and edit this file to remove the offending colliding forwarding port(s):

config.vm.network :forwarded_port, guest: 22, host: 2122, host_ip: “127.0.0.1”
config.vm.network :forwarded_port, guest: 80, host: 6080, host_ip: “127.0.0.1”
# config.vm.network :forwarded_port, guest: 8001, host: 8001, host_ip: “127.0.0.1”

By:

~/dev/vagrant user$ cp ~/.vagrant.d/boxes//trusty/0/virtualbox/Vagrantfile ~/.vagrant.d/boxes//trusty/0/virtualbox/Vagrantfile.old
~/dev/vagrant user$ vi ~/.vagrant.d/boxes//trusty/0/virtualbox/Vagrantfile

and watch out for other Vagrantfiles inclusion i.e.:

include_vagrantfile = File.expand_path(“../include/_Vagrantfile”, __FILE__)

And now it works:

$ vagrant up
Bringing machine ‘default’ up with ‘virtualbox’ provider…
==> default: Importing base box ‘trusty’…
==> default: Matching MAC address for NAT networking…
==> default: Setting the name of the VM: vagrant_default_1401234565101_12345
==> default: Clearing any previously set forwarded ports…
==> 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 => 2122 (adapter 1)
default: 80 => 6080 (adapter 1)
default: 22 => 2222 (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:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying…
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM…
==> default: Configuring and enabling network interfaces…
==> default: Mounting shared folders…
default: /vagrant => /Home/user/dev/vagrant/vagrant-docker
==> default: Running provisioner: shell…
default: Running: inline script

Type then:

vagrant ssh

And you will be connected.

Enjoy your Vagrant install of Ubuntu Server 14.04 LTS (Trusty Tahr).

Packaged with this Ubuntu Server 14.04 LTS (Trusty Tahr), you will get Juju and Docker pre-installed. This will enable you to connect to http://127.0.0.1:8001 to access Juju web UI.

Update:

Then you want to run Docker. Two ways to do that:

  1. Run Docker from within Linux Ubuntu 14.04
  2. Run Docker from within Mac OS X with Boot2Docker

 

Leave a Reply