Monday, June 1, 2015

Pushing Docker containers to more than one registry using Gradle

In the early days, we had to maintain two docker registries, one in AWS us-east-1 and the other in eu-west-1. This required us to build our containers and then push it to both registries. Below is a build.grade using Ben’s Gradle-Docker plugin to do the following:
  1. Builds a docker container based on the Dockerfile in the root directory.
  2. Tag and push to both repos (us-east-1 and eu-west-1).
The root directory (project folder) should contain your Dockerfile and build.gradle.

This is how the build.grade looks like:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.bmuschko:gradle-docker-plugin:2.3.1'
}
}
apply plugin: 'com.bmuschko.docker-remote-api'
import com.bmuschko.gradle.docker.tasks.container.*
import com.bmuschko.gradle.docker.tasks.image.*
def dockerRepo = "dockerregistry.us-east-1.mycompany.net:7001/offline-pipeline-jenkins"
def euDockerRepo = "dockerregistry.eu-west-1.mycompany.net:7001/offline-pipeline-jenkins"
repositories {
mavenCentral()
}
docker {
if ("JENKINS_HOME" in System.env) {
url = "unix:///var/run/docker.sock"
} else {
url = "http://192.168.59.103:2375"
}
}
task buildImage(type: DockerBuildImage) {
inputDir = file('.')
tag = "${dockerRepo}"
}
task pushImage(type: DockerPushImage) {
dependsOn buildImage
imageName = "${dockerRepo}"
tag = "latest"
}
task tagDocker(type: DockerTagImage) {
dependsOn buildImage
force = true
imageId = buildImage.getTag()
repository = "${euDockerRepo}"
tag = "latest"
}
task pushImageToEu(type: DockerPushImage) {
dependsOn tagDocker
imageName = "${euDockerRepo}"
tag = "latest"
}
task pushImages() {
dependsOn pushImage, pushImageToEu
}
view raw build.gradle hosted with ❤ by GitHub


Note that you can also build this on Jenkins.

No comments:

Post a Comment