Docker in docker: sharing volume does not work

The files aren’t shared.

---
kind: pipeline
type: docker
name: Tests

platform:
  arch: amd64
  os: linux

steps:
  - name: tests
    image: docker:dind
    volumes:
      - name: docker_sock
        path: /var/run/docker.sock
      - name: cache
        path: /cache
    commands:
      - pwd
      - touch /cache/hello-world.txt
      - ls -lia /cache/*.txt
      - echo "=== begin of inner container ==="
      - docker run -v /cache:/cache alpine sh -c "ls -lia /cache/*.txt"
      - docker run -v /cache:/cache alpine sh -c "touch /cache/bye-world.txt"
      - docker run -v /cache:/cache alpine sh -c "ls -lia /cache/*.txt"
      - echo "=== end of inner container ==="
      - ls -lia /cache/*.txt
    depends_on:
      - clone

volumes:
  - name: docker_sock
    host:
      path: /var/run/docker.sock
  - name: cache
    host:
      path: /opt/xablau1

Output:

 + pwd
/drone/src
+ touch /cache/hello-world.txt
+ ls -lia /cache/*.txt
 580364 -rw-r--r--    1 root     root             0 Sep 28 18:12 /cache/hello-world.txt
+ echo "=== begin of inner container ==="
=== begin of inner container ===
+ docker run -v /cache:/cache alpine sh -c "ls -lia /cache/*.txt"
2816006 -rw-r--r--    1 root     root             0 Sep 28 18:12 /cache/bye-world.txt
+ docker run -v /cache:/cache alpine sh -c "touch /cache/bye-world.txt"
+ docker run -v /cache:/cache alpine sh -c "ls -lia /cache/*.txt"
2816006 -rw-r--r--    1 root     root             0 Sep 28 18:12 /cache/bye-world.txt
+ echo "=== end of inner container ==="
=== end of inner container ===
+ ls -lia /cache/*.txt
 580364 -rw-r--r--    1 root     root             0 Sep 28 18:12 /cache/hello-world.txt

Does anybody have an idea?

Because you are mounting the host machine docker socket, docker is mounting /cache on the host machine, as opposed to inside the pipeline container. You should adjust your configuration accordingly:

- docker run -v /cache:/cache       alpine sh -c "ls -lia /cache/*.txt"
+ docker run -v /opt/xablau1:/cache alpine sh -c "touch /cache/bye-world.txt"
1 Like

Thank you!

Is there a way to mount a temporary volume?

yes, there is an example in the docs, however, please note that mounting a temporary volume would not solve this issue since the volume in --volume is always going to be mounted on the host when you mount the host machine docker socket.

In case of temporary volume, I don’t have the path in the host to use on docker run -v on steps.

volumes:
- name: cache
  temp: {}

Is there a way to get that path?