简体   繁体   中英

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

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?

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!

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM