jenkinsci / docker

Docker official jenkins repo
https://hub.docker.com/r/jenkins/jenkins
MIT License
6.73k stars 4.56k forks source link

'docker cp' command in Jenkins docker container copies a file into a directory #441

Closed DrWeiZhou closed 7 years ago

DrWeiZhou commented 7 years ago

Since there is no problem when I run the script in real Jenkins and in local host machine, I think it is a problem related to the Jenkins Image.

I am running a Jenkins server in a docker container.

I started the Jenkins container by the following command:

sudo docker run --name jenkins -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker -d weizhou/jenkins

In a Jenkins job, I want to copy the builded .war to the local machine (i.e. out of the docker container) and to launch another Tomcat docker server. However, the file in the docker file become a directory after I use the command docker cp, that is, the rpm.war file become a rpm.war directory and nothing in it. How should I solve this problem, thanks very much.

Below are my script post in the Execute shell in a jenkins job.

sudo docker exec tomcat  /usr/local/tomcat/bin/catalina.sh stop
sudo docker rm -f tomcat
sudo rm /home/rpm.war
sudo docker cp jenkins:/var/jenkins_home/workspace/rpm/target/rpm.war /home/
sudo chmod 777 /home/rpm.war
sudo docker run --name tomcat --link mysql:mysql  -p 80:8080 -v /home/rpm.war:/usr/local/tomcat/webapps/rpm.war --rm -d tomcat:7.0.75-jre7

Here are the Jenkins console:

[rpm] $ /bin/sh -xe /tmp/hudson1380760968175739587.sh
+ sudo docker exec tomcat /usr/local/tomcat/bin/catalina.sh stop
+ sudo docker rm -f tomcat
tomcat
+ sudo rm /home/rpm.war
+ sudo docker cp jenkins:/var/jenkins_home/workspace/rpm/target/rpm.war /home/
+ sudo chmod 777 /home/rpm.war
+ sudo docker run --name tomcat --link mysql:mysql -p 80:8080 -v /home/rpm.war:/usr/local/tomcat/webapps/rpm.war --rm -d tomcat:7.0.75-jre7
205b8551375c1e3667965288d8d3d4240f8be765ea124e1ff57956e801509318
Finished: SUCCESS

And the rpm.war is a directory and nothing in it!

ubuntu@ip-172-31-66-12:/home$ ls -l
total 12
-rwxrwxrwx 1 root   root    368 Feb 24 09:23 rpmBuild.sh
drwxr-xr-x 2 root   root   4096 Feb 24 09:38 rpm.war
drwxr-xr-x 6 ubuntu ubuntu 4096 Feb 24 09:25 ubuntu

Besides, the script can run ok in the local machine and command `docker cp' really copy file as file, rather than a directory.

Furthermore, I use the similar script runing in a real Jenkins rather than in a container, it does work and the file is copied as a file. The similar script is

sudo docker exec tomcat  /usr/local/tomcat/bin/catalina.sh stop
sudo docker rm -f tomcat
sudo rm /home/rpm.war
sudo cp /var/lib/jenkins/workspace/rpm/target/rpm.war /home/
sudo chmod 777 /home/rpm.war
sudo docker run --name tomcat --link mysql:mysql  -p 80:8080 -v /home/rpm.war:/usr/local/tomcat/webapps/rpm.war --rm -d tomcat:7.0.75-jre7

At last, my jenkins Dockerfile:

FROM jenkins

USER root
RUN apt-get update \
  && apt-get install -y sudo maven\
  && rm -rf /var/lib/apt/lists/*
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers

USER jenkins
RUN /usr/local/bin/install-plugins.sh bouncycastle-api:2.16.0 cloudbees-folder:5.17 structs:1.6 junit:1.20 antisamy-markup-formatter:1.5 pam-auth:1.3 windows-slaves:1.2 display-url-api:1.1.1 mailer:1.19 ldap:1.14 icon-shim:2.0.3 matrix-auth:1.4 token-macro:2.0 script-security:1.26 matrix-project:1.8 external-monitor-job:1.7 build-timeout:1.18 credentials:2.1.11 plain-credentials:1.4 workflow-step-api:2.9 credentials-binding:1.10 timestamper:1.8.8 resource-disposer:0.6 scm-api:2.0.7 workflow-api:2.11 workflow-basic-steps:2.4 ws-cleanup:0.32 ant:1.4 gradle:1.26 workflow-scm-step:2.3 ssh-credentials:1.13 git-client:2.2.1 jquery-detached:1.2.1 workflow-support:2.13 ace-editor:1.1 workflow-cps:2.27 git-server:1.7 workflow-cps-global-lib:2.6 pipeline-milestone-step:1.3 pipeline-build-step:2.4 pipeline-stage-step:2.2 pipeline-input-step:2.5 workflow-job:2.10 pipeline-graph-analysis:1.3 pipeline-rest-api:2.5 handlebars:1.1.1 momentjs:1.1.1 pipeline-stage-view:2.5 durable-task:1.13 workflow-durable-task-step:2.9 pipeline-model-api:1.0.2 pipeline-stage-tags-metadata:1.0.2 authentication-tokens:1.3 docker-commons:1.6 docker-workflow:1.10 pipeline-model-declarative-agent:1.0.2 branch-api:2.0.6 workflow-multibranch:2.12 pipeline-model-definition:1.0.2 workflow-aggregator:2.5 github-api:1.84 git:3.0.5 github:1.26.0 github-branch-source:2.0.3 pipeline-github-lib:1.0 github-organization-folder:1.6 mapdb-api:1.0.9.0 subversion:2.7.1 ssh-slaves:1.13 email-ext:2.57 build-token-root:1.4
carlossg commented 7 years ago

You better ask in the docker mailing list. You are trying to copy a file from one container to another using docker cp and a local file reference , no idea if docker supports it

DrWeiZhou commented 7 years ago

Thanks to reply. In the script, I just copy the file from the Jenkins container to the local machine. It does work but copy as a directory. I did not copy a file from a container to another one. Besides, I run a similar script in the local Jenkins rather than in a Jenkins container, it does work. Therefore, the problem should be related to the Jenkins container.

carlossg commented 7 years ago

You are running jenkins in a container, so when you run

sudo docker cp jenkins:/var/jenkins_home/workspace/rpm/target/rpm.war /home/

it is copying the war into the container under /home/rpm.war

Then you run a container mounting /home/rpm.war as a volume

sudo docker run --name tomcat --link mysql:mysql -p 80:8080 -v /home/rpm.war:/usr/local/tomcat/webapps/rpm.war --rm -d tomcat:7.0.75-jre7

but docker daemon runs in the host, and in the host there is no /home/rpm.war

DrWeiZhou commented 7 years ago

Thanks very much for your reply and you are right.

I found the rpm.war in the /home of the container. Is there any way I can copy the rpm.war to the local machine by a command run in the container? Thanks for your help.

DrWeiZhou commented 7 years ago

I solved this problem by mounting a volume to a container's directory and copying the rpm.war to the mounted directory. Thanks for the help:)

ymakkapati commented 6 years ago

@DrWeiZhou , looks like you have installed docker in docker container. How did you do that? I am getting a ulimit error when I try to run docker command in a docker container