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

[Cloud Native Blog - Container Solutions ](https://blog.container-solutions.com)

# [How to build Docker images with Gradle](https://blog.container-solutions.com/how-to-build-docker-images-with-gradle)

 Written by [Frank Scholten](https://blog.container-solutions.com/author/frank-scholten) | Jun 5, 2015 1:03:42 PM

<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)

[View full post](https://blog.container-solutions.com/how-to-build-docker-images-with-gradle)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Frank Scholten"
  },
  "dateModified" : "2019-07-09T13:56:25.950Z",
  "datePublished" : "2015-06-05T13:03:42Z",
  "headline" : "How to build Docker images with Gradle",
  "image" : {
    "@type" : "ImageObject",
    "height" : 612,
    "url" : "http://cdn2.hubspot.net/hubfs/2668666/All-InOne-2016/Image/bg-img.jpg",
    "width" : 1600
  },
  "mainEntityOfPage" : "https://blog.container-solutions.com/how-to-build-docker-images-with-gradle",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60.0,
      "url" : "https://info.container-solutions.com/hubfs/Container_Solutions_Logo.png",
      "width" : 197.59778
    },
    "name" : "Cloud Native Blog "
  }
}
```