desktop-linux icon indicating copy to clipboard operation
desktop-linux copied to clipboard

Cannot see host's files when mounting hostPath on Kubernetes cluster

Open mthaak opened this issue 2 years ago • 10 comments

  • [x] I have tried with the latest version of Docker Desktop
  • [ ] I have tried disabling enabled experimental features
  • [ ] I have uploaded Diagnostics
  • Diagnostics ID:

Expected behavior

I'm running a Kubernetes cluster in Docker Desktop Linux on Ubuntu (using Skaffold). I have defined a pod that mounts the host's filesystem like this:

...
      containers:
        - name: mycontainer
          ... 
          volumeMounts:
            - name: temp
              mountPath: "/home/temp"
      volumes:
        - name: temp
          hostPath:
            path: /home/temp
...

Strangely, this shows as two containers: image

I can confirm that the spawned container k8s_mycontainer_mycontainer has this mount: image

Also file sharing is enabled for /home/temp in the settings: image

However, running ls /home/temp in the container returns no files, while on the host it does contain a file. The opposite way, after creating a file in the container, I cannot see it on the host either.

Actual behavior

I should be able to see the file in my host's mounted /home/temp directory.

Information

Listing the host's files does work when I create a container directly (i.e. without Kubernetes):

❯ docker run -v /home/temp:/home/temp busybox ls /home/temp
myfile.txt

So I have two containers with the same mount. One container spawned by Kubernetes and another directly through docker run. And only the one spawned by Kubernetes cannot see the host's file.

I have docker inspected both containers to look for differences but that didn't make me much wiser because I don't know what to look for. But I can share if useful?

I didn't have this problem with kind so it looks like a Docker Desktop Linux specific issue. Also, my colleagues using Docker Desktop for Mac don't have this issue.

  • Linux distro: Ubuntu 22.04.01 LTS
  • Docker Desktop Version: 4.16.2 (95914)

Steps to reproduce the behavior

(See Expected behavior)

Basically, run a Kubernetes pod with hostPath mount and list the host's files within the pod:

kubectl run mycontainer \
  --image=busybox \
  --restart=Never \
  --overrides='
  {
    "kind": "Pod",
    "apiVersion": "v1",
    "metadata": {
    },
    "spec": {
      "volumes": [
        {
          "name": "temp",
          "hostPath": {
            "path": "/home/temp"
          }
        }
      ],
      "containers": [
        {
          "name": "mycontainer",
          "image": "busybox",
          "command": [
            "ls",
            "/home/temp"
          ],
          "volumeMounts": [
            {
              "name": "temp",
              "mountPath": "/home/temp"
            }
          ],
          "imagePullPolicy": "IfNotPresent"
        }
      ],
      "restartPolicy": "Never"
    }
  }
  ' \
    --quiet -i --rm

mthaak avatar Feb 17 '23 10:02 mthaak

#90 might be related

mthaak avatar Feb 17 '23 10:02 mthaak

I got same problem today, looking for solution.

crazyfree avatar May 17 '23 09:05 crazyfree

I got the same issue

jveling-yoti avatar May 18 '23 15:05 jveling-yoti

I got the same issue

dimatymko avatar May 23 '23 09:05 dimatymko

same problem, any clue?

oleghind avatar Jun 10 '23 00:06 oleghind

I had the same issue and discovered that if I add /host_mnt at the beginning of the host path (e.g. /host_mnt/home/tmp instead of /home/tmp) then it works.

TomerGodinger avatar Aug 06 '23 07:08 TomerGodinger

I confirm. The solution @TomerGodinger helped me too. Thank you. Why is that? Is it possible that this will change in the future?

danijar2000 avatar Aug 24 '23 06:08 danijar2000

This solves the problem for me too. Thank you @TomerGodinger!

It means that when running our Kubernetes cluster on Linux, all host paths must be prefixed with /host_mnt. This can simply be done by prefixing all host paths with an environment variable $HOST_MNT that only is set to /host_mnt on Linux systems

I searched for this information in the Docker Desktop for Linux docs just now, but could not find it. It would be very beneficial if it were added

Or perhaps this behaviour can be made the default in Docker Desktop on Linux?

mthaak avatar Aug 29 '23 08:08 mthaak

@mthaak The best solution would be for Docker Desktop to have the same behavior on all operating systems - i.e. this /host_mnt requirement prefix "requirement" should be removed. Until then, though, at least it works this way. Kind of an annoyance since I need the same K8s YAMLs to work on different systems, so like you said, I need to add /host_mnt just for Linux systems. Unfortunately, K8s YAMLs don't support environment variables, so you need to run them through envsubst or something similar.

TomerGodinger avatar Aug 29 '23 12:08 TomerGodinger

This workaround doesn't work for me on MacOS

tdiesler avatar Mar 24 '24 13:03 tdiesler