3

I have a ci/cd automation in my project with gitlab, and after push my code on master branch, gitlab runner create a new docker image on my server and set latest commit hash as tag on that image, and recreate container with new image. after a while, there is a lot of unused image, and I want to delete them automaticly.

I delete old image manually.

This is my Makefile

NAME   := farvisun/javabina
TAG    := $$(git log -1 --pretty=%h)
IMG    := ${NAME}:${TAG}
LATEST := ${NAME}:latest

app_build:
    docker build -t ${IMG} -f javabina.dockerfile . && \
    docker tag ${IMG} ${LATEST}

app_up:
    docker-compose -p farvisun-javabina up -d javabina

And after all of this, I want a simple bash code, or other tools, to delete unused image, for example keep 3 lastest image, or keep last 2 day past builds, and delete others.

  • 1
    Possible duplicate of [How to remove old and unused Docker images](https://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images) – David Maze Sep 23 '19 at 10:24

3 Answers3

6

If you are fine with keeping a single image, you can use docker image prune -f, which will remove all images but the ones associated with a container, so if you run this command while the container is running, it will remove the rest of the images.

Don't forget to run docker system prune every often as well to further reduce storage usage.

In your situation where you need to keep more than one image, you can try this:

#!/bin/bash
for tag in $(docker image ls | sed 1,4d | awk '{print $3}')
do
    docker image rm -f $tag
done

The first line will list all docker images, remove from the list the first 3 ones which you want to keep, and select only the column with the image ID. Then, for each of the IDs, we remove the image.

If you want to remove more, change the 4d for another number. Notice that the first line is a header, so it must always be removed.

If you want to filter the images by a tag first, you can make your own filters.

Marc Sances
  • 2,402
  • 1
  • 19
  • 34
0

You can schedule (e.g. once a day or once a week) in the compilation machine a "docker image prune" command. https://docs.docker.com/engine/reference/commandline/image_prune/

Andrea
  • 335
  • 1
  • 9
  • this solution delete all unused images, but I want keep 3 or 5 or other number of them, and delete others. or I want to keep last week builds image, and delete others. when I execute docker image prune, delete all unused image. – Mohammad Askari Sep 23 '19 at 07:19
  • @MohammadAskari you could filter by label or timestamp, have you looked at prune command option ? – Andrea Sep 23 '19 at 07:21
  • I'm looking for that filter solution, and there is multistage build, and if I use prune command, docker remove some images that I need it for my builds. – Mohammad Askari Sep 23 '19 at 07:23
0

Here is a way we can remove old images, after a new successful build in our pipeline

# build
- docker build -t APP_NAME:$(git describe --tags --no-abbrev) .

# docker tag
- docker tag APP_NAME:$(git describe --tags --no-abbrev) artifactory.XYZ.com/APP_NAME:latest

# remote old images
- sha=$(docker image inspect artifactory.XYZ.com/APP_NAME:latest -f '{{.ID}}')
- image_sha=$(echo $sha | cut -d':' -f2)
- image_id=$(echo $image_sha | head -c 12)
- docker image ls | grep APP_NAME | while read name tag id others; do if ! [ $id = $image_id ]; then docker image rm --force $id; fi ; done
Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44