简体   繁体   中英

how to copy file from docker container over to Jenkins

I am new to building and running dockers. So I have a jenkins pipeline setup where I have a stage which runs a python file. I have created a docker to run this python file.Using commands

    sh "docker build -f create-abc-Dockerfile -t create-abc ."
    sh "docker run create-abc python create-abc.py $name $abc ${abcDescription}"
    getDetails = sh(returnStdout: true, script: "cat abc_details").trim()

where create-abc-Dockerfile is my docker file and create-abc.py is my python file. create-abc-Dockerfile contains

     FROM python:3.8-alpine3.10
     COPY create-abc.py .
     RUN pip install requests
     CMD ["python", "create-abc.py"]

My python file performs some action - calls an API and some parts of the response in a file "xyz_details"

The above piece of code in Jenkins pipeline runs correctly calling my python file executing the API, but it cannot access xyz_details file. It says file not found.

I read a couple of posts and tried a few methods

  1. Instead of running the build, I changed CMD to RUN in docker - but my python file is paramterized, and it passes dynamic values. So this method does not work for me
  2. Tried this method
    docker cp <containerId>:/file/path/within/container /host/path/target

My code :

    sh "docker cp create-abc:/xyz_details ."

where create-rfc is my container (I am using the tag name provided above when building the docker) But I am getting the following error

    + docker cp create-abc:/xyz_details .
     Error: No such container:path: create-abc:/xyz_details

All I want to do is, run the python file, write the result to xyz_details, read this xyz_details file again in Jenkins pipeline.

You can try to use volumes. You basically need to run the docker container and attache a volume for the destination path (where the files will be generated). and then you will have access to the files from the host machine.

sh "docker run -v $PWD/myfiles:/dir/path/within/container create-abc python create-abc.py $name $abc ${abcDescription}"
sh "cp -r $PWD/myfiles /to/path"

This is how i copy file from docker container to local in jenkins through pipeline

  sh """
          docker cp  container_name:/app/test-reports ./test-reports
     """

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM