Elessar.perm
Elessar.perm

Reputation: 811

Jenkins pipeline is unable to terminate a docker container

I have a docker container that performs some tasks and is scheduled inside Jenkins pipeline like this:

pipeline {
    stages {
        stage('1') {
            steps {
                sh "docker run -i --rm test"
            }
        }
    }
}

If the pipeline is aborted somehow, by timeout or manually for example, the container won't stop and stays alive.

How do I configure it to be terminated along with pipeline?

Docker version 17.06-ce

Upvotes: 2

Views: 3625

Answers (1)

Guel135
Guel135

Reputation: 788

Hi Elessar you can configure an "always" in the post steps. Mainly it will run the command inside always without depending on the build cancelation, fail or success.

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                 sh "docker run -i --rm test"
            }
        }
    }
    post { 
        always { 
            sh "docker stop test" //or something similar 
        }
    }
}

I hope this solve your problem!

Upvotes: 2

Related Questions