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 17, 2019 in DevOps Tools by anonymous

recategorized Jan 17, 2019 by Vardhan 1,547 views
hey, did Hannah's suggestion work?

1 answer to this question.

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, 2019 by Hannah
• 18,520 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
• 52,350 points
1,553 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,300 points
2,539 views
0 votes
2 answers

Can Jenkins detect and build new tags from Git repository?

Yes, this can be done in 3 ...READ MORE

answered Jun 25, 2019 in DevOps Tools by Neel
• 3,020 points
18,500 views
+1 vote
1 answer

i have a pyhton code in my git/git repository how to build that code in jenkins

Follow these steps @Bhaskar: Step 1: Open the Jenkins ...READ MORE

answered Oct 14, 2019 in DevOps Tools by Kalgi
• 52,350 points
914 views
0 votes
0 answers

Multiple YAML build pipelines in Azure DevOps

Using the new YAML way I want ...READ MORE

Mar 15, 2022 in DevOps Tools by Kichu
• 19,040 points
1,158 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
3,979 views
+2 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP