days
-1
-8
hours
-2
-2
minutes
-3
0
seconds
-3
-7
search
Docker + Eclipse =

Doclipser or how I’ve put Docker in your favorite IDE

Mario Loriedo
© doclipser

Docker has received a lot of attentions and that’s no surprise that developers want to see it integrated with their development tools. This article is about doclipser, a plugin we have developed at Zenika to add top-notch Docker support in the Eclipse IDE.

Docker for development environments

Before going into the details of doclipser, let’s argument why Docker is a useful development tool. How can we use Docker to help setting up and effectively use a development environment ? Well there is not just one way to do that and in this section we will have a look at two interesting use cases.

Build system

The first use case is using Docker as a build tool.

Docker allows us to make build environments portable, repeatable and isolated. Let’s take a C source file for example :hello.c. If you need to build it using gcc compiler, version 5.1, all you need to do is run the following command (assuming that you have Docker installed of course):

docker run gcc:5.1 gcc -o helloworld helloword.c

You can execute that command anywhere. You don’t have to bother about libraries, conflicts and you don’t have to install gcc v5.1. If it runs on your laptop it will run on the integration server too.

Runtime environment

The second use case for Docker is as a runtime platform. If you need to run a java web application (HelloWorld.war) using version 8 of Tomcat for example, you can just type:

docker run tomcat:8 \
    -v HelloWorld.war:/usr/local/tomcat/webapps/Helloworld.war

Again you don’t have to worry about dependencies, platforms configurations or discrepancies among Linux distributions. It just works.

Docker and IDEs

It should be obvious now how Docker can be a useful development tool. We have proved it with a bunch of shell commands. But we can be even more convincing: we will run Docker containers right from the IDE, we will integrate Docker within Eclipse.

Using doclipser

Doclipser is an Eclipse plugin we have developed earlier this year. The goal is to edit and run Docker containers from the IDE using Dockerfiles that are included in the project source files. In this section we will see how to install, use and configure doclipser.

Installation

To install doclipser just follow the usual Eclipse plugin installation procedure using the update site URL https://dl.bintray.com/zenika/doclipser.
Once the installation is completed you should be able to see 2 new installed features:

Doclipser - Dockerfile Editor and Doclipser - Docker API client.
doclipser2

Doclipser features

Editing Dockerfiles

The first feature of doclipser is the support of Dockerfiles. If you are not familiare with Docker, these files are used to create Docker images. They include all the instructions to build a Docker image as installing packages, creating users, exposing ports etc…

Dockerfiles use a specific DSL. We have used the Xtext framework to specify Dockerfiles grammar.

Dockerfile support includes syntax highlighting, autocomplete and syntax verification.

doclipser3

Dockerfile syntax validation

doclipser4

Dockerfile autocomplete

doclipser5

Dockerfile syntax highlighting

Running containers

Another cool feature of doclipser is the ability to run Docker containers from within Eclipse UI. When editing a Dockerfile, a few Docker commands are available from Run As menu items

doclipser6

Docker commands available from the Eclipse menu

Here is a description of the available commands:

Docker Build: run docker build using the folder containing the Dockerfile as the build context. The format for the name of the resulting Docker image will be <project>_<folder>.

Docker Run: executes a docker build command that creates the image followed by a docker run that creates and run a container using the image just built. The format of the name of the container will be <project>_<folder>_<n>.

Docker Logs: shows the logs of a Docker container launched using doclipser. No logs will be shown until the container is launched using doclipser.

Docker Ps: shows the list of currently running containers. Stopped containers are not shown.

Docker Rm: removes a Docker container previously run with doclipser.

Configuration

Doclipser configuration file can be found under <eclipse-installation-path>/ com.zenika.docker.api_<version>/docker.properties.

When running doclipser on Linux, and Docker is installed on the same host, there is no need to change the configuration file: the default values should work.

But when Docker is running on a remote host or on a Windows or OS X host the properties should be customized otherwise doclipser won’t be able to talk to Docker API.

doclipser7

doclipser configuration file

Here is a brief description of these properties:

docker.api.url: the URI of the Docker API. It can be the Docker socket (Linux default) or the IP address and port of the Docker API (Windows and Mac OSX defaults).

docker.api.cert.path: the filesystem path to the Docker related certificates.

docker.client.lib: the java docker API library that will be used with doclipser. This property is here for debugging purposes and there should be no need to change it.

Doclipser Development

Doclipser is an open source project. The source code is available on GitHub and people are invited to contribute to improve it. In this section we will review some of the implementation details and the tools used to build doclipser.

Dockerfile editor

The Dockerfile editor has been build using Xtext. For those that are not familiar with it, Xtext is a framework that makes it easy to define the grammar of a domain specific language. It covers all aspects of a complete language infrastructure, from parsers, over linker, compiler or interpreter to full Eclipse IDE integration.

The Dockerfile grammar we have built is called com.zenika.doclipser.dsl.DockerfileDsl and can be found in file DockerfileDsl.xtext. Here is the definition of the FROM instruction:

From:
    'FROM ' name=ID (':' tag=ID)?
;

Xtext comes with out of the box syntax coloring, autocomplete, validation and quick fixes based on the lexical structure and the semantic data. Moreover writing tests for the grammar is straightforward:

@Test 
def void parseFromWithTag() {
  var dockerfile = parser.parse("FROM busybox:latest")
  assertTrue(dockerfile.eResource.errors.empty);
  val from = dockerfile.instructions.head as From
  assertEquals("latest",from.tag)
}

This excerpt is taken from DockerfileParserTest.xtend and is written using Xtend programming language.

Running Docker commands

To communicate with the Docker engine we have used the Spotify Docker client for the JVM. This is a great library developed by Spotify’s development team that makes it easy to use Docker from a Java program:

// Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars
final DockerClient docker = DefaultDockerClient.fromEnv().build();
// Pull an image
docker.pull("busybox");

CI infrastructure

Doclipser build is completely automated. We use Maven Tycho to build Eclipse plugins : this allow us to build OSGI bundles automatically using maven but even manually using Eclipse IDE.

CircleCI is the continuous integration platform used to generate new artifacts as soon as a commit is merged on GitHub. And finally BinTray is used to distribute the plugin.

doclipser8

CircleCI build details

Plans for the future

We are currently focused on fixing issues and improve the stability of doclipser. The issues are opened on GitHub.

As soon as the issues will be fixed we will work on the support for Docker compose. This will be a tremendous feature allowing run and configure full application stacks using docker-compose.yml files.

Another cool feature that we want to implement soon are Dockerfiles Quick Fixes. These are the corrections that Eclipse suggests when the syntax of a Dockerfile has some errors.

Conclusion

I hope you have enjoyed this short introduction to doclipser. Don’t hesitate to contribute or contact us on GitHub.

Author
Mario Loriedo
Mario Loriedo ist CTO von Zenika, einer französischen Consulting-Firma für Java und Open-Source- Technologien. Früher arbeitete er als Softwareingenieur bei IBM und im öffentlichen Dienst in Italien. Er ist Sprecher auf internationalen Konferenzen und Autor der Plug-ins doclipser und sublime docker. Mario ist zertifizierter Docker-Trainer.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of