Running docker container in Jenkins container, How can I set the volume from host?

I’m running a container with jenkins using “docker outside of docker”. My docker compose is:

---
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.

  • accessing a docker container's file system through terminal
  • openshift v3 sed couldn't open temporary file
  • Connection refused when trying to run Kong API Gateway using a docker container
  • Nginx with docker not working for web application
  • Docker build: returned a non-zero code: 1
  • How to configure dns entries for Docker Compose
  • 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.

  • Does docker-compose up destroy old database?
  • Install vim in node Docker image
  • Why my docker container apache service will be shut down everytime I attach my container and then exit?
  • How do I use the public swagger-generator docker image to generate a client?
  • docker-machine specify daemon port
  • etcd api on CoreOS - setting ip address configurations remotely
  • 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:

    1. 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.

    2. 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_home and then update your compose file to have the same path (you may need to specify /var/jenkins_home/. to follow the symlink).

    3. 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.

    1. 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:

    volumes:
    - ${CUR_DIR:-.}:/app

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.