18

I'm using Docker Desktop on Windows 10. For the purposes of development, I want to expose a local folder to a container. When running the container in Docker, I do this by specifying the volume flag (-v).

How do I achieve the same when running the container in Kubernetes?

Røye
  • 1,077
  • 3
  • 14
  • 27

3 Answers3

14

You should use hostpath Volume type in your pod`s spec to mount a file or directory from the host node’s filesystem, where hostPath.path field should be of following format to accept Windows like paths:

  • /W/fooapp/influxdb
  • //W/fooapp/influxdb
  • /////W/fooapp/influxdb

Please check this github issue explaining peculiarities of Kubernetes Volumes on Windows. I assume also that you have enabled Shared Drives feature in your Docker for Windows installation.

Jory Geerts
  • 1,916
  • 15
  • 25
Nepomucen
  • 4,449
  • 3
  • 9
  • 24
  • 2
    Thank you for the detailed answer. This works for me, and seems to be the best quick and dirty way to share my local folder. I managed to share my folder using "/c/temp/data" as the hostpath. I had tried this before, but it turns out that I was running into the Shared drives issue that you mention (as I was attempting to share the drive using an Active Directory account). – Røye Jan 07 '19 at 14:40
  • Hello, this seems to solve the error for me, anyway the path ofthe folder that gets monted is not what I expected it to be. For example using `path: /C/config` and `type: DirectoryOrCreate` the folder gets created and everything works, but I have no idea where the folder is in the host filesystem (it is not in C:/config) – daniele piscaglia Nov 11 '20 at 09:30
  • 6
    For those encountering my same problem (previous comment), check this: github.com/docker/for-win/issues/7023 And the solution basically was to use: /run/desktop/mnt/host/c/dir/otherDir (source: https://stackoverflow.com/questions/62812948/volume-mounts-not-working-kubernetes-and-wsl-2-and-docker) – daniele piscaglia Nov 18 '20 at 08:17
  • Also read this https://stackoverflow.com/questions/71018631/kubernetes-on-docker-for-windows-persistent-volume-with-hostpath-gives-operatio – Mark Parris Sep 04 '22 at 14:13
9

Using k8s 1.21.5 the following type of path worked for me:

/run/desktop/mnt/host/c/PATH/TO/FILE

Digging through this github issue helped me resolve which path to use: https://github.com/kubernetes/kubernetes/issues/59876

darophi
  • 456
  • 5
  • 15
  • is anyone available to comment on WHY this works? it just worked for me, but no clue why – RobbieS Jan 25 '22 at 17:08
  • this is annoying, if I use /c/path, I get "operation not permitted" error running a simple ls, this magical path works (dockerdesktop with kubernetes on wsl2) – cyberguest Jul 20 '22 at 18:34
4

The explanation is on the github link above.

The folder mount for /run/desktop/mnt/host/c does not exist on the distro you installed in WSL2 - on that WSL2 distro, the mount point to your C:\ drive is the more obvious /mnt/c.

Realize that Kubernetes and Docker are not installed in your installed WSL2 distro. Instead, Docker Desktop for Windows creates its own WSL2 VM called docker-desktop and installs Docker and Kubernetes on that VM. Then Docker Desktop for Windows installs the docker and kubectl CLIs on your WSL2 distro (and also on your Windows machine) and configures them all to point to the Docker and Kubernetes instances it created on the docker-desktop VM. This docker-desktop VM is hosting Docker and Kubernetes and also contains the /run/desktop/mnt/host/c mount point to your Windows C:\ drive and that can be used by your containers to persist data.

You can remote into the docker-desktop VM and see the /run/desktop/mnt/host/c mount point and folder structure by following the instructions (and discussion) at https://stackoverflow.com/a/62117039/11057678:

docker run -it --rm --privileged --pid=host justincormack/nsenter1
plplmax
  • 1,765
  • 8
  • 19