Running docker container in Jenkins container, How can I set the volume from host?
--- version: '2' services: jenkins-master: build: context: . volumes: - /var/run/docker.sock:/var/run/docker.sock - /dev/urandom:/dev/random - /home/jj/jenkins/jenkins_home/:/var/jenkins_home ports: - "8080:8080"
So all containers launched from “jenkins container” are running in host machine.
But when I try to run docker-compose in “jenkins container” in a job thats needs a volume, it takes the path from host instead of jenkins. I mean, when I run docker-compose with
volumes: - .:/app
It is mounted in /var/jenkins_home/workspace/JOB_NAME in the host but I want that it is mounted in /home/jj/jenkins/jenkins_home/workspace/JOB_NAME
Any idea for doing this with a “clean” mode?
P.D.: I did a workaround using environments variables.
One Solution collect form web for “Running docker container in Jenkins container, How can I set the volume from host?”
Docker on the host will map the path as is from the request, and docker-compose will make the request with the path it sees inside the container. This leaves you with a few options:
Don’t use host volumes in your builds. If you need volumes, you can use named volumes and use docker io to read in and out of those volumes. That would look like:
tar -cC data . | docker run -i --rm -v app-data:/target busybox /bin/sh -c "tar -xC /target". You’d reverse the docker/tar commands to pull data back out.
Make the path on the host match that of the container. On your host, if you have access to make a symlink in var, you can
ln -s /home/jj/jenkins/jenkins_home /var/jenkins_homeand then update your compose file to have the same path (you may need to specify /var/jenkins_home/. to follow the symlink).
Make the path of the container match that of the host. This may be the easiest option, but I’m not positive it would work (depends on where compose thinks it’s running). Your Dockerfile for the jenkins master can include the following:
RUN mkdir -p /home/jj/jenkins \
&& ln -s /var/jenkins_home /home/jj/jenkins/jenkins_home
ENV JENKINS_HOME /home/jj/jenkins/jenkins_home
If the easy option doesn’t work, you can rebuild the image from jenkins and change the JENKINS_HOME variable to match your environment.
- Make your compose paths absolute. You can add some code to set a variable:
export CUR_DIR=$(pwd | sed 's#/var/jenkins_home#/home/jj/jenkins/jenkins_home#'). Then you can set your volume with that variable: