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.

  • Copy folder with wildcard from docker container to host
  • Running magellan on pyspark-notebook docker container
  • Share dynamic content across docker containers nodes in Jelastic
  • How to run mongorestore after mongod in docker
  • Installation latest kubernetes version (1.1.1) on Mesos 0.25
  • Auto-restarting Docker container on Google Compute Engine
  • 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.

  • Socket connection into Docker initially succeeds then fails
  • How to run Docker container and watch the logs in one single command
  • docker run container, how to rerun
  • docker registry v2 : get parent image name
  • Running copies of the same multi-container app with Docker Compose
  • RunContainer: Error response from daemon: Duplicate mount point (Kubernetes)
  • 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.