1

I have a docker container running Jenkins on a server. I'm trying to get code from GitHub and copy the contents to the target directory from the Jenkins pipeline.

My repository is cloned here: /var/jenkins_home/workspace/my-repo

My target directory is: /home/deploy/project

This is my cp command: cp -r . /home/deploy/peatio

This is the error statement: cp: cannot create directory '/home/deploy/peatio': No such file or directory

UPDATE: I installed Jenkins without Docker and my use case worked fine. It was due to running Jenkins in Docker container. So, the question is, how can we copy something from Docker container to local host by running copy command inside the Docker container? Is this even possible? I'm aware of docker cp and it is used outside Docker container.

Abdullah Khawer
  • 4,461
  • 4
  • 29
  • 66

3 Answers3

0

Just in case an intermediate directory is missing, create it/them:

mkdir -p /home/deploy/peatio
cp -r . /home/deploy/peatio
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I need to run these commands from my Jenkins pipeline where Jenkins is running in Docker and the target is on host (local directory). You commands didn't work. – Abdullah Khawer Sep 07 '19 at 22:43
  • I don't see any error message now because the location is different. Jenkins pipeline has created /home/deploy/peatio somewhere else and it was already existing at /home/deploy/peatio. Could be because of Jenkins running in Docker container? – Abdullah Khawer Sep 07 '19 at 22:50
  • @abdullahkhawer That is possible: Docker container might have mounted the local path to a container path (https://docs.docker.com/storage/bind-mounts/): you might need to use that mounted path. – VonC Sep 07 '19 at 22:55
  • UPDATE: I installed Jenkins without Docker and my use case worked fine. It was due to running Jenkins in Docker container. So, the question is, how can we copy something from Docker container to local host by running copy command inside the Docker container? Is this even possible? I'm aware of docker cp and it is used outside Docker container. – Abdullah Khawer Sep 07 '19 at 23:23
  • 1
    @abdullahkhawer That is where the bind mount I mentioned previously comes in: https://docs.docker.com/storage/bind-mounts/: then workspace is in `-v jenkins_home:/var/jenkins_home`, as described in https://hub.docker.com/r/jenkins/jenkins/ – VonC Sep 07 '19 at 23:25
0

No, It is not possible to copy from inside container to host location that is not bind mount or other words shared between host and container using -v flag or -mount.

enter image description here

you can just have a workaround for that.

Bind two directory of the host with the container.

  • Jenkins home
  • Another directory for artifacts or for copy purpose

Your Jenkins related configuration will save to jenkins_host_path:/var/jenkins_home

Mount empty directory of the host something path_to_host_cp/:/jenkins_container_path_for_cp, so when you copy some thing like in your case a git repo, copy this to /jenkins_container_path_for_cp and it will available to host and if you to update new files on host location just run rm inside container and then cp -r . /home/deploy/peatio or cp -r . jenkins_container_path_for_cp.

You can also the entry point to copy files and folders from one location to other on container boot to host empty directory as if there is anything the host the directory it will hide the container content.

Adiii
  • 54,482
  • 7
  • 145
  • 148
0

like this


$ export JENKINS_HOME=/srv/jenkins

$ export TARGET_DIR=/home/deploy/peatio

$ docker run ***(ignore other important parameters) -v $JENKINS_HOME:/var/jenkins_home -v $TARGET_DIR:/home/deploy/peatio

Important parameters: -v $TARGET_DIR:/home/deploy/peatio

  • 2.continue to execute the commands you want inside the jenkins container
$ docker exec -it jenkins bash
$ cp -r . /home/deploy/peatio
xq nie
  • 41
  • 5