---
title: How to build Docker images with Gradle
description: 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.
image: http://cdn2.hubspot.net/hubfs/2668666/All-InOne-2016/Image/bg-img.jpg
---

Check out our **[Cloud Native Services](https://www.container-solutions.com/services) **and** **book a call with one of our experts today! 

[![Container Solutions](https://blog.container-solutions.com/hubfs/CS_blog/logo-main.svg "Container Solutions")](http://container-solutions.com)

- share:
- [**](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fblog.container-solutions.com%2Fhow-to-build-docker-images-with-gradle)
- [**](https://www.twitter.com/share?url=https%3A%2F%2Fblog.container-solutions.com%2Fhow-to-build-docker-images-with-gradle)
- [**](http://www.linkedin.com/shareArticle?mini=true&url=https://blog.container-solutions.com/how-to-build-docker-images-with-gradle)
- [**](https://blog.container-solutions.com/how-to-build-docker-images-with-gradle#)

[![](http://cdn2.hubspot.net/hubfs/2668666/All-InOne-2016/Image/bg-img.jpg) ](https://blog.container-solutions.com/how-to-build-docker-images-with-gradle)

[Docker](https://blog.container-solutions.com/tag/docker), [Microservices](https://blog.container-solutions.com/tag/microservices)

# How to build Docker images with Gradle

[Frank Scholten](https://blog.container-solutions.com/author/frank-scholten)

![Frank Scholten](https://blog.container-solutions.com/hubfs/frank.jpg)

 June 5, 2015

 4 minutes Read

[![How to build Docker images with Gradle - Gradle logo](https://blog.container-solutions.com/hs-fs/hubfs/Imported_Blog_Media/gradle1-1.jpeg?width=100&name=gradle1-1.jpeg)](https://blog.container-solutions.com/hubfs/Imported_Blog_Media/gradle1-1.jpeg) 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](https://github.com/bmuschko/gradle-docker-plugin) which we use to build the scheduler image for [Mesos Elasticsearch](https://github.com/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](https://registry.hub.docker.com/u/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](https://registry.hub.docker.com/u/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](https://github.com/mesos/elasticsearch). The project is sponsored by [Cisco Cloud Services](https://github.com/CiscoCloud).

### Enabling the Docker plugin in Gradle

Now let's talk about Gradle. To enable [@bmuschko](https://github.com/mesos/elasticsearch)'s Docker plugin add the following line to the top of *build.gradle*.

```

apply plugin: 'com.bmuschko.docker-remote-api'
```

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

```

buildscript {
  repositories {
   jcenter()
  }
  dependencies {
    classpath 'com.bmuschko:gradle-docker-plugin:2.2'
  }
}
	
```

### 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.

```

jar {
    baseName = "elasticsearch-mesos-scheduler"
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    manifest { attributes 'Main-Class': 'org.apache.mesos.elasticsearch.scheduler.ElasticsearchScheduler' }
}
 
task copyJar(type: Copy) {
    dependsOn   'jar'
    from        "build/libs/elasticsearch-mesos-scheduler-${project.version}.jar"
    into        'build/docker'
    rename { String fileName ->
        fileName.replace("-${project.version}", "")
    }
}
 
task buildDockerImage(type: DockerBuildImage) {
    dependsOn   copyJar
    if (System.env.DOCKER_HOST) {
        url = "$System.env.DOCKER_HOST".replace("tcp","https")
        if (System.env.DOCKER_CERT_PATH) {
            certPath = new File(System.env.DOCKER_CERT_PATH)
 
        }
    }
    else {
        url = 'unix:///var/run/docker.sock'
    }
    inputDir = file('.')
    tag = 'mesos/elasticsearch-scheduler'
}
 
build.dependsOn buildDockerImage
build.dependsOn copyJar
	
```

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](https://github.com/bmuschko/gradle-docker-plugin/tree/master/src/main/groovy/com/bmuschko/gradle/docker/tasks)

[### What is Strategy?

](https://blog.container-solutions.com/what-is-strategy)

[![prev-arrow](https://cdn2.hubspot.net/hubfs/3842749/Wow%202019/Images/left-arrow.png) Previous article](https://blog.container-solutions.com/what-is-strategy)

[ Next article ![next-arrow](https://cdn2.hubspot.net/hubfs/3842749/Wow%202019/Images/right-arrow.png) ](https://blog.container-solutions.com/puppet-in-docker)

[

### Puppet in Docker

](https://blog.container-solutions.com/puppet-in-docker)

Comments

Leave your Comment

![cs-logo-white](https://blog.container-solutions.com/hs-fs/hubfs/cs-logo-white.png?width=150&height=61&name=cs-logo-white.png "cs-logo-white")

#### **Talk to sales**

[info@container-solutions.com](mailto:info@container-Solutions.com)

#### Stay In Touch

- [**](https://www.linkedin.com/company/container-solutions/)
- [**](https://twitter.com/containersoluti)

© 2024 Container Solutions