Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

docker - Jenkins: How to keep container running and copy artifacts out of container to host machine

Here is what I am doing and hope to achieve

  1. I Run docker container.
  2. Inside container I run the tests.
  3. And finally I would like to copy the test artifacts out of the container before closing the container.

At the moment I am using it like this and first 2 steps work fine

 stage('Pulling Docker Image'){
            steps{
                echo "Pulling Docker Image"
                script{
                  docker.withRegistry("url", "credentials"){
                      docker.image(<image>).inside(<arguments>->c{
                       //run tests inside container
                      }
                  }
                }
         }
 }
 post{
   always{
     docker cp <container>:/test_result:/test_result_store/
     docker stop <container>
     docker rm <container>
   }

But with this approach the container is closing and getting removed, before post step is invoked.

I looked at documentation where it mentions run and withRun option which returns container handle. So I modified the code like this

 stage('Pulling Docker Image'){
            steps{
                echo "Pulling Docker Image & Running Tests"
                script{
                  docker.withRegistry("url", "credentials"){
                      def container_handle = docker.image(<image>).run(<arguments>->c{
                       //run tests inside container
                      }
                  }
                }
         }
 }
 post{
   always{
     docker cp container_handle:/test_result:/test_result_store/
     docker stop container_handle
     docker rm container_handle
   }

but here container_handle seems to be invalid reference inside post step, also test is not run inside the container.

I would appreciate if one can point out to obvious errors I am doing here.

Also I am planning to save all this to a pipeline script so let me know, if these steps will work as is or some change might be needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you don't do anything at all, Jenkins will bind-mount the workspace directory into the container at the same path. The docker.inside() helper will take care of creating, starting, stopping, and destroying the container for you.

stage('Running tests in container') {
  docker.withRegistry("url", "credentials") {
    docker.image("image").inside {
      sh "./run_tests.sh"
      // Assume this drops JUnit test results into the current directory
    }
  }
  // outside the container
  junit(testResults: 'test-results.xml')
}

You do not need to specify a -v bind-mount option; Jenkins provides this for you. You do not need to manually docker cp because Docker has the bind-mount of the workspace directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...