1

I have configured Jenkins in Docker container. I am able to take a build. After a build I want to move WAR file into my Tomcat server which is running in host system. I have added copy command in post build task. Jenkins is not able to move the WAR to host system , since it is running in container.

How to move WAR file from container to Host system ?

Host path : /home/test/tomcat/webapps

Jenkins container path: /var/jenkins/workspace/dev/welcome/target/welcome.war

Gnana
  • 2,130
  • 5
  • 26
  • 57
  • Are you running Jenkins in docker, or are you running a build job in docker? In case of the first, map any parent directory of where you want to put the file on your host system into the container and copy it there (be careful with the user inside your container), so i.e `-v /home/test/tomcat/webapps/:/home/test/tomcat/webapps/` and then you can copy it as post build-step and it will persist (basically like the answer from @kimy82). In case of the latter, your workspace will be mapped by Jenkins, so any files created during build will persist. – Rik Dec 01 '16 at 22:28
  • If you build something in a container, the `userid:groupid` will be the ones of the jenkins service user inside the container. So you must be able to alter this on the host system, or make sure they are the same on host and container (i.e if you user test has `uid:gid` `1001:1001` make sure your the user inside the container that creates the `welcome.jar` has the same `uid:gid` to prevent `Permission denied` on host – Rik Dec 01 '16 at 22:33
  • I am running Jenkins in Docker and taking build through that one. After build WAR is inside Jenkins container. Now I want to move the WAR to Host system. Which command I have to give in post build task script section. please help me on this. – Gnana Dec 01 '16 at 22:38
  • 1
    when you spin up your docker instance with jenkins pass the option `-v /home/test/:/home/test`. This maps the home dir of test user to the exact same location inside the container (if user test exist in container choose different location, i.e `-v /home/test/tmp/:/tmp/`. Directory on host must exist, in container will be created automatically. Then in your post build step copy file there: `cp /var/jenkins/workspace/dev/welcome/target/welcome.war /home/test/tomcat/webapps`. The file should now be on your host at `/home/test/tomcat/webapps/welcome.jar`. – Rik Dec 01 '16 at 22:45
  • If you map a different directory (volume) you have to move it on the host system as well ofcourse – Rik Dec 01 '16 at 22:46
  • [Docker - copy file from container to host](https://stackoverflow.com/q/22049212/6521116) – LF00 Nov 17 '17 at 06:14

1 Answers1

0

I would create a volumne when starting jenkins with docker and then copy the war file there with a normal shell jenkins command. I usually do docker run -d -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 -p 50000:50000 -v /home/docker:/var/jenkins_home --net="host" --env JAVA_OPS="-Xms1024m -Xmx1024m" --privileged=true axltxl/jenkins-dood

The -v option is to create a volumne, which is a shared folder for container and host. In my case I use that for having the jenkins configuration outside docker container.

kimy82
  • 4,069
  • 1
  • 22
  • 25
  • I have also done volume between container and host -v /home/test:/var/jenkins_home. how can I move the WAR to webapps folder after taking build. could you please give me solution for this one ? – Gnana Dec 01 '16 at 21:31
  • You can create another volume **-v {path to webapps}/welcome.war:/var/jenkins/workspace/dev/welcome/target/welcome.war** . That will hopefuly work. Not sure if it is possible to mount a volume of a file – kimy82 Dec 01 '16 at 21:39
  • I tried the same. But getting error while starting the container. – Gnana Dec 01 '16 at 21:48
  • ERROR: for jenkins Cannot start service jenkins: Cannot start container e9aa05b7724a8c2a001a225b75f9162b32a282c5abd3bd017ba0c40a76782d46: [8] System error: not a directory ERROR: Encountered errors while bringing up the project. – Gnana Dec 01 '16 at 21:54
  • @kimy82 you can map files, but only from host->client (file must exist on host) – Rik Dec 01 '16 at 22:36
  • It works for in my case, even if the directory doesn't exist in the container. I just have to make sure that the war is in the host ... if not it creates a directory. Do you have the last docker version? – kimy82 Dec 01 '16 at 22:37
  • If you have your DiD you can run in your Jenkins after war build **docker cp [containerid]:/var/jenkins/workspace/dev/welcome/target/welcome.war /home/test/tomcat/webapps/welcome.war** and it will work. – kimy82 Dec 01 '16 at 22:59
  • Moreover if you start your docker with a name ex. **--name=jenkins** you can use the name instead of the containerId and there is no need to change jenkins shell command – kimy82 Dec 01 '16 at 23:03