SMB share mappings created with New-SmbGlobalMapping for docker containers not restored after reboot on windows server 1803
Asked Answered
E

2

7

I am trying to create a simple Docker host to try using containers for some .net projects.

I have setup a Windows Server 1803 host and installed Docker EE with powershell and it is running as a service correctly.

I wanted to use the new "SMB Global Mapping" feature available since 1709 to map a samba share on my domain and use it in containers without resorting to gMSA or other tricks, and I wanted it to automount and start the containers at reboot with docker restart policies, as if they were windows services.

I run these commands and everything worked

$creds = Get-Credential 

New-SmbGlobalMapping -RemotePath \\contosofileserver\share1 -Credential $creds -LocalPath G:

docker run -v G:/:G: -it test cmd.exe

but after a host reboot, G: is not mapped anymore so I cannot ideally place the container on auto-start. I guess that it's because of the credentials not persisted anywhere, but even after that I doubt that the powershell command will make anything persistent as it is, also because it lacks the -Persistent parameter of the standard New-SmbMapping commandlet.

Ecumenical answered 18/5, 2018 at 16:12 Comment(1)
bloggingforlogging.com/2018/11/22/…Conservator
C
5

I also use this cmdlet with Windows Server 1803 and Docker. To solve this problem I do the following:

Create this PS1 script in C:\data\smbshare.ps1

$secpasswd = ConvertTo-SecureString 'password' -AsPlainText -Force;
$creds = New-Object System.Management.Automation.PSCredential ("domain\user", $secpasswd);
New-SmbGlobalMapping -RemotePath 'RemotePath' -Credential $creds -LocalPath X:;

Now, create a Scheduled Task that start with the server StartUP. I do this with this cmdlet:

$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-file C:\data\smbshare.ps1" -WorkingDirectory "C:\data";
$Trigger = New-ScheduledTaskTrigger -AtStartup;
$Settings = New-ScheduledTaskSettingsSet -DontStopOnIdleEnd -RestartInterval (New-TimeSpan -Minutes 1) -RestartCount 10 -StartWhenAvailable;
$Settings.ExecutionTimeLimit = "PT0S";
$SecurePassword = ConvertTo-SecureString 'password' -AsPlainText -Force;
$UserName = "domain\user";
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword;
$Password = $Credentials.GetNetworkCredential().Password;
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings;
$Task | Register-ScheduledTask -TaskName 'SMBGlobalShare' -User "domain\user" -Password $Password;
Conservancy answered 1/8, 2018 at 19:12 Comment(2)
thank you, this is nice. isn't it strange that microsoft went all the way on the "we love docker", but didn't think that docker for windows needed persisted credentials and shares for windows applications in an easier way?Ecumenical
Fantastic. To help with the googles, this approach solves authentication problems (e.g., invalid mount config for type "bind": CreateFile username password) for me as opposed to using the "net" command recommended elsewhereWommera
H
11

For those stumbling on this, New-SmbGlobalMapping has a Persistent flag that needs to be set to $true, i.e.

New-SmbGlobalMapping -Persistent $true -RemotePath \\contosofileserver\share1 -Credential $creds -LocalPath G:
Hypothesize answered 9/1, 2020 at 10:47 Comment(0)
C
5

I also use this cmdlet with Windows Server 1803 and Docker. To solve this problem I do the following:

Create this PS1 script in C:\data\smbshare.ps1

$secpasswd = ConvertTo-SecureString 'password' -AsPlainText -Force;
$creds = New-Object System.Management.Automation.PSCredential ("domain\user", $secpasswd);
New-SmbGlobalMapping -RemotePath 'RemotePath' -Credential $creds -LocalPath X:;

Now, create a Scheduled Task that start with the server StartUP. I do this with this cmdlet:

$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-file C:\data\smbshare.ps1" -WorkingDirectory "C:\data";
$Trigger = New-ScheduledTaskTrigger -AtStartup;
$Settings = New-ScheduledTaskSettingsSet -DontStopOnIdleEnd -RestartInterval (New-TimeSpan -Minutes 1) -RestartCount 10 -StartWhenAvailable;
$Settings.ExecutionTimeLimit = "PT0S";
$SecurePassword = ConvertTo-SecureString 'password' -AsPlainText -Force;
$UserName = "domain\user";
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword;
$Password = $Credentials.GetNetworkCredential().Password;
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings;
$Task | Register-ScheduledTask -TaskName 'SMBGlobalShare' -User "domain\user" -Password $Password;
Conservancy answered 1/8, 2018 at 19:12 Comment(2)
thank you, this is nice. isn't it strange that microsoft went all the way on the "we love docker", but didn't think that docker for windows needed persisted credentials and shares for windows applications in an easier way?Ecumenical
Fantastic. To help with the googles, this approach solves authentication problems (e.g., invalid mount config for type "bind": CreateFile username password) for me as opposed to using the "net" command recommended elsewhereWommera

© 2022 - 2024 — McMap. All rights reserved.