How to prevent from docker to stop/remove the container on error











up vote
1
down vote

favorite
1












Consider I have a Jenkins groovy code that runs docker container and some bash will run inside of it:



    docker.withRegistry(dockerRegistry, dockerCredentials) {
docker.image(new String(dockerImage + ":" + dockerVersion)).inside(mountCommand) {

try {
withEnv(["AWS_PROFILE=${mappings['stackEnv']}",
"FULL_PACKAGE_VERSION=${mappings['revision']}",
"ARTIFACTORY_USER=${IC_ARTIFACTORY_USER}"]) {
withCredentials([
string(credentialsId: IC_ARTIFACTORY_KEY, variable: 'ARTIFACTORY_KEY')
]) {
sh '''
bash -x Jenkinsfile-e2e-test.sh
'''
}
}
} finally { }
}
}


If Jenkinsfile-e2e-test.sh falls for some reason then Jenkins will automatically stop and remove the container:



09:40:08 $ docker stop --time=1 6e78cf6d940cb1ca1cb1c729617fd3e6ba3fa4085c2750908dff8f2a1e7ffeed
09:40:09 $ docker rm -f 6e78cf6d940cb1ca1cb1c729617fd3e6ba3fa4085c2750908dff8f2a1e7ffeed


How do I prevent Jenkins to destroy the container on failure?










