destroying container after build success

0 votes
i have created a jenkins master and slave images in docker and spinned those images. both the containers are running. i have pushed them in my private registry. here the scenario is i have to take them in jenkins pipieline script both master and slave.agent should be the slave here, and then the the code should get cloned from git repository after that the image should build, next the container should get destroyed after the build success. for this i need a pipeline script.
Jan 16 in DevOps Tools by anonymous

recategorized Jan 16 by Vardhan 49 views
hey, did Hannah's suggestion work?

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

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()
            }
       }
}
answered Jan 17 by Hannah
• 14,080 points

Related Questions In DevOps Tools

0 votes
2 answers
0 votes
1 answer

how to fix the build with maven in bluemix

As per Bluemix Status Page today DevOps Delivery Pipeline ...READ MORE

answered Jul 6, 2018 in DevOps Tools by Kalgi
• 36,220 points
46 views
0 votes
1 answer

Build error in TF build definition

I think the problem is in your ...READ MORE

answered Aug 3, 2018 in DevOps Tools by ajs3033
• 7,080 points
594 views
+5 votes
7 answers

Docker swarm vs kubernetes

Swarm is easy handling while kn8 is ...READ MORE

answered Aug 27, 2018 in Docker by Mahesh Ajmeria
841 views
0 votes
1 answer
0 votes
1 answer

Unalble to pull a new container after upgrading Helm.

If there are changes to roll out, ...READ MORE

answered Apr 12, 2018 in Docker by shubham
• 6,560 points
44 views
+2 votes
5 answers
0 votes
1 answer

Empty json created after SonarQue webhook build

It isn't like SonarQube to send empty ...READ MORE

answered Jun 1, 2018 in Other DevOps Questions by ajs3033
• 7,080 points
28 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.