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 54 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 by Hannah
• 14,090 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
• 39,310 points
62 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,280 points
758 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 24 in DevOps Tools by Neel
• 2,690 points
29 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
980 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,890 points
60 views
+2 votes
5 answers