Setting up a development environment using Docker and Vagran

TL;DR

The first part of this blog post deals with the common flaws of development environments, the setup of a simple Docker environment and the benefits of a Vagrant+Docker configuration. But if you want to just start using Docker with Vagrant jump to thecorresponding section.

What’s wrong with development environments?It can take too long to set it up

How long does it takes for a new developer to setup your current project’s development environment? The answer may depends on many factors (the project age, the number of developers that have worked on it, etc…) but half a day or more is not an uncommon one.Hey! It should be much faster than that: checkout a script and execute it. That’s all. Two steps should be sufficient to setup your environment and get ready for development.

It can differ too much from testing and production environments

Have you ever skipped your build’s automated tests because they failed on your machine? Or even worst, have you ever been the cause of a build break even if your changes compiled smoothly on your machine but failed consistently on CI server?Any slight difference can result in an unexpected behaviour. Diverging can be as simple as giving a try to the last version of a framework or switching to a different project for half a day.Finding out what makes your system behave differently is an annoying task every developer should avoid.

Virtual environments and Docker

As a consequence development environment should have two characteristics:

Isolated: you don’t want to mess it up when testing some new tool or a different project.Repeatable: the same environment should be consistently reproducible on every team member machine and on CI and production servers.

Virtual environments ensure these features. But classic VMs are resource consuming. Developers need to code/build/test every few minutes and won’t accept the virtualization overhead.

Here is whereDockerbecomes helpful. Its lightweight containers are extremely fast compared to classic VMs and have become extremely popular among developers. Here is an excerpt from the Docker blog that explains the reasons for this success:

In its first 12 months Docker usage rapidly spread among startups and early adopters who valued the platform’s ability to separate the concerns of application development management from those of infrastructure provisioning, configuration, and operations. Docker gave these early users a new, faster way to build distributed apps as well as a “write once, run anywhere” choice of deployment from laptops to bare metal to VMs to private and public clouds.

Using Docker to configure an isolated and repeatable development environment

As an example we are going to setup a Docker container that can build and test aVert.xHTTP server.

Vert.x is a lightweight application framework that encourages architectures of small, independent micro-services. A micro-service is "just a little stand-alone executable that communicates with other stand-alone executables"(Uncle Bob). We think it fits perfectly in a Docker container and that is why we choose it as an example here.

If you haven’t already installed Docker do it first. You can refer to theofficial docor useget docker scriptto install it. We assume in this section that we are running on Linux. Even if Docker can be installed on Windows and Mac too (with boot2docker), we are going to see in the next session how to do that using Vagrant and why it can be a better choice.

Dockerfile

To describe a container we need a Dockerfile:

FROM ubuntu:14.04# Install dev tools: jdk, git etc…RUN apt-get updateRUN openjdk-7-jdk git wget# jdk7 is the default jdkRUN usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java /etc/alternatives/java# Install vertxRUN \ usrusr/local/vertx && \ # Add vertx to the pathENV PATH /usr/local/vertx/vert.x-2.1.2/bin:$PATHRUN usr/local/srcWORKDIR /usr/local/srcCMD

Dockerfiles are really straightforward and when you need to digg deeper there is an excellentonline reference manual.

FROM ubuntu:14.04defines the base image which we rely on. You can find a comprehensive list of Docker base images at thedocker hub. For this example we use the one used by thedocker teamto build Docker.

Subsequent lines describe modifications that will be applied on top of the base image:

Once we have copied the Dockerfile we can build the Docker image:

$ sudo docker build -t=vertxdev .Get the source code你曾经说,你曾经说。走在爱的旅途,我们的脚步多么轻松……

Setting up a development environment using Docker and Vagran

相关文章:

你感兴趣的文章:

标签云: