How to make your project workflow effortless with Docker (Part 2)

on

Part 1: Technologies that we will use and why

Part 2: Getting your local development environment started with Vagrant and Ansible

Part 3: Getting started with Docker and Jenkins

Part 4: Using Docker in AWS through Ansible

First steps

First we will need to install Vagrant and Virtualbox. After that download the project from Github.

Or you can clone the repo with git clone https://github.com/Robaum/hello-docker-jenkins-aws.git

And with just this 3 things you got everything you need to start working.

But how? what? Is that all? Yep. First hold your horses lets start the VM first. Open up your terminal and go to the folder and type vagrant up. This will start the Vagrant box. And keep reading while it does its magic.

vagrant up

Beware: it takes a really long time to finish the first time since it has to download everything. You will normally will just do this process once so don’t worry.

Note: Keep the Virtualbox GUI open in case the process pauses. If it does just unpause it. I still haven’t figured out how to fix this so if you know please tell me.

Lets go though the 2 main files that make this possible. So first we have the Vagrantfile. This keeps all the information related to our Vagrant box.


# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "phusion-open-ubuntu-14.04-amd64"
  config.vm.box_url = "https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-14.04-amd64-vbox.box"
  # Or, for Ubuntu 12.04:
  #config.vm.box = "phusion-open-ubuntu-12.04-amd64"
  #config.vm.box_url = "https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-12.04-amd64-vbox.box"

  # Add port-forward for Express app
  config.vm.network :forwarded_port, guest: 3000, host: 3000

  # Add port-forward for Jenkins
  config.vm.network :forwarded_port, host: 8080, guest: 8080

  # Sync'd folder
  config.vm.synced_folder ".", "/vagrant"

  # Install Ansible and use as provision
  config.vm.provision "shell" do |sh|
    sh.path = "ansible/ansible.sh"
    sh.args = "ansible/provision.yml"
  end
end

The file is quite straight forward but let me explain some of them for better clarity.
First the config.vm.box = “phusion-open-ubuntu-14.04-amd64” will download a Ubuntu 14.04 VM that was configured to work better with Docker on Vagrant.

Then we forward the ports 3000 for the Node app and 8080 for Jenkins.

Then we will sync the current folder with the folder vagrant will create in the VM. So any changes we make to our files in our machine will be reflected automatically. This step is done by default if you don’t want to add it.

Finally it will run the ansible.sh script you see below. With an Ansible playbook called provision.yml.


#!/usr/bin/env bash
#
# Uncomment if behind a proxy server.
# export {http,https,ftp}_proxy='http://username:password@proxy-host:80'

# Playbook dir
ANSIBLE_PLAYBOOK=$1

# Is a good practice to always update before installing
sudo apt-get update

# Install Ansible
echo "Installing Ansible"
sudo apt-get install -y software-properties-common
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible

# Run the playbook.
echo "Running Ansible provisioner defined in Vagrantfile."
sudo ansible-playbook /vagrant/${ANSIBLE_PLAYBOOK} -c local

This script will install Ansible for Ubuntu 14.04 inside the newly created VM and then run the Ansible playbook provision.yml. The provision file will install java, Docker and the Jenkins container with the appropriate configuration.

If you want to see all the steps Ansible does to make this possible you can browse the Ansible folder.

After it finished then we can ssh into our new Vagrant box. In your terminal inside the folder run vagrant ssh

vagrant ssh

And now you should be inside the Ubuntu VM. And everything should be running as expected. You can try going to localhost:8080 and you will see the Jenkins server.

jenkins

If you want to exit the Vagrant VM you can type exit. And in the Host OS you can stop or destroy the VM with vagrant halt and vagrant destroy.

vagrant halt

Every time you stop the VM Docker will also stop, therefore the Jenkins server will not be up when you start the VM again. To redo the provision process you can run sudo ansible-playbook /vagrant/ansible/provision.yml. Don’t worry, this time it will be faster since Ansible is smart and only applies changes.

ansible provision

And this is all for our development environment. If you want to learn more about Vagrant and Ansible please refer to the official tutorials (Vagrant, Ansible). If you want me to explain something in more detail please leave it in the comments and I’ll revisit this tutorial.

Well that’s all for now. I hope you liked it, on the next tutorial we will go over the basics of Docker by making our Node app inside a container and how to make Jenkins build, test and upload that container to Dockerhub.