2
votes

We're using the Cloudbees Docker Build and Publish plugin to build Docker images in our Jenkins instance. The builds are working fine and we're pushing to Docker Hub successfully, but the images are sticking around on the Jenkins slave and causing space issues.

Is there an option to remove the images after a successful build and push? Thanks.

1
Looking at their code (1.2.2), there should be an Additional Build Arguments textfield where you can add build flags like --rm. - ivan.sim
Thanks for the reply. We do have that under Additional Build Arguments and it is removing intermediate containers (i.e docker build --rm=true ....) But I'm also seeing the following in the build output. Step 41 : CMD /usr/lib/systemd/systemd ---> Running in 9454f885701d ---> 0cee7a3ced47 Removing intermediate container 9454f885701d It's not removing Ocee7a3ced47 and that's rather large (~ 1GB) and there's several of them like that each build. Is it possible to remove those or are they required for the parent image? - Michael Walker
sorry about the formatting - Michael Walker
I can't tell if 0cee7a3ced47 is a parent image. Have you tried other ways to reduce your image size such as using a smaller base image, chain multiple RUN with &&, use multiple variables for ENV, EXPOSE etc. Every instruction in your Dockerfile adds a layer to your container image. - ivan.sim

1 Answers

0
votes

Like you said, you need to have --rm as an Additional Build Arguments in advanced section of Cloudbees Docker Build and Publish plugin to get rid of those intermediate container but images that you build and push to repo will still remain on your host. Simple fix would be to add a build step and execute a shell command like this to delete those images:

docker rmi ACCOUNT/IMAGE:${BUIL_NUMBER}

Assuming you're tagging your images with Jenkins BUILD_NUMBER, you can replace it with whatever variable that you use.