Is it possible to to define a volume usable both in k8s and docker?

1/8/2019

If I want to run a container B within a container A (reusing the Docker daemon) I can just bind mount /var/run/docker.sock and /usr/bin/docker and can happily call docker run within A. Now I would like to share a k8s volume between A and B. For that I thought of creating an emptyDir volume in A and pass it to B using docker run -v. But this does not work as the emptyDir volume does not seem to be a Docker volume (it does not appear when running docker volume ls).

The snippet below prints a list of volume where cache-volume does not appear:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: alpine
    name: test-container
    command: [ash]
    args: ["-c", "docker volume ls"]
    volumeMounts:
    - name: dockersock
      mountPath: "/var/run/docker.sock"
    - name: dockerlib
      mountPath: "/usr/bin/docker"
    - name: cache-volume
      mountPath: /cache
  volumes:
  - name: dockersock
    hostPath:
      path: /var/run/docker.sock
  - name: dockerlib
    hostPath:
      path: /usr/bin/docker
  - name: cache-volume
    emptyDir: {}

So the question is: is there any way to define a volume usable both in k8s and docker?

-- stackoverflowed
docker
docker-volume
kubernetes

1 Answer

1/8/2019

For the record I found a workaround but if you have a better suggestion please don't hesitate to share it.

A solution to the problem above is to mount a hostPath directory D and bind mount this directory into the container B.

apiVersion: v1 kind: Pod metadata: name: test-pd3 spec: containers: - image: alpine name: test-container command: [ash] args: ["-c", "echo blabla > /home/test.txt ; docker run -v /home:/home --entrypoint ls alpine /home"] volumeMounts: - name: dockersock mountPath: "/var/run/docker.sock" - name: dockerlib mountPath: "/usr/bin/docker" - name: home mountPath: "/home" volumes: - name: dockersock hostPath: path: /var/run/docker.sock - name: dockerlib hostPath: path: /usr/bin/docker - name: home hostPath: path: /home

The result of the above snippet is that B now has a /home/test.txt, which is what we wanted.

-- stackoverflowed
Source: StackOverflow