microsoft / navcontainerhelper

Official Microsoft repository for BcContainerHelper, a PowerShell module, which makes it easier to work with Business Central Containers on Docker.
MIT License
382 stars 246 forks source link

Get-NavContainerSharedFolders returns error The length must not be less than 0 (zero). #2798

Closed silasvd closed 1 year ago

silasvd commented 1 year ago

Describe the issue I added the following parameter while creating a bc container -additionalParameters @('-v \.\pipe\docker_engine:\.\pipe\docker_engine')

This leads to the problem that Get-NavContainerSharedFolders stopped working correctly due to the following error message: The length must not be less than 0 (zero). Parameter name: length

Scripts used to create container and cause the issue

[string]$ContainerName = "CRONUS-BC18-01-DE"
[string]$artifactUrl  = "https://bcartifacts.azureedge.net/sandbox/18.1.24822.40616/de"

New-BcContainer `
    -accept_eula `
    -accept_outdated `
    -containerName $ContainerName `
    -artifactUrl $artifactUrl  `
    -memoryLimit 6G `
    -auth NavUserPassword `
    -Credential $cred `
    -updateHosts `
    -restart unless-stopped `
    -doNotCheckHealth `
    -shortcuts None `
    -PublishPorts @(80,443) `
    -isolation hyperv `
    -additionalParameters @('-v \\.\pipe\docker_engine:\\.\pipe\docker_engine')

Full output of scripts

BcContainerHelper is version 4.0.5
BcContainerHelper is running as administrator
Hyper-V is Enabled
UsePsSession is True
Host is Microsoft Windows 11 Pro - 21H2
Docker Client Version is 20.10.17
Docker Server Version is 20.10.17
Removing entries from hosts
Removing CRONUS-BC18-01-DE from container hosts file
Removing CRONUS-BC18-01-DE-* from container hosts file
Removing Desktop shortcuts
Removing C:\ProgramData\BcContainerHelper\Extensions\CRONUS-BC18-01-DE
Fetching all docker images
Fetching all docker volumes
Using image mcr.microsoft.com/businesscentral:10.0.20348.1249
Publishing port 80
Publishing port 443
Disabling Health Check (always report healthy)
Creating Container CRONUS-BC18-01-DE
Style: sandbox
Multitenant: Yes
Version: 18.1.24822.40616
Platform: 18.0.24800.40606
Generic Tag: 1.0.2.13
Container OS Version: 10.0.20348.1249 (ltsc2022)
Host OS Version: 10.0.22000.675 (21H2)
Using hyperv isolation
Using locale de-DE
Disabling the standard eventlog dump to container log every 2 seconds (use -dumpEventLog to enable)
Additional Parameters:
-v \\.\pipe\docker_engine:\\.\pipe\docker_engine
-v c:\temp:c:\temp
Files in C:\ProgramData\BcContainerHelper\Extensions\CRONUS-BC18-01-DE\my:
- AdditionalOutput.ps1
- MainLoop.ps1
- SetupVariables.ps1
- updatehosts.ps1
Creating container CRONUS-BC18-01-DE from image mcr.microsoft.com/businesscentral:10.0.20348.1249
452d70b49923667ac7a27df3a381053a75dd79293204d72a2d68f0bc24ff8a0a
Waiting for container CRONUS-BC18-01-DE to be ready
Adding CRONUS-BC18-01- to hosts file
Using artifactUrl https://bcartifacts.azureedge.net/sandbox/18.1.24822.40616/de
Using installer from C:\Run\150-new
Installing Business Central
Installing from artifacts
Starting Local SQL Server
Starting Internet Information Server
Copying Service Tier Files
c:\dl\sandbox\18.1.24822.40616\platform\ServiceTier\Program Files
c:\dl\sandbox\18.1.24822.40616\platform\ServiceTier\System64Folder
Copying PowerShell Scripts
c:\dl\sandbox\18.1.24822.40616\platform\WindowsPowerShellScripts\Cloud\NAVAdministration
c:\dl\sandbox\18.1.24822.40616\platform\WindowsPowerShellScripts\WebSearch
Copying Web Client Files
c:\dl\sandbox\18.1.24822.40616\platform\WebClient\Microsoft Dynamics NAV
Copying Client Files
c:\dl\sandbox\18.1.24822.40616\platform\LegacyDlls\program files\Microsoft Dynamics NAV
c:\dl\sandbox\18.1.24822.40616\platform\LegacyDlls\program files\Microsoft Dynamics NAV
c:\dl\sandbox\18.1.24822.40616\platform\LegacyDlls\systemFolder
Copying ModernDev Files
c:\dl\sandbox\18.1.24822.40616\platform
c:\dl\sandbox\18.1.24822.40616\platform\ModernDev\program files\Microsoft Dynamics NAV
Copying additional files
Copying ConfigurationPackages
C:\dl\sandbox\18.1.24822.40616\platform\ConfigurationPackages
Copying Test Assemblies
C:\dl\sandbox\18.1.24822.40616\platform\Test Assemblies
Copying Extensions
C:\dl\sandbox\18.1.24822.40616\de\Extensions
Copying Applications
C:\dl\sandbox\18.1.24822.40616\platform\Applications
Copying Applications.DE
C:\dl\sandbox\18.1.24822.40616\de\Applications.DE
Copying dependencies
Copying ReportBuilder
Importing PowerShell Modules
Restoring CRONUS Demo Database
Setting CompatibilityLevel for tenant on localhost\SQLEXPRESS
Exporting Application to CRONUS
Removing Application from tenant
Modifying Business Central Service Tier Config File for Docker
Creating Business Central Service Tier
Installing SIP crypto provider: 'C:\Windows\System32\NavSip.dll'
Starting Business Central Service Tier
Importing license file
Copying Database on localhost\SQLEXPRESS from tenant to default
Taking database tenant offline
Copying database files
Attaching files as new Database default
Putting database tenant back online
Mounting tenant database
Mounting Database for default on server localhost\SQLEXPRESS with AllowAppDatabaseWrite = False
Sync'ing Tenant
Tenant is Operational
Stopping Business Central Service Tier
Installation took 152 seconds
Installation complete
Initializing...
Setting host.docker.internal to 192.168.178.96 in container hosts file (copy from host hosts file)
Setting gateway.docker.internal to 192.168.178.96 in container hosts file (copy from host hosts file)
Setting kubernetes.docker.internal to 127.0.0.1 in container hosts file (copy from host hosts file)
Setting host.containerhelper.internal to 172.22.192.1 in container hosts file
Starting Container
Hostname is CRONUS-BC18-01-DE
PublicDnsName is CRONUS-BC18-01-DE
WARNING: Container starts with TimeZone = Mitteleuropäische Zeit, which is not recognized in the list of TimeZones.
Using NavUserPassword Authentication
Creating Self Signed Certificate
Self Signed Certificate Thumbprint 55E587FF39132178000AE78085A584DCE751B21B
DNS identity CRONUS-BC18-01-DE
Modifying Service Tier Config File with Instance Specific Settings
Starting Service Tier
Registering event sources
Creating DotNetCore Web Server Instance
Using application pool name: BC
Using default container name: NavWebApplicationContainer
Copy files to WWW root C:\inetpub\wwwroot\BC
Create the application pool BC
Create website: NavWebApplicationContainer without SSL
Update configuration: navsettings.json
Done Configuring Web Client
Enabling Financials User Experience
Dismounting Tenant
Mounting Tenant
Mounting Database for default on server localhost\SQLEXPRESS with AllowAppDatabaseWrite = False
Sync'ing Tenant
Tenant is Operational
Creating http download site
Setting SA Password and enabling SA
Creating admin as SQL User and add to sysadmin
Creating SUPER user
WARNING: The password that you entered does not meet the minimum requirements. 
It should be at least 8 characters long and contain at least one uppercase 
letter, one lowercase letter, and one number.
Container IP Address: 172.22.196.99
Container Hostname  : CRONUS-BC18-01-DE
Container Dns Name  : CRONUS-BC18-01-DE
Web Client          : http://CRONUS-BC18-01-DE/BC/?tenant=default
Dev. Server         : http://CRONUS-BC18-01-DE
Dev. ServerInstance : BC
Dev. Server Tenant  : default
Setting CRONUS-BC18-01-DE to 172.22.196.99 in host hosts file
Setting CRONUS-BC18-01-DE-default to 172.22.196.99 in host hosts file
Setting CRONUS-BC18-01-DE-default to 172.22.196.99 in container hosts file

Files:
http://CRONUS-BC18-01-DE:8080/ALLanguage.vsix

Container Total Physical Memory is 6.5Gb
Container Free Physical Memory is 4.1Gb

Initialization took 32 seconds
Ready for connections!
Reading CustomSettings.config from CRONUS-BC18-01-DE
Container CRONUS-BC18-01-DE successfully created
Get-BcContainerSharedFolders -containerName $ContainerName
Get-BcContainerSharedFolders Telemetry Correlation Id: 8511a82d-cd98-44be-886b-2ae0b978f30e
ForEach-Object : Ausnahme beim Aufrufen von "Substring" mit 2 Argument(en):  "Die Länge darf nicht kleiner als 0 (null) sein.
Parametername: length"
In C:\Users\svanduehren\Documents\WindowsPowerShell\Modules\BcContainerHelper\4.0.5\ContainerInfo\Get-NavContainerSharedFolders.ps1:28 Zeichen:37
+         $inspect.HostConfig.Binds | ForEach-Object {
+                                     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [ForEach-Object], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.ForEachObjectCommand

https://github.com/microsoft/navcontainerhelper/blob/master/ContainerInfo/Get-NavContainerSharedFolders.ps1 Maybe code could be changed like this:

$inspect = docker inspect $containerName | ConvertFrom-Json
$sharedFolders = @{}
if ($inspect.HostConfig.Binds) {
    $inspect.HostConfig.Binds | ForEach-Object {
        if ($_.StartsWith('\\')) {
            $idx = $_.IndexOf(':')
        }
        else {
            $idx = $_.IndexOf(':', $_.IndexOf(':') + 1)
        }
        $src = $_.Substring(0, $idx).TrimEnd('\')
        $dst = $_.SubString($idx+1)
        if ($dst.Contains(':')) {
            $idx = $dst.IndexOf(':', $_.IndexOf(':') + 1)
            if ($idx -gt 0) {
                $dst = $dst.SubString(0,$idx)
            }
        }
        $sharedFolders += @{ $src = $dst }
    }
}
freddydk commented 1 year ago

There was actually another bug in that code, introduced in the fix for https://github.com/microsoft/navcontainerhelper/issues/929 but never found.

If anything, this code: $idx = $dst.IndexOf(':', $_.IndexOf(':') + 1) should have been $idx = $dst.IndexOf(':', $dst.IndexOf(':') + 1)

But with the new fix, but are fixed.

freddydk commented 1 year ago

Shipped in 4.0.7