Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 888
francesco paolo schiavone Avatar asked May 18 '18 16:05

francesco paolo schiavone


2 Answers

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:
like image 70
Rafael Rivera Avatar answered Nov 08 '22 07:11

Rafael Rivera


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;
like image 5
João Ernesto Arzamendia Avatar answered Nov 08 '22 06:11

João Ernesto Arzamendia