Hey,
Define your pipeline to clone the git code, build your image and then destroy the image post-always. To delete your containers after build you need to define a variable container outside the pipeline and the use it anywhere inside the pipeline to either start or stop the container.
def container
pipeline {
agents if any
node {
checkout scm
docker.withRegistry('https://registry.example.com', 'credentials-id') {
def customImage = docker.build("my-image:${env.BUILD_ID}")
container = customImage.run("-d -u 0:0 -v ")
/* Push the container to the custom Registry */
customImage.push()
}
}
post {
always {
script {
container.stop()
}
}
}