0

How to clear obsolete docker images created via Jenkins CI/CD?

I have created a CI/CD Jenkins Pipeline which does the following tasks

  1. Run a gradle build. The gradle build does the next set of tasks
    • Build several springboot microservices
    • Create a docker image for each of the micro-service
    • It pushes the images to a private Docker registry
  2. Execute helm templates to create/refresh the k8s deployments in a k8s cluster.

While the entire process works well. I am looking for a solution for a couple of scenarios.

  1. Since it is CI/CD the build is triggered for every code push. So, the docker images in the private registry will be created as well and eventually eat up all the disk space. How do I conditionally clear the docker images?
  2. If a script is developed to use the docker REST APIs to clear the images how do I conditionally skip to delete certain images (ex: images related to tagged Jenkins builds)
  3. Are there any recommendations or standards for this task?
junelane
  • 101
  • 3
  • 13

2 Answers2

1

One way of addressing this issue is to have a different jenkins job especially for the purpose of deleting older images.

That job would probably trigger on some appropriate schedule, say every night, once a week and so on, depending on how quickly you're worried you'll run out of space.

As to how you would delete the images, take a look at the docker image prune command, with the --filter option, as explained in this answer. That will allow you to only delete images, for example, older than 7 days, etc.

Hope that helps!

gsaslis
  • 3,066
  • 2
  • 26
  • 32
  • docker image prune is to clear the images residing on Jenkins slave. I want to clear the images that's inside private Docker registry. – junelane Jun 04 '19 at 09:25
  • the `docker` command is really a client that communicates with a docker server. Now, yes, in most cases it communicates with the server that is running inside the same VM, but you can configure where the docker client connects to using the `DOCKER_HOST` env var - e.g. see here: https://docs.docker.com/engine/reference/commandline/cli/#environment-variables and here for an example how to set it up: https://nickjanetakis.com/blog/docker-tip-73-connecting-to-a-remote-docker-daemon – gsaslis Jun 05 '19 at 10:39
0

I think below should be the way to go forward

  1. Find all the containers

    docker ps -a -f "your condition"

  2. Then stop and remove all containers which you found with below commands

    docker stop "container name" docker rm "container name"

  3. find all dangling images

    docker images -f "dangling=true"

  4. Remove all images

    docker rmi "image name"

psi
  • 269
  • 2
  • 13
  • This works for the images on the VM/machine but not for docker registry – junelane Jun 04 '19 at 09:26
  • Is your docker registry available on same VM/machine where you create images? – psi Jun 04 '19 at 10:39
  • no. The docker images are created in the Jenkins slave machine and the pushed to a private registry running in a different VM – junelane Jun 06 '19 at 04:18
  • refer https://stackoverflow.com/questions/25436742/how-to-delete-images-from-a-private-docker-registry and answer is posted by Yavuz Sert – psi Jun 06 '19 at 04:36