I'm using Google Cloud Platform with Cloud Build and cloudbuild.yaml for software deployments. The target VM always has the Google Container Optimized OS (COS).
The basic thing we do, is to update a running docker container with a new container.
- name: 'gcr.io/cloud-builders/gcloud'
args: ['compute','instances','update-container','my-vm-id','--zone','europe-west3-c','--container-restart-policy=always','--container-image=gcr.io/cloud-02/my-vm-id','--container-mount-host-path=host-path=/var/extdata,mount-path=/var/extdata,mode=rw']
But in this case, the old docker images remain on the Host-VM. I can list them on the Host-VM with docker image list. Since the images are in /var/lib/docker, the files are stateful and are'nt gone after a restart.
So, I don't get it, how can I prune the docker image files on the Host-VM within the deployment process?
I found a way to get this task done. I'm using add-metadata to add a startup-script to the meta-header of the vm. You can put the startup-script to your repository if it's cloned into the pipeline in a previous step. In my case the startup-script can be found in git folder /build/compute-engine. /workdir is the default path of cloud build's working pipeline.
The task is now executed everytime the build is triggered.
cloudbuild.yaml:
- name: 'gcr.io/cloud-builders/gcloud'
args: ['compute','instances','add-metadata','my-vm-id','--zone','europe-west3-c','--metadata-from-file=startup-script=/workspace/build/compute-engine/startup-vm.bash']
startup-vm.bash:
#! /bin/bash
/usr/bin/docker image prune -a -f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With