How to map LAN network share to Docker volume?

My host machine is Windows 10 in LAN. The LAN has same network file storage shares.
How to map LAN network share to Docker volume to make the share accessible inside of docker?

I.e. my dockerized ASP NET Core cross-platform app should enumerate files in the LAN like this

public void ConfigureServices(IServiceCollection services)
{
    try
    {
        Console.WriteLine("****TEST: GetFiles");
        string[] filePaths = Directory.GetFiles(@"\\Share1\FolderMix\");
        foreach (var f in filePaths)
        {
            Console.WriteLine(@"***file: {0}", f);
        }
    }
    catch(Exception x)
    {
        Console.WriteLine(@"***Exception: {0}", x.Message);
    }

To not bloat the required capabilities unnecessarily, the recommended approach is to use volumes instead (pointing to your CIFS shares) and map them into a folder inside the container, then use them in your containerized app as you would use any local folder.

Either create your volume first and then use it in docker run commands:

docker volume create \
    --driver local \
    --opt type=cifs \
    --opt o=username={smbuser},password={smbpass},uid={UID for mount},gid={gid for mount},vers=3.0 \
    --opt device=//Share1/FolderMix \
    foldermix

or declare it in a docker-compose.yml:

volumes:
  foldermix:
    driver_opts:
      type: cifs
      o: username={smbuser},password={smbpass},uid={UID for mount},gid={gid for mount},vers=3.0
      device: //Share1/FolderMix

Make sure to use valid credentials for smbuser/smbpass and that the uid/gid matches those of the user inside the container that start your application inside the container.

1 Like

Thanx, yet some questions:

  1. What is recommended to use in this case: CIFS or NFS?
  2. I have a netwotk share with permissions for Everyone Full rights. What should I use here
    o: username={smbuser},password={smbpass},uid={UID for mount},gid={gid for mount},
    smbuser?
    smbpass?
    UID?
    gid?

My situation allows to use NFS v4 and I prefer it over CIFS (not NFS v3 though!) . Instead of requiring user credentials, you need to whitelist the docker host in the NFS export (of the server) to allow access to the share.

Appart from that: it is up to you and whatever is given in your environment.

I don’t know how to retrieve the correct UID and GID, so this is useless advice.

I like that sentiment :slight_smile:

just type “id” in console to get that info. Just use some of your time to find how to retrieve it and dont waste time in useless comments.

1 Like

Hi 4y later!

I think this solves my problem (trying to get a drive shared by Samba in a Pi available to a docker instance in Windows) but I can’t get docker to mount it and I can’t find proper documentation about it. (I’m super new to this so very likely I just don’t understand what’s relevant)

Is the username/password mandatory? My share does not have one nor have I created a user for it.
How does one access the share in the compose file?

I have this:

services:
  immich-server:
    container_name: immich_server
    image: ghcr.io/immich-app/immich-server:${IMMICH_VERSION:-release}
    command: [ "start.sh", "immich" ]
    volumes:
      - ${UPLOAD_LOCATION}:/usr/src/app/upload
      - /etc/localtime:/etc/localtime:ro
      - D:\Me:/mnt/media/Me:ro
      - cifs:/Me/Fotos:/mnt/media/Me

and then on volumes, at the bottom:

volumes:
  pgdata:
  model-cache:
  cifs:
    driver: local
    driver_opts:
      type: cifs
      o: uid=plex,gid=plex,vers=3.0
      device: //192.168.1.82/Me/Fotos

Appreciate any feedback, feel like I’m close but doing something stupid.

I have no idea.

This is not possible. You can not mount a specific folder of the volume to a container, just the whole volume:

      - cifs:/mnt/media/Me

The matching declaration would look like this (remove username=myuser,password=mypass, to test if it works without credentials)

  cifs:
    driver_opts:
      type: cifs 
      o: username=myuser,password=mypass,uid=1000,gid=100,vers=3.0
      device: //192.168.1.82/Me/Fotos

The user id and group id are numeric ids, not names. They must match those, the container uses to run the main process. Check the Docker HUB description of the used image whether it describes which UID:GID it uses, or check if it allows setting them as environment variables (some images do, most don’t).

NOTE: a volume declaration is immutable! Once created, changes to the compose file are not reflected back to the configuration of the volume. In order to update the volume configuration, you must stop your compose project, delete the volume, then re-deploy the compose project.

Thanks for the super quick reply!
I thought the uid/gid were the ones set in Pi in /etc/fstab.

I will try to find them for the image, applied the changes you mentioned and tried with 1000/1000, no dice.
Also appreciate the tip on killing the volume after stopping the container.
If after getting the proper uid/gid still doesnt work I’ll look into adding a username and pass.

Nope. Every volume must be configured from the perspective and needs of a particular container.

You can find the UID and GID required by a container by running these commands:

# find user name that runs the main process in the output
docker exec -u 0 {container id or name } ps
# find uid/gid of the user 
docker exec -u 0 {container id or name } id {USER} 

Thanks for the feedback.
Are there specificities to docker win?

I’ve tried “docker exec -u 0 immich_microservices ps” with one of the containers and got

OCI runtime exec failed: exec failed: unable to start container process: exec: “ps”: executable file not found in $PATH: unknown

You try to execute a command that either does not exist or requires an absolute path, because its path is not present in the $PATH variable.

Hi - I have a related question here where I have successfully added the volumes in docker for my smb shares but was wondering if you can then specify a folder within that volume??

For example, I have a volume that points to my “media” share - but inside that shared drive are separate folders for TV, Movies & Music. Do I have to set up these folders as their own volumes? Or can I somehow point to these folders inside these volumes? Would make more sense and be much cleaner to have one volume rather than 3 but I can’t seem to find a way to make this happen

You can’t. It is not possible.

It would. But it is not possible with Docker… Though, Kubernetes volume mounts support it. I guess it’s easier to raise a feature request in the issues of the compose specification Github project.

Hi, I’m in a similar situation to yours.
I’m setting up an immich container in a docker environment.
Proxmox → Debian VM → docker ->immich

I have another Truenas VM with an SMB share that I would like to use for immich

Proxmox → Truenas VM → Share SMB immich

I would like to replace “UPLOAD_LOCATION=./library” in the .env with something that points to the share on Truenas.

How did you solve the problem?