desktop-linux
desktop-linux copied to clipboard
Cannot see host's files when mounting hostPath on Kubernetes cluster
- [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:

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

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

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
#90 might be related
I got same problem today, looking for solution.
I got the same issue
I got the same issue
same problem, any clue?
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.
I confirm. The solution @TomerGodinger helped me too. Thank you. Why is that? Is it possible that this will change in the future?
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 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.
This workaround doesn't work for me on MacOS