3

We have previously been running Jenkins in Docker in Docker (DIND) mode, i.e. running a docker daemon inside the Jenkins docker container. But due to many problems (some of which are described in the link above) we've decided to move away from this approach and instead let the container use the host daemon by simply mounting it as volume when starting the container:

-v /var/run/docker.sock:/var/run/docker.sock

But now we run into problems when mounting relative paths with Docker Compose that is started inside the container which worked fine in DIND mode. Consider this docker-compose file:

myimage:
  build: .
  environment:
     LANG: C.UTF-8     
  working_dir: /code
  volumes:
    - ../../../:/code
    - ~/.m2/repository:/root/.m2/repository
    - ~/.gradle:/root/.gradle

Previously this mounted all folders, for example the ../../../ folder, from the container but now it seems to try to mount them from the host. When I check the directory structure on the host it seems like docker-compose have replicated the directory structure from the container and then tries to mount this folder which makes it empty.

So my question is, how can one mount relative paths in Docker Compose when using the docker daemon from the host?

Johan
  • 37,479
  • 32
  • 149
  • 237
  • Don't understand why this was downvoted, I'd love to know so that I can improve my questions in the future. – Johan Dec 18 '17 at 19:32

2 Answers2

0

You'll need to make sure the relative path on your host is the same inside your Jenkins container for this to work.

It's not really a relative path, docker-compose is doing the best it can to convert a relative path into an absolute path which the docker host requires. All paths are evaluated on the docker host to create a new container, it doesn't know you are running the docker client remotely or inside of a container, and it doesn't know what directory you are currently in.

As another option, you may want to consider switching to named volumes and map the same named volume in your Jenkins container as in your other containers.

BMitch
  • 231,797
  • 42
  • 475
  • 450
0

Docker has a client server architecture, when you mount the docker socket which is on the host, you are simply communication with the hosts docker deamonm. Thus all host volume path will be interpreted as path on the host.

To solve that you need to bind the jenkins containers directories onto the host and then use the host folders as mount points. Thus simply start jenkins container with

-v ./code:<path-to-../../..> -v ./m2-repo:.../.m2/repository

Then change the compose file to use the host folders:

myimage:
  build: .
  environment:
     LANG: C.UTF-8     
  working_dir: /code
  volumes:
    - ./code:/code
    - ./m2-repo:/root/.m2/repository
    ...
yamenk
  • 46,736
  • 10
  • 93
  • 87
  • Thanks, but a problem with this is that the the `` is different for each Jenkins project. I would like to avoid having to change the volume mounts each time a new project is added (note that I haven't down-voted your answer). – Johan Dec 18 '17 at 16:06