Is there a way to share drives via command line?

Is there a way to share drives via a command line, other then accessing Settings/Shared Drives UI ? I looked in the documentation, did not find anything on this.

We are deploying several instances running Docker for Windows automatically, but we have not found anything to automate that part.

I may have found something here: http://docker-saigon.github.io/post/Docker-Beta/, under section “Mounting Volumes”

Mostly based on the link shared above:

For instance, I want to share drives within this context:

username: johndoe
password: qwerty
Drives to share: C and D

Add credentials to Credentials Manager

Cmdkey.exe /generic:"Docker Host Filesystem Access" /user:DOMAIN\johndoe /pass:qwerty

Share drives as required

net share C=C:\ /grant:johndoe,FULL /CACHE:None
net share D=D:\ /grant:johndoe,FULL /CACHE:None

This is where it gets tricky… setting up data for the guest VM to obtain the credentials…

I should be adding a HostExchangeItem under this, but don’t know how to do it:

$VmMgmt = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService
$vm = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter {ElementName='MobyLinuxVM'}

($vm.GetRelated("Msvm_KvpExchangeComponent")[0] ).GetRelated("Msvm_KvpExchangeComponentSettingData").HostExchangeItems | % { $GuestExchangeItemXml = ([XML]$_).SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Name']/VALUE[child::text() = 'cifsmount']") 
        if ($GuestExchangeItemXml -ne $null) 
        { 
           $GuestExchangeItemXml.SelectSingleNode(` 
            "/INSTANCE/PROPERTY[@NAME='Data']/VALUE/child::text()").Value 
        }    
    }

I guess this is not really something other people are pursuing…

We want to be able to automate our Docker for Windows usage. Some of us out there need to run Docker on Windows as part of build farms. It’s really difficult to achieve right now: there are limitations, such as having to change some settings via the UI only.

Furthermore, when restarting docker service lately, we have got a pop up that asks /Do you recommend Docker for Windows?/ and while the pop up is waiting for an answer, the service is not starting. That is clearly a no-no for unattended/automated Docker for Windows execution !

I have a different use-case, but being able to share from CLI would solve it for me. I’m using file-based BitLocker-encrypted drive for my projects. And docker keeps forgetting that I shared it. I created an issue on github for that. https://github.com/docker/for-win/issues/1650

I’ve had the same problem, plus the docker dialogue doesn’t prompt properly in Windows 10 so it makes it especially difficult.

Would it help to write a script to check for the following? (the reason I ask is that I have done this before and I think I could accomplish the task pretty easily)

  1. Check for a local user dev_agent_1 and if missing create it with some permissions.
    2.Check for the docker host access, if missing run:
    Cmdkey.exe /generic:“Docker Host Filesystem Access” /user:dev_agent_1 /pass:password
  2. Check for a share, if missing create it using the following:
    net share c=c:\ /grant:dev_agent_1,FULL /CACHE:None

Would it also be helpful to run the docker agent service as a particular service?

I know this is years old, but I am pretty desperate, so I may as well make the attempt: did you ever find a solution for this particular problem? If you did, do happen to remember what it was?

2 Likes

Ditto here. Trying to get a working install of docker for windows requires this (volumes), and as such, it should be able to be automated.

Hi Gdouaire, have you find out a solution for this? would you please share it? thanks a lot.

I am also looking for a solution for this. But not only sharing of drives, also setup of memory limits for docker.

Since Docker Desktop can remember that I have shared a drive after I restart Windows, it must be saved somewhere. Is there a settings file or a registry key that can be edited?

We never found the way to do it via a script or on the command line…

When sharing a drive in the UI, the file C:\Users<username>\AppData\Roaming\Docker\settings.json is updated with the section:
“sharedDrives”: {
“C”: true,
“E”: false
}
but that settings file does not list the credentials used to map the drive. The files:
C:\ProgramData\DockerDesktop\vm-data\DockerDesktop.vhdx
C:\Users\All Users\DockerDesktop\vm-data\DockerDesktop.vhdx

are also updated, so maybe the credential information is baked into the VM image files.

It looks like credentials are stored in the windows credentialmanager.
The command “cmdkey /list” gives:
Target: LegacyGeneric:target=Docker Host Filesystem Access
Type: Generic
User: [domain]\[username]

But when I try to add credentials on my own with:
cmdkey /generic:“Docker Host Filesystem Access” /user:[domain]\[username] /pass:[password]

and then start Docker Desktop I get this error:
The parameter is incorrect.

at System.Security.Cryptography.ProtectedData.Unprotect(Byte encryptedData, Byte optionalEntropy, DataProtectionScope scope)
at Docker.WPF.Credentials.MountCredentialAsker.DecryptPassword(Byte encrypted)
at Docker.WPF.Credentials.MountCredentialAsker.RetrieveCredential(String target)
at Docker.Actions.PreStartCheck(ContainerEngineMode engine)
at Docker.Actions.DoStart(SynchronizationContext syncCtx, Boolean showWelcomeWindow, Boolean executeAfterStartCleanup)
at Docker.Actions.<>c__DisplayClass24_0.b__0()
at Docker.ApiServices.TaskQueuing.TaskQueue.<>c__DisplayClass17_0.<.ctor>b__1()

1 Like

This is also something I am interested in from the standpoint of automated installation and configuration. Did anyone get this working? I am going to attempt the steps detailed above and see whether they now work (I am on the latest build of Windows 10 through the fast channel).

I found this: https://github.com/docker/for-win/issues/5139#issuecomment-559700215 it seems that the newest version of Docker Desktop uses a more integrated way of sharing the drive.

I’m also looking for this. Have you found a way in the past months?

docker run --name=nginx -d -v ~/nginxlogs:/var/log/nginx -p 5000:80 nginx

Taken from here Step 1 — Bindmounting a Volume

Mounting a volume is straightforward – it’s sharing the drives in Docker Desktop that is much more complicated.

create the mount point outside of the docker container, mount that as described and then directly share that mounted folder as a usual windows (shared) folder