vine
vine

Reputation: 147

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?

Upvotes: 6

Views: 1416

Answers (3)

Blair Nangle
Blair Nangle

Reputation: 1541

An alternative way to do things if you don't want to wait until VM restart to prune: send the docker command to the VM via gcloud and ssh. For example, to prune old images immediately after deploying the new image in my CI/D pipeline:

gcloud compute instances update-container <instance-name> --zone <zone> --container-image <artifact-registry-domain>/<repo>/<image-name>:<version>

Followed by:

gcloud compute config-ssh
gcloud compute ssh <instance-name> --zone <zone> --command "docker image prune -af"

Hat tip to Steve for this answer.

Upvotes: 1

vine
vine

Reputation: 147

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

Upvotes: 4

Alfredo F.
Alfredo F.

Reputation: 25

Reading your issue, it’s probably you need to use these steps to Creating custom build steps.

In this page explain how to use the cloudbuild.yaml for execute custom steps, for example to run a script.

You can deploy the new image and later execute the docker run command with your preferences

Regards.

Upvotes: -1

Related Questions