How To Use Docker In Your Development Environment

Docker makes it easy to create, deploy and run applications as portable, self-sufficient containers that can virtually run everywhere. By using Docker you can separate your application from its environment, for example differences in operating systems. With the use of virtual machines you also have this option but there are two important benefits with docker: firstly, it’s way faster than a VM and secondly it uses less resources so your laptop doesn’t heat up as much!

In this tutorial, I will show you how to run Docker in your development environment.

First up, let’s have a quick look at what Docker is actually doing for us.

Docker provides an easy way to create lightweight, portable, self-sufficient containers from any application. The same container that a developer builds and tests on a laptop can run at scale, in production, on VMs , bare metal, OpenStack clusters , public clouds and more. –

So basically there are two major things that it does:

Using a lot of tools like Puppet or Chef we would need some kind of virtual machine (or Vagrant) to set our environment up with all the neccessary services (Webserver etc.). With Docker you only need to do this once for all your servers and it is already running with all the services you need. It will create a container that is sandboxed in its own environment, separated from the other containers which are running on the same machine. So your application is still able to work (if written correctly) even if there are some programs/services installed in the host machines that don’t like each other .

What we’re going to do:

Install Docker on our Host machine (which can either be Windows or Linux) Create an image of Ubuntu with Apache2 installed  (We will use this image later when creating new containers). Run a new container based on our image inside VirtualBox . Install Mysql inside the new container and create a database.

Finally we will be able to access our application from the host machine via browser .

Let’s start!

On Windows:

Open PowerShell as administrator and run the following commands one by one to install Docker on your machine: Invoke-WebRequest -OutFile “${env:ProgramFiles}\Docker\Docker\bootstrap.exe” Dismount-WindowsImage -Path C:\Temp -Save If you haven’t installed VirtualBox already, do it now ! Install Vagrant  (which is a tool used for creating/running portable development environments). Download all necessary Vagrant boxes (Ubuntu Trusty 64 Bit) : Invoke-WebRequest -OutFile Create a new development environment in PowerShell with Vagrant : New-Vagrantfile

# -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = “2” Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| = “trusty64” end vagrant up

After the Vagrant box is created (and every time you want to use it), run the following command within the same directory as your Vagrantfile :

  Invoke-WebRequest -OutFile

Now we are ready to create our development container inside VirtualBox ! First, lets start the virtual machine (and disconnect it from the network):

  vagrant up –no-provision

  vagrant ssh

Now install Docker in this Ubuntu Trusty VM:

sudo apt-get update sudo apt-get install lxc-docker

After that is done, run the following command to make sure everything was installed correctly: docker images

It should come back with an empty list not including any layers (You might see some if you’ve already downloaded images). Now create a new image out of our Vagrant box . It will take some time to download everything (the Ubuntu Trusty box has around 1.5 GB):

docker commit $( docker ps -lq )  ubuntu-apache

Now you can work with your new image anywhere (simply by typing the name, like any other Linux command). For example: docker run -d –name=my-application-container  -p 8080:80   ubuntu-apache

We are now able to access our application at http://localhost:8080 via browser !

On Linux:

I will use DigitalOcean as an example here but there are many more providers out there which all have their own procedures for installing Docker. Other providers should be just easy to install it though.

For installing it on DigitalOcean you can go here .

Now that we have Docker installed, lets create an image out of our Vagrant box. You will need to run all these commands in the directory where your Vagrantfile is located (or copy the whole directory over to your host machine). It might take some time (and depends on your internet connection) but once its done – you’ll be able to access your app via browser at http://localhost:8080 !

vagrant destroy  -f vagrant up –no-provision vagrant ssh sudo apt-get update sudo apt-get install lxc-docker mkdir ~/images docker commit $( docker ps -lq ) ubuntuapacheservice 

docker create -it –name=my-application-container  ubuntuapacheservice

Now you can run your new application container anywhere by simply typing the command docker run -d –name my-application-container  –link some-mysql:MySQL be  ubuntuapacheservice

You will able to access your app via browser at http://localhost:8080 ! πŸ™‚ Thats it! Now you have a Docker powered development environment on Windows AND Linux. I’m gonna write more about this in future posts. πŸ˜‰ Cheers!