share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    Consider I have a Jenkins groovy code that runs docker container and some bash will run inside of it:



        docker.withRegistry(dockerRegistry, dockerCredentials) {
    docker.image(new String(dockerImage + ":" + dockerVersion)).inside(mountCommand) {

    try {
    withEnv(["AWS_PROFILE=${mappings['stackEnv']}",
    "FULL_PACKAGE_VERSION=${mappings['revision']}",
    "ARTIFACTORY_USER=${IC_ARTIFACTORY_USER}"]) {
    withCredentials([
    string(credentialsId: IC_ARTIFACTORY_KEY, variable: 'ARTIFACTORY_KEY')
    ]) {
    sh '''
    bash -x Jenkinsfile-e2e-test.sh
    '''
    }
    }
    } finally { }
    }
    }


    If Jenkinsfile-e2e-test.sh falls for some reason then Jenkins will automatically stop and remove the container:



    09:40:08 $ docker stop --time=1 6e78cf6d940cb1ca1cb1c729617fd3e6ba3fa4085c2750908dff8f2a1e7ffeed
    09:40:09 $ docker rm -f 6e78cf6d940cb1ca1cb1c729617fd3e6ba3fa4085c2750908dff8f2a1e7ffeed


    How do I prevent Jenkins to destroy the container on failure?










    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      Consider I have a Jenkins groovy code that runs docker container and some bash will run inside of it:



          docker.withRegistry(dockerRegistry, dockerCredentials) {
      docker.image(new String(dockerImage + ":" + dockerVersion)).inside(mountCommand) {

      try {
      withEnv(["AWS_PROFILE=${mappings['stackEnv']}",
      "FULL_PACKAGE_VERSION=${mappings['revision']}",
      "ARTIFACTORY_USER=${IC_ARTIFACTORY_USER}"]) {
      withCredentials([
      string(credentialsId: IC_ARTIFACTORY_KEY, variable: 'ARTIFACTORY_KEY')
      ]) {
      sh '''
      bash -x Jenkinsfile-e2e-test.sh
      '''
      }
      }
      } finally { }
      }
      }


      If Jenkinsfile-e2e-test.sh falls for some reason then Jenkins will automatically stop and remove the container:



      09:40:08 $ docker stop --time=1 6e78cf6d940cb1ca1cb1c729617fd3e6ba3fa4085c2750908dff8f2a1e7ffeed
      09:40:09 $ docker rm -f 6e78cf6d940cb1ca1cb1c729617fd3e6ba3fa4085c2750908dff8f2a1e7ffeed


      How do I prevent Jenkins to destroy the container on failure?










      share|improve this question















      Consider I have a Jenkins groovy code that runs docker container and some bash will run inside of it:



          docker.withRegistry(dockerRegistry, dockerCredentials) {
      docker.image(new String(dockerImage + ":" + dockerVersion)).inside(mountCommand) {

      try {
      withEnv(["AWS_PROFILE=${mappings['stackEnv']}",
      "FULL_PACKAGE_VERSION=${mappings['revision']}",
      "ARTIFACTORY_USER=${IC_ARTIFACTORY_USER}"]) {
      withCredentials([
      string(credentialsId: IC_ARTIFACTORY_KEY, variable: 'ARTIFACTORY_KEY')
      ]) {
      sh '''
      bash -x Jenkinsfile-e2e-test.sh
      '''
      }
      }
      } finally { }
      }
      }


      If Jenkinsfile-e2e-test.sh falls for some reason then Jenkins will automatically stop and remove the container:



      09:40:08 $ docker stop --time=1 6e78cf6d940cb1ca1cb1c729617fd3e6ba3fa4085c2750908dff8f2a1e7ffeed
      09:40:09 $ docker rm -f 6e78cf6d940cb1ca1cb1c729617fd3e6ba3fa4085c2750908dff8f2a1e7ffeed


      How do I prevent Jenkins to destroy the container on failure?







      linux bash docker jenkins-pipeline jenkins-groovy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked 2 days ago









      Yuri

      7281022




      7281022
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can ensure the command will not fail using this :



          bash -x Jenkinsfile-e2e-test.sh || true


          Or even better :



          bash -x Jenkinsfile-e2e-test.sh > e2e.log 2>&1 & || true
          # check the log file and do something based on the log





          share|improve this answer




























            up vote
            2
            down vote













            Jenkins will always stop and remove the container, once the body of your .inside command is finished. No matter if your bash script succeeds or fails.



            The rational behind is that jenkins takes care of the housekeeping for you, to prevent that you end up with lots of stopped containers hanging around on your build machine.



            This is the case for the Image.withRun and Image.inside methods.
            If you want to have control over the lifecycle of the container you should use Image.run:




            Uses docker run to run the image, and returns a Container which you could stop later




            (from docs)






            share|improve this answer





















            • Thanks, great explanation
              – Yuri
              2 days ago











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203812%2fhow-to-prevent-from-docker-to-stop-remove-the-container-on-error%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You can ensure the command will not fail using this :



            bash -x Jenkinsfile-e2e-test.sh || true


            Or even better :



            bash -x Jenkinsfile-e2e-test.sh > e2e.log 2>&1 & || true
            # check the log file and do something based on the log





            share|improve this answer

























              up vote
              1
              down vote



              accepted










              You can ensure the command will not fail using this :



              bash -x Jenkinsfile-e2e-test.sh || true


              Or even better :



              bash -x Jenkinsfile-e2e-test.sh > e2e.log 2>&1 & || true
              # check the log file and do something based on the log





              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                You can ensure the command will not fail using this :



                bash -x Jenkinsfile-e2e-test.sh || true


                Or even better :



                bash -x Jenkinsfile-e2e-test.sh > e2e.log 2>&1 & || true
                # check the log file and do something based on the log





                share|improve this answer












                You can ensure the command will not fail using this :



                bash -x Jenkinsfile-e2e-test.sh || true


                Or even better :



                bash -x Jenkinsfile-e2e-test.sh > e2e.log 2>&1 & || true
                # check the log file and do something based on the log






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                chenchuk

                1,91521322




                1,91521322
























                    up vote
                    2
                    down vote













                    Jenkins will always stop and remove the container, once the body of your .inside command is finished. No matter if your bash script succeeds or fails.



                    The rational behind is that jenkins takes care of the housekeeping for you, to prevent that you end up with lots of stopped containers hanging around on your build machine.



                    This is the case for the Image.withRun and Image.inside methods.
                    If you want to have control over the lifecycle of the container you should use Image.run:




                    Uses docker run to run the image, and returns a Container which you could stop later




                    (from docs)






                    share|improve this answer





















                    • Thanks, great explanation
                      – Yuri
                      2 days ago















                    up vote
                    2
                    down vote













                    Jenkins will always stop and remove the container, once the body of your .inside command is finished. No matter if your bash script succeeds or fails.



                    The rational behind is that jenkins takes care of the housekeeping for you, to prevent that you end up with lots of stopped containers hanging around on your build machine.



                    This is the case for the Image.withRun and Image.inside methods.
                    If you want to have control over the lifecycle of the container you should use Image.run:




                    Uses docker run to run the image, and returns a Container which you could stop later




                    (from docs)






                    share|improve this answer





















                    • Thanks, great explanation
                      – Yuri
                      2 days ago













                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Jenkins will always stop and remove the container, once the body of your .inside command is finished. No matter if your bash script succeeds or fails.



                    The rational behind is that jenkins takes care of the housekeeping for you, to prevent that you end up with lots of stopped containers hanging around on your build machine.



                    This is the case for the Image.withRun and Image.inside methods.
                    If you want to have control over the lifecycle of the container you should use Image.run:




                    Uses docker run to run the image, and returns a Container which you could stop later




                    (from docs)






                    share|improve this answer












                    Jenkins will always stop and remove the container, once the body of your .inside command is finished. No matter if your bash script succeeds or fails.



                    The rational behind is that jenkins takes care of the housekeeping for you, to prevent that you end up with lots of stopped containers hanging around on your build machine.



                    This is the case for the Image.withRun and Image.inside methods.
                    If you want to have control over the lifecycle of the container you should use Image.run:




                    Uses docker run to run the image, and returns a Container which you could stop later




                    (from docs)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 2 days ago









                    fab

                    7231924




                    7231924












                    • Thanks, great explanation
                      – Yuri
                      2 days ago


















                    • Thanks, great explanation
                      – Yuri
                      2 days ago
















                    Thanks, great explanation
                    – Yuri
                    2 days ago




                    Thanks, great explanation
                    – Yuri
                    2 days ago


















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203812%2fhow-to-prevent-from-docker-to-stop-remove-the-container-on-error%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Schultheiß

                    Liste der Kulturdenkmale in Wilsdruff

                    Android Play Services Check