GKE: Multi-stage dockerfiles inside Jenkins

To anyone reading this, we use multi-stage builds on top of GKE, this is how:

  1. We deploy a pod with docker:dind and run it using this args:

    - dockerd

    - --storage-driver=overlay2

    - -H tcp://0.0.0.0:2375

  2. We expose this pod as a service (dind-service)

  3. Each new job in jenkins create a new pod with a jnlp container (this is the default for jenkins on top of k8s) + our own custom container (base: FROM docker:18-dind)
  4. We config the DOCKER_HOST=tcp://dind-service:2375 inside the Jenkins job using withEnv.
  5. When we do: docker build . inside our job it use the daemon of dind pod.
  6. It gave us great cache and performance + allowed us to use multi stage build in gke :)

If you use Jenkins on top k8s I really advise you to read (helped a lot to get a better understanding) : https://akomljen.com/set-up-a-jenkins-ci-cd-pipeline-with-kubernetes


I think you need to wait until a GKE version is released that has a newer Docker version that has this feature (I believe multi-stage builds have started on a version like 16.04 or 16.10).


Even with Kubernetes 1.9.7, we are still stuck with Docker 17.03 which doesn't support multistage builds (available in 17.05). You can use GCP's Container Builder until we have proper Docker support.

Instead of

sh("docker build -t ${imageTagFrontEnd} .")
sh("gcloud docker -- push ${imageTagFrontEnd}")

you can invoke the container builder and push with

sh("gcloud container builds submit --tag ${imageTagBackEnd} .")

Remember that first 120 minutes are free for Container Builder and then there are after you would incur some charges.

Don't forget to authenticate the request, you need to include service-account file before you do anything, for eg:

sh("gcloud auth activate-service-account --key-file serviceAccountXYZ.json")