Docker Community Forums

Share and learn in the Docker community.

How to mount local dir to sibling container


(Hegyhati) #1

I have a Debian server running Jenkins inside a docker container. In my pipeline I have a stage executed in a container:

agent {
        docker {
          image 'TESTER'
          args '-v /var/run/docker.sock:/var/run/docker.sock'
        }        
      }

In an sh step I build an EXECUTOR container, that puts the binaries built in a previous stage to their place, sets some variables, etc. Then I run the container, and I want to mount a log directory, where my binary puts some log messages into error.txt. (There is a CMD binary 2> /log/error.txt in the Dockerfile.)

mkdir log

docker build ${PWD} -f pathtodockerfile/Dockerfile -t EXECUTOR

docker run -d -rm  -v "${PWD}/log":/log:rw  EXECUTOR

cat ${PWD}/log/error.txt

After this I get a message that the error.txt file does not exist.

Logging onto the server I’ve found the file under something like:
/var/jenkins_home/workspace/branchname-hash/log/

However, the directory, where the TESTER container works is:
/var/lib/docker/volumes/jenkins-data/_data/workspace/branchname-hash/

If I understand correctly, since I’ve passed the docker socket when running the EXECUTOR, PWD is evaluated to the workdir of Jenkins on the Debian server. I’m still uncertain, how it gives back the same path for cat, but I can understand, that the container can not access that path, as it doesn’t exist in its filesystem.

My problem: I need to pass the docker socket to avoid DinD, but then I can not mount a local directory of the running TESTER container.

I could imagine a workaround, where I mount the same global (on the Debian filesystem) directory to both the TESTER and the EXECUTOR container, but I assume, that there is a less cumbersome way to achieve what I want, just I couldn’t find it in the last couple of hours…

If anybody could direct me to a tutorial, or whatever, I’d be really thankful.