Pre-populating GCE persistent disks while building and/or before running Docker image on GKE

Solution 1:

The obvious answer is to populate each persistent disk immediately after creating it.

If the application configs change from build to build, and they must match the running build, then there's an unresolved problem about what to do if multiple app versions share the same PD and conflict over what should be stored there.

If you don't need to worry about cross-version PD sharing, then you can initialize the contents of the PD using a job running in the application's pod. Kubernetes has a feature called init containers designed to make this easier; but it's still alpha at the time of writing.

Solution 2:

I did not hear about best practices, so this is what I have adopted for now:

  1. docker build the image with Dockerfile that also tars e.g. /etc/<application>/ into <application>.tar after it has done its other build steps
  2. briefly docker run the image and scp tar files off the running image
  3. briefly create a temporary VM instance and add/attach the PD to it; scp tar files to the VM instance; gcloud compute ssh into tit, mount the PD, and untar needed files below mount point