Docker add network drive as volume on windows
Asked Answered
C

3

29

I am trying to mount a network drive as a volume. This is the command I am trying

docker run -v //NetworkDirectory/Folder:/data alpine ls /data

I am running this command on windows and the data directory is coming up empty. How can I mount this network directory as a volume on the windows host and access it inside the container?

Working with local directories works just fine, so the following command works as expected.

docker run -v c:/Users/:/data alpine ls /data

I can make it work in linux since I can mount the share with cifs-utils on a local directory and use that directory as the volume.

Edit: Looks like this is not possible: How to mount network Volume in Docker for Windows (Windows 10)

Currey answered 8/5, 2018 at 17:39 Comment(0)
J
37

My colleague came up with this and it works with our company network drive and it might help someone out there.

We start by creating a docker volume named mydockervolume.

docker volume create --driver local --opt type=cifs --opt device=//networkdrive-ip/Folder --opt o=user=yourusername,domain=yourdomain,password=yourpassword mydockervolume
  • --driver specifies the volume driver name
  • --opt Sets driver specific options. I guess they are given to the linux mount command when the container starts up.

We can then test that the volume works with
docker run -v mydockervolume:/data alpine ls /data

Here you can read more about driver specific options and docker volume create

Jael answered 15/8, 2019 at 13:8 Comment(10)
Explaining what those do and where you tested them might help. From the phrase "My colleague gave me this" seems like you didn't tested, just asked around...Heteroclite
@JulioCezarSilva I tried to explain a little bit more. I use this code myself since I cannot mount volumes locally on my current machine (no admin and firewall settings).Jael
How do you find your 'domain'??Balalaika
@EsbenEickhardt - I think the default workgroup is often "WORKGROUP". But the network drive does not need to have a workgruop. You should probably try to remove "domain=yourdomain," from the argument.Jael
I followed this carefully. docker volume create occurred without error (but I don't know if it checks much). An attempt to do the docker test run resulted in an "invalid argument" message. Failed to mount local volume.Unity
This only works for Linux, right? Since windows volume command does not support any options. The title of this question says windows.Gustie
@Json This only works with a Linux container as far as I know (alpine is linux). The host machine was a windows machine as in I was using a windows pc for development.Jael
@MarcusAlsterman, could you give an example of how the networkdrive-ip looks like? I am using something on the lines of //192.xxx.x.x/<foldername>. This creates a volume, but an error comes up while mounting it. Any leads will be great! Thanks.Prickle
I got the same "invalid argument" message that @Unity got. I also noticed that this saves your password in clear text. if you run docker volume inspect mydockervolume you can see it right there.Fiedling
I got invalid argument because the network drive was using the name of the drive i.e. mynas.local or just mynas. This will not work you need to specify the ip address.Asp
R
5

I found this when looking for something similar but see that though it's old it's missing some key information, possibly because they weren't available at the time

The CIFS storage is, I believe, only for when you are connecting to a Windows System as I do not believe it is used by Linux at all.

EDIT: It looks like Docker considered SMB(Samba) to be CIFS Volumes

This same thing can be done with NFS, which is less secure, but is supported by almost everything.

you can create an NFS volume in a similar way to the CIFS one, just with a few changes. I'll list both so they can be seen side by side

When using NFS on WSL2 you 1st need to install the NFS service into the Linux Host OS. I believe CIFS requires a similar one, but as I don't use it I'm not certain.

EDIT: It looks like WSL2 Docker at least for SMB(Samba), CIFS Volumes either don't require any dependencies or I already have them, possibly the same one I install for NFS bellow

In my case the Host OS is Ubuntu, but you should be able to find the appropriate one by finding your system's equivalent for nfs-common installation

sudo apt update
sudo apt install nfs-common

That's it. That will install the service so NFS works on Docker (It took me forever to realize that was the problem since it doesn't seem to be mentioned as needed anywhere)


On the network device you need to have set NFS permissions for the NFS folder, in my case this would be done at the folder folder with the mount then being to a folder inside it. That's fine. In my case the NAS that is my server mounts to #IP#/volume1/folder, within the NAS I never see the volume1 in the directory structure, but that full path to the shared folder is shown in the settings page when I set the NFS permissions. I'm not including the volume1 part as your system will likely be different & you want the FULL PATH after the IP (use the IP as the numbers NOT the HostName), according to your NFS share, whatever it may be.

  • The nolock option is often needed but may not be on your system. It just disables the ability to "lock" files.
  • The soft option means that if the system cannot connect to the mount directory it will not hang. If you need it to only work if the mount is there you can change this to hard instead.
  • The rw (read/write) option is for Read/Write, ro (read-only) would be for Read Only

As I don't personally use the CIFS volume the options set are just ones in the examples I found, whether they are necessary for you will need to be looked into.

  • The username & password are required & must be included for CIFS
  • uid & gid are Linux user & group settings & should be set, I believe, to what your container needs as Windows doesn't use them to my knowledge
  • file_mode=0777 & dir_mode=0777 are Linux Read/Write Permissions essentially like chmod 0777 giving anything that can access the file Read/Write/Execute permissions (More info Link #4) & this should also be for the Docker Container not the CIFS host
  • noexec has to do with execution permissions but I don't think actually function here, nosuid limits it's ability to access files that are specific to a specific user ID & shouldn't need to be removed unless you know you need it to be, as it's a protection, nosetuids means that it won't set UID & GUID for newly created files, nodev means no access to/creation of devices on the mount point, vers=1.0 I think is a fallback for compatibility, I personally would not include it unless there is a problem or it doesn't work without it

In these examples I'm mounting //NET.WORK.DRIVE.IP/folder/on/addr/device to a volume named "my-docker-volume" in Read/Write mode. The CIFS volume is using the user supercool with password noboDyCanGue55

NFS from the CLI

docker volume create --driver local --opt type=nfs --opt o=addr=NET.WORK.DRIVE.IP,nolock,rw,soft --opt device=:/folder/on/addr/device my-docker-volume

CIFS from CLI (May not work if Docker is installed on a system other than Windows, will only connect to an IP on a Windows system)

docker volume create --driver local --opt type=cifs --opt o=user=supercool,password=noboDyCanGue55,rw --opt device=//NET.WORK.DRIVE.IP/folder/on/addr/device my-docker-volume

This can also be done within Docker Compose or Portainer. When you do it there, you will need to add a Volumes: at the bottom of the compose file, with no indent, on the same level as services:

In this example I am mounting the volumes

  • my-nfs-volume from //10.11.12.13/folder/on/NFS/device to "my-nfs-volume" in Read/Write mode & mounting that in the container to /nfs
  • my-cifs-volume from //10.11.12.14/folder/on/CIFS/device with permissions from user supercool with password noboDyCanGue55 to "my-cifs-volume" in Read/Write mode & mounting that in the container to /cifs
version: '3'
services:
  great-container:
    image: imso/awesome/youknow:latest
    container_name: totally_awesome
    environment:
      - PUID=1000
      - PGID=1000
    ports:
      - 1234:5432
    volumes:
      - my-nfs-volume:/nfs
      - my-cifs-volume:/cifs

volumes:
  my-nfs-volume:
   name: my-nfs-volume
   driver_opts:
      type: "nfs"
      o: "addr=10.11.12.13,nolock,rw,soft"
      device: ":/folder/on/NFS/device"
  my-cifs-volume:
    driver_opts:
      type: "cifs"
      o: "username=supercool,password=noboDyCanGue55,uid=1000,gid=1000,file_mode=0777,dir_mode=0777,noexec,nosuid,nosetuids,nodev,vers=1.0"
      device: "//10.11.12.14/folder/on/CIFS/device/"

More details can be found here:

  1. https://docs.docker.com/engine/reference/commandline/volume_create/
  2. https://www.thegeekdiary.com/common-nfs-mount-options-in-linux/
  3. https://web.mit.edu/rhel-doc/5/RHEL-5-manual/Deployment_Guide-en-US/s1-nfs-client-config-options.html
  4. https://www.maketecheasier.com/file-permissions-what-does-chmod-777-means/
Richierichlad answered 21/11, 2022 at 16:38 Comment(0)
B
1

I didn't find a native CIFS storage driver on docker.

You can use an external volume plugin like this one: https://github.com/ContainX/docker-volume-netshare which support NFS, AWS EFS & Samba/CIFS

Boarhound answered 8/5, 2018 at 18:47 Comment(2)
Thanks, this actually allows us to mount shares from the container. While I would like to add the share as a volume to the container from windows host. Seeing this link it doesn't look like it's possible. #42287908Currey
Yep, this is not possible. You should use nfs or iSCSI for a similar setupBoarhound

© 2022 - 2024 — McMap. All rights reserved.