Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Build - How to prune docker images on VM?

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?

like image 527
vine Avatar asked Oct 16 '22 11:10

vine


1 Answers

SOLUTION

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
like image 62
vine Avatar answered Oct 20 '22 15:10

vine