How to build Docker images with Gradle


by Frank Scholten

Gradle In this blog I will show you how to build Docker images from a Gradle task. A number of Docker plugins have been created for Gradle. I discuss the one from Benjamin Muschko which we use to build the scheduler image for Mesos Elasticsearch. This plugin is quite versatile because it provides Gradle tasks for almost every Docker CLI command.

Docker images used by Mesos Elasticsearch

In Mesos Elasticsearch we build two Docker images. The first image, mesos/elasticsearch-scheduler is an image which contains the executable jar of the Elasticsearch scheduler. By creating an image for it we can deploy it via Marathon. The second image, mesos/elasticsearch-cloud-mesos contains elasticsearch and the cloud-mesos plugin and it is used by containers launched by the scheduler. In this blog I will show how to build these containers with Gradle. If you want to know more about Mesos Elasticsearch itself, checkout the Mesos Elasticsearch Github page. The project is sponsored by Cisco Cloud Services.

Enabling the Docker plugin in Gradle

Now let’s talk about Gradle. To enable @bmuschko‘s Docker plugin add the following line to the top of build.gradle.

Then you also have to add the repository where Gradle can download the plugin from.

Docker Gradle tasks

Below I will explain how to build the mesos/elasticsearch-scheduler image. Have a look at the snippet from scheduler/build.gradle. I removed the dependencies and buildscript definitions as they are not relevant here.

There are 3 tasks. The first task, jar, builds the executable scheduler jar including it’s dependencies. Before we can build the Docker image we have to copy this jar to the build/docker folder. This is done by the second task: copy. The version is stripped so that the Dockerfile CMD is always java -jar elasticsearch-mesos-scheduler.jar . Finally in the third task we run the docker task. It has the type DockerBuildImage and it has a dependency on the copy task, sets the inputDir and connect either to DOCKER_HOST or uses the socket.

Update 01-07-2016 – Make sure you use the correct Docker context when you have multiple Docker Machines. Run ‘eval “$(docker-machine env)’. Kudos to Abhijit Sarkar for pointing this out in the comment below.

As you can see building Docker images with Gradle is relatively straightforward. For more information on other Docker tasks, checkout the other Docker Gradle task types


  1. Hi,
    I’m using the same plugin on a Mac with docker-machine and it kept failing because it couldn’t connect to docker. I spent several hours on the internet, trying several suggestions like modifying DOCKER_TLS in /var/lib/boot2docker/profile of the VM, to setting environment variables during docker-machine create, to even trying to set up a private network.

    In the end, yours is the only solution that worked for me, just using the environment variables already set by docker machine. Thank you.

    Only caveat of this solution is that the build must be running in the VM context you want. If you’ve multiple VMs, this means running eval “$(docker-machine env ) before the build. You may want to update your post to add that information.


Leave a Reply

Your email address will not be published. Required fields are marked *