microsoft / navcontainerhelper

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

Initialization of container failed #3242

Open PRA-TRM opened 7 months ago

PRA-TRM commented 7 months ago

Describe the issue After running the below script getting error : Error How to resolve this?

Scripts used to create container and cause the issue

$containerName = "xxxproduction"
$vaultName = "xxxxxx"
$environment = "PROD"
$installApps = @(
    "\xxx.app"
    "\xxx.app"
)

if (!($licenseFileSecret)) {
    Write-Host -ForegroundColor Yellow "Reading Key Vault"
    Get-AzKeyVaultSecret $vaultName  | % {
        Write-Host $_.Name
        Set-Variable `
            -Name "$($_.Name)Secret" `
            -Value (Get-AzKeyVaultSecret $vaultName -Name $_.Name)
    }
}

<#Write-Host -ForegroundColor Yellow "SaaS Settings"
$refreshToken = $BcSaasRefreshTokenSecret.SecretValue | Get-PlainText
$authContext = New-BcAuthContext -refreshToken $refreshToken
$bcEnvironment = Get-BcEnvironments `
    -bcAuthContext $authContext | Where-Object {
        $_.Name -eq $environment }
$baseApp = Get-BcPublishedApps `
    -bcAuthContext $authContext `
    -environment $environment | Where-Object { 
        $_.Name -eq "Base Application" }
$artifactUrl = Get-BCArtifactUrl `
    -country $bcEnvironment.countryCode `
    -version $baseApp.Version `
    -select Closest

$UseLatestBackup = $true
$stoAcc = Get-AzStorageAccount | Where-Object { 
    $_.StorageAccountName -eq ($storageAccountNameSecret.SecretValue | Get-PlainText) }
$stoToken = $stoAcc | New-AzStorageAccountSASToken `
    -Service Blob `
    -ResourceType Container,Object `
    -Permission "cdrw" `
    -ExpiryTime (Get-Date).AddHours(25) `
    -Protocol HttpsOnly
if ($UseLatestBackup) {
    $export = Get-BcDatabaseExportHistory `
        -bcAuthContext $authContext `
        -environment $environment | Sort-Object `
            -Property blob | Select-Object -Last 1
    if ($export) {
        Write-Host -ForegroundColor Yellow "Use Latest Database Export"
        $blobCtName = $export.container
        $blobName = $export.blob
    }
    else {
        $UseLatestBackup = $false
    }
}
if (!$UseLatestBackup) {
    Write-Host -ForegroundColor Yellow "Create New Database Export"
    $uri = "$($stoAcc.PrimaryEndpoints.Blob)$stoToken"
    $blobCtName = "$($environment)backup".ToLowerInvariant()
    $blobName = "$([datetime]::Now.ToString('yyyyMMddHHmm')).bacpac"
    New-BcDatabaseExport `
        -bcAuthContext $authContext `
        -environment $environment `
        -storageAccountSasUri $uri `
        -blobContainerName $blobCtName `
        -blobName $blobName
}#>

Write-Host -ForegroundColor Yellow "Download Database Export"
$uri = "$($stoAcc.PrimaryEndpoints.Blob)$blobCtName/$blobName$stoToken"
$backupFile = "xxx.bacpac"
<#Download-File `
    -sourceUrl $uri `
    -destinationFile $backupFile#>

$auth = "UserPassword"
$passwordSecret = "P@ssw0rd"
$credential = New-Object pscredential 'admin', (ConvertTo-SecureString 'P@ssw0rd' -AsPlainText -Force)
$licenseFile = 'xxx.bclicense'
$dbCredential = New-Object pscredential 'admin', (ConvertTo-SecureString 'xxx' -AsPlainText -Force)
$artifactUrl = "https://bcartifacts.azureedge.net/sandbox/22.5.59966.61937/nl"
$databaseParams = @{
    "databaseServer" = "localhost\MSSQLSERVER01"
    "databaseInstance" = ""
    "databasePrefix" = "$($containerName)-"
    "databaseName" = "SAT"
    "databaseCredential" = $dbCredential
    "multitenant" = $true
}
New-BcContainer @databaseParams -replaceExternalDatabases `
    -accept_eula `
    -containerName "$containerName" `
    -credential $credential `
    -auth $auth `
    -artifactUrl $artifactUrl `
    -enableTaskScheduler `
    -licenseFile $licenseFile `
    -updateHosts

    New-BCContainer -accept_eula `
    -containerName $containerName `
    -artifactUrl $artifactUrl `
    -credential $credential `
    -auth $auth `
    -updateHosts `
    -multitenant:$true `
    -alwaysPull `
    -licenseFile "xxx.bclicense"

$tenantId = $environment
$dacdll = Get-Item "C:\Program Files\Microsoft SQL Server\*\DAC\bin\Microsoft.SqlServer.Dac.dll"
if (!($dacdll)) {
    throw "DAC Framework is not installed"
}
Add-Type -path $dacdll.FullName
$databaseName = "$($databaseParams.databasePrefix)$tenantId"
$conn = @(
    "Data Source=localhost"
    "Initial Catalog=master"
    "Connection Timeout=0"
    "User Id=$($dbCredential.UserName)"
    "Password=$($dbCredential.Password|Get-PlainText)"
) -join ";"
Write-Host "Restoring Database from $backupFile as $databaseName"
$AppimportBac = New-Object Microsoft.SqlServer.Dac.DacServices $conn
$ApploadBac = [Microsoft.SqlServer.Dac.BacPackage]::Load($backupFile)
$AppimportBac.ImportBacpac($ApploadBac, $databaseName)

Publish-BCContainerApp -containerName $containerName -appFile $installApps -skipVerification -sync -install

$dockerApps = Get-BcContainerAppInfo `
    -containerName $containerName
$tenantApps = Get-BcInstalledExtensions `
    -bcAuthContext $authContext `
    -environment $environment 
$allApps = $true
$removeApps = $tenantApps | ForEach-Object {
    $appId = $_.id
    $appName = $_.DisplayName
    $appPub = $_.Publisher
    $appVer = [Version]::new(
        $_.versionMajor,
        $_.versionMinor,
        $_.versionBuild,
        $_.versionRevision)
    $app = $dockerApps | Where-Object { $_.appid -eq $appid }
    if (!($app)) {
        Write-Host "- $appName from $appPub version $appVer is missing"
        $appName
        $allApps = $false
    }
    elseif ($app.Version -lt $appVer) {
        Write-Host "- $appName from $appPub is version $($app.Version)."
        Write-Host "  Version expected was $appVersion"
        $appName
        $allApps = $false
    }
}

if ($allApps) {
    Write-Host -ForegroundColor Green "All apps are present in container"
}
else {
    Invoke-ScriptInBcContainer `
        -containerName $containerName `
        -scriptBlock { Param($dbServer,$dbInstance,$dbCredential,$dbName,$removeApps)
            $removeApps | ForEach-Object {
                Write-Host "Remove $_"
                Invoke-SqlCmdWithRetry `
                    -DatabaseServer $dbServer `
                    -DatabaseInstance $dbInstance `
                    -DatabaseName $dbName `
                    -databaseCredentials $dbCredential `
                    -maxattempts 1 -Query "
                DELETE FROM [dbo].[NAV App Published App]
                WHERE Name = '$_'
                DELETE FROM [dbo].[NAV App Installed App]
                WHERE Name = '$_'
                GO"
        }
    } -argumentList `
        $databaseParams.databaseServer,
        $databaseParams.databaseInstance, 
        $databaseParams.databaseCredential,
        $databaseName,
        $removeApps
}

Write-Host "Mount tenant $tenantId"
Invoke-ScriptInBcContainer `
    -containerName $containerName `
    -scriptBlock { 
    Param(
        $databaseServer,
        $databaseInstance,
        $databaseCredential,
        $tenantId,
        $databaseName)
    Mount-NavTenant `
        -ServerInstance $ServerInstance `
        -id $tenantId `
        -databaseserver $databaseServer `
        -databaseinstance $databaseInstance `
        -databasename $databaseName `
        -databaseCredentials $databaseCredential `
        -EnvironmentType Sandbox `
        -OverwriteTenantIdInDatabase `
        -Force
    Sync-NavTenant `
        -ServerInstance $ServerInstance `
        -Tenant $tenantId `
        -Mode ForceSync `
        -Force
} -argumentList `
    $databaseParams.databaseServer,
    $databaseParams.databaseInstance,
    $databaseParams.databaseCredential,
    $tenantId,
    $databaseName

Get-NavContainerAppInfo `
    -containername $containerName `
    -tenant $tenantId `
    -tenantSpecificProperties `
    -sort DependenciesFirst | ForEach-Object {
        $syncState = $_.SyncState.ToString()
        $appName = $_.Name
        if ($SyncState -eq "NotSynced") {
            Sync-NavContainerApp `
                -containerName $containerName `
                -tenant $tenantId `
                -appName $appName `
                -Force
        }
}

Get-NavContainerAppInfo `
    -containername $containerName `
    -tenant $tenantId `
    -tenantSpecificProperties `
    -sort DependenciesFirst | ForEach-Object {
        if ($_.NeedsUpgrade) {
            Start-BcContainerAppDataUpgrade `
                -containerName $containerName `
                -tenant $tenantId `
                -appName $_.Name
        }
}

Invoke-ScriptInBcContainer `
    -containerName $containerName `
    -scriptblock { Param( $tenantId )
        Start-NAVDataUpgrade `
            -ServerInstance $serverInstance `
            -Tenant $tenantId `
            -Force `
            -FunctionExecutionMode Serial `
            -SkipIfAlreadyUpgraded
        Get-NAVDataUpgrade `
            -ServerInstance $serverInstance `
            -Tenant $tenantId `
            -Progress
} -argumentList $tenantId

New-BcContainerBcUser `
    -containerName $containerName `
    -tenant $tenantId `
    -Credential $credential `
    -PermissionSetId 'SUPER' `
    -ChangePasswordAtNextLogOn:$false

Start-Process "http://$containerName/BC?tenant=$tenantId"

**Full output of scripts**

NavContainerHelper is version 0.6.5.7 NavContainerHelper is running as administrator Host is Microsoft Windows 10 Enterprise - 2004 Docker Client Version is 19.03.8 Docker Server Version is 19.03.8 ...


**Screenshots**
If applicable, add screenshots to help explain your problem.

**Additional context**
- does it happen all the time?
- did it use to work?
PRA-TRM commented 7 months ago

now getting this error : image

PRA-TRM commented 7 months ago

Have no clue what to give in this script : $databaseParams = @{

"databaseServer" = "localhost\MSSQLSERVER01"

"databaseServer" = "PRAPC-T15"
"databaseInstance" = "localhost\MSSQLSERVER01"
"databasePrefix" = "$($containerName)-"
"databaseName" = "SAT"
"databaseCredential" = $dbCredential
"multitenant" = $true
freddydk commented 7 months ago

Could you tell me what you are trying to do?

PRA-TRM commented 7 months ago

Hi Freddy, I have a bacpac file locally stored on my computer. wanted to execute the script so that the bacpac file is stored in SSMS and also create a connection with container created SATPROD in docker and get it running from on prem.

freddydk commented 7 months ago

And you do have SQL Server installed on your computer (the docker host)?

PRA-TRM commented 7 months ago

yes

PRA-TRM commented 7 months ago

I have a connection with SQl Server. But after executing the script, Getting error : image

the user name and password credentials are also correct in SSMS

PRA-TRM commented 7 months ago

the output is : C:\Users\pra\OneDrive - TRIMIT Development A S\Documents\PowerShell\FreddyScriptImportDatabase.ps1 Reading Key Vault Password Download Database Export BcContainerHelper is version 6.0.0 BcContainerHelper is running as administrator HyperV is Enabled UsePsSession is True Host is Microsoft Windows 10 Enterprise - 10.0.19045.3693 Docker Client Version is 20.10.13 Docker Server Version is 20.10.13 Removing Session satprod Removing container satprod Removing entries from hosts Removing satprod from container hosts file Removing satprod-* from container hosts file Removing Desktop shortcuts Removing C:\ProgramData\BcContainerHelper\Extensions\satprod Starting Database Restore job from https://bcartifacts.azureedge.net/sandbox/22.5.59966.61937/nl C:\Program Files\WindowsPowerShell\Modules\BcContainerHelper\6.0.0\Import-BcContainerHelper.ps1 Fetching all docker images Fetching all docker volumes INFO: Windows 10 21H1/21H2 images are not yet available, using 2004 as these are found to work better than 20H2 on 21H1/21H2 Using image mcr.microsoft.com/businesscentral:10.0.19041.1415 Creating Container satprod Style: sandbox Multitenant: Yes Version: 22.5.59966.61937 Platform: 22.0.61936.0 Generic Tag: 1.0.2.14 Container OS Version: 10.0.19041.1415 (2004) Host OS Version: 10.0.19045.3693 (22H2) WARNING: Host OS is Windows 10 21H1 or newer and Container OS is 2004, defaulting to process isolation. If you experience problems, add -isolatio n hyperv. Using process isolation Using locale nl-NL Disabling the standard eventlog dump to container log every 2 seconds (use -dumpEventLog to enable) Using license file C:\License\4804774_BC22_NL.bclicense Additional Parameters: --env customNavSettings=EnableTaskScheduler=True Files in C:\ProgramData\BcContainerHelper\Extensions\satprod\my:

at , : line 66Error Using artifactUrl https://bcartifacts.azureedge.net/sandbox/22.5.59966.61937/nl Using installer from C:\Run\210-new Installing Business Central Installing from artifacts Starting Local SQL Server Starting Internet Information Server Copying Service Tier Files c:\dl\sandbox\22.5.59966.61937\platform\ServiceTier\Program Files c:\dl\sandbox\22.5.59966.61937\platform\ServiceTier\System64Folder Copying PowerShell Scripts c:\dl\sandbox\22.5.59966.61937\platform\WindowsPowerShellScripts\Cloud\NAVAdministration c:\dl\sandbox\22.5.59966.61937\platform\WindowsPowerShellScripts\WebSearch Copying Web Client Files c:\dl\sandbox\22.5.59966.61937\platform\WebClient\Microsoft Dynamics NAV Copying ModernDev Files c:\dl\sandbox\22.5.59966.61937\platform c:\dl\sandbox\22.5.59966.61937\platform\ModernDev\program files\Microsoft Dynamics NAV Copying additional files Copying ConfigurationPackages C:\dl\sandbox\22.5.59966.61937\nl\ConfigurationPackages Copying Test Assemblies C:\dl\sandbox\22.5.59966.61937\platform\Test Assemblies Copying Extensions C:\dl\sandbox\22.5.59966.61937\nl\Extensions Copying Applications C:\dl\sandbox\22.5.59966.61937\platform\Applications Copying Applications.NL C:\dl\sandbox\22.5.59966.61937\nl\Applications.NL Copying dependencies Copying ReportBuilder Importing PowerShell Modules Skipping restore of Cronus database Modifying Business Central Service Tier Config File for Docker Creating Business Central Service Tier Installing SIP crypto provider: 'C:\Windows\System32\NavSip.dll' Installation took 32 seconds Installation complete Initializing... Setting host.docker.internal to 192.168.20.42 in container hosts file (copy from host hosts file) Setting gateway.docker.internal to 192.168.20.42 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.24.48.1 in container hosts file Starting Container Hostname is satprod PublicDnsName is satprod Using NavUserPassword Authentication Error restoring databases. Error was The network name cannot be found.

at , : line 66 New-BcContainer Telemetry Correlation Id: 504447db-d6d6-4d46-b3a3-98b6b4ea42e7 Initialization of container satprod failed At C:\Program Files\WindowsPowerShell\Modules\BcContainerHelper\6.0.0\ContainerHandling\Wait-NavContainerReady.ps1:42 char:17

freddydk commented 7 months ago

And the SQL Server has enabled TCP protocol?

PRA-TRM commented 7 months ago

it was not enabled but after enabling still getting the error : image

The TCP Communication to the Named instance is allowed to All Ports image

freddydk commented 7 months ago

It isn't in the firewall - it is the SQL Server Configuration module - TCP is not enabled by default

PRA-TRM commented 7 months ago

yes it is enable in SQL SERVER configuration module: image

freddydk commented 7 months ago

I think your database settings should be:

$databaseParams = @{ "databaseServer" = 'host.containerhelper.internal' "databaseInstance" = the SQL instance of the SQL Server "databasePrefix" = "$containerName-" "databaseName" = 'CRONUS' "databaseCredential" = Your credentials "multitenant" = $true or $false }

PRA-TRM commented 7 months ago

It is not working : Still getting errors after changing the script to: $databaseParams = @{ "databaseServer" = "host.containerhelper.internal" "databaseInstance" = "PRAPC-T15\MSSQLSERVER01" "databasePrefix" = "$($containerName)-" "databaseName" = "SAT" "databaseCredential" = $dbCredential "multitenant" = $true } image

freddydk commented 7 months ago

"PRAPC-T15\MSSQLSERVER01" is not a databaseInstance - that looks like a server name + an instance. If MSSQLSERVER01 is a database instance on the SQL server on the docker host, then MSSQLSERVER01 should be the value to use.

PRA-TRM commented 7 months ago

after changing it to : $databaseParams = @{ "databaseServer" = "host.containerhelper.internal" "databaseInstance" = "MSSQLSERVER01" "databasePrefix" = "$($containerName)-" "databaseName" = "SAT" "databaseCredential" = $dbCredential "multitenant" = $true } still not working :
image

freddydk commented 7 months ago

Haven't seen that before Is SSL required for the SQL Server?

PRA-TRM commented 7 months ago

I dont know , currently the SSL is disabled there is no certificate:

image