Sunday, February 8, 2015

Ratpack + Gradle + Docker


we'd like to put docker into the dev workflow and ship the docker images.  it's important the developer can develop/test on the docker image to get the max benefit out of docker in devops: no more "but it works on my machine."  since building the ratpack application is using gradle, it'd make sense to add docker plugin to gradle build script.  (developer has less command to deal with)

inspired by this post http://kyleboon.org/blog/2014/08/14/ratpack-plus-docker-plus-gradle/, i started to do the same thing but choose the bmuschko gradle plugin https://github.com/bmuschko/gradle-docker-plugin instead.  it's using later version of docker-java https://github.com/docker-java/docker-java.  and can do much more than just creating an image and publish to docker registry.

Add the followings to your build.gradle -
you can put the docker.url & registryUrl in gradle.properties, so it won't be hardcoded

Add the Dockerfile to your project root directory
To minimize the file size, we use busybox.
docker images

REPOSITORYTAGIMAGE IDCREATEDVIRTUAL SIZE
msvc-search1.014c228ee14356 seconds ago197.9 MB
hystrix-dashboardlatest6b93ff8445719 hours ago479.8 MB
centos7dade6cb4530a2 days ago210.1 MB
registrylatestc55308716b3610 days ago414.2 MB
ubuntulatest5ba9dab4745910 days ago188.3 MB
peelsky/zulu-openjdk-busyboxlatest050380da31ed13 days ago181.8 MB
progrium/busyboxlatest8cee90767cfe2 weeks ago4.789 MB
atcol/docker-registry-uilatest28d7d683c7a95 weeks ago908.6 MB

progrium/busybox is just 5MB! And we have license issue to ship with oracle jdk, so we use openjdk. But appears openjdk are only distributed through yum/apt and i can't find any way to install that on busybox as it only has opkg. Zulu http://www.azulsystems.com/products/zulu, is certified build of openjdk and it's 100% opensource and free. It also provides official docker image, however, they are not using busybox. Luckily someone already did that! thanks to peelsky/zulu-openjdk-busybox.  with the jdk it's mere 182MB.

Also, since the Dockerfile is put on the project root, it'll tar up the whole project and send to the docker deamon while we only need the fat jar!  to avoid that, add .dockerignore to your project root.

Now when you run ./gradlew --info buildDocker, only the necessary file are sent to the docker daemon.

Sending build context to Docker daemon 16.12 MB
Sending build context to Docker daemon
Step 0 : FROM peelsky/zulu-openjdk-busybox
 ---> 050380da31ed
Step 1 : WORKDIR /app
 ---> Using cache
 ---> 954a21f6409e
Step 2 : USER daemon
 ---> Using cache
 ---> 208cd5606087
Step 3 : ADD ./build/libs/msvc-search-1.0-all.jar /app/msvc-search-1.0-all.jar
 ---> 6e11e87fb593
Removing intermediate container c109ca327f00
Step 4 : CMD java -jar /app/msvc-search-1.0-all.jar
 ---> Running in bea41332ae6a
 ---> 82e41217ecd2
Removing intermediate container bea41332ae6a
Step 5 : EXPOSE 9500
 ---> Running in 40d91cd09bd0
 ---> b4e042fa2a6e
Removing intermediate container 40d91cd09bd0
Successfully built b4e042fa2a6e

16MB is pretty much the size of the fat jar.