Home > Net >  Remove old containers for Google Container OS
Remove old containers for Google Container OS

Time:09-21

I'm hosting a Docker image inside of Google Container OS on Google Compute Engine. To deploy my container, I do this:

gcloud compute instances update-container example --container-image "$IMAGE_WITH_TAG"

On my latest deployment, I got this error:

failed to register layer: Error processing tar file(exit status 1): write /usr/share/man/man1/journalctl.1.gz: no space left on device

After SSHing into my Docker container, I ran docker images and noticed that Docker was still hanging on to my old images. I can manually delete these images, but the same problem is going to occur after a few more deployments.

Is there a way I can automatically remove old Docker images while deploying to Container OS via gcloud compute instances update-container?

CodePudding user response:

Currently, Compute Engine does not have a feature on cleaning up old and unused images automatically. It's not mentioned at the steps performed when updating a container on Compute Engine.

As of now, a workaround is to run the commands below to clear disks. You can also add on on your shutdown script:

docker rm  $(docker ps -q -a) \
docker images -q -f dangling=true | xargs docker rmi \
docker system prune --all

Above commands will do the following:

  1. Delete all stopped docker containers.
  2. Remove all dangling images.
  3. Remove all unused images and volumes.

Explanation of distinction between "dangling" and "unused" images.

This similar answer provides an example using prune command and another workaround to make sure that old images are cleared before updating the container image.

CodePudding user response:

You can try using the GCR Cleaner tool,a third party tool to help automatically delete the old images. All newly created images are tagged with “latest” so they won’t get accidentally deleted by the tool.

GCR Cleaner deletes untagged images in Google Cloud Container Registry or Google Cloud Artifact Registry. This can help reduce costs and keep your container images list in order.

GCR Cleaner is designed to be deployed as a Cloud Run service and invoked periodically via Cloud Scheduler. This is not an official Google product.

And also another way is using the prune command to automatically remove all resources that aren't associated with a container. This is a streamlined way to clean up unused images, containers, volumes, and networks.

In a terminal window, enter the following:

$ docker system prune

Additional flags can be included:

-a To include stopped containers and unused images

-f Bypasses confirmation dialog

--volumes Removes all unused volumes

Running docker system prune -a removes both unused and dangling images . Images used in a container, either currently running or exited, will NOT be affected.

You can also refer to the script to clean up Google Container Registry images pushed before a particular date.

  • Related