NavContainerHelper – Create a SQL Server container and restore a .bak file

Update 2021/2/10: Microsoft stopped creating images for Docker in the summer of 2020. We now publish artifacts, which can be used to spin up containers and BcContainerHelper has replaced NavContainerHelper. This blog post reflects the old way of using NAV/BC on Docker and references NavContainerHelper, which is outdated.

If you haven’t read the first post about the NavContainerHelper, you should do so.

The following script sample, will create a new SQL Server container and restore a NAV 2018 database backup file (Demo Database NAV (11-0).bak) placed on the host in a folder called c:\temp\navdbfiles. The folder c:\temp\navdbfiles on the host is shared as c:\temp inside the container.

$hostFolder = "c:\temp\navdbfiles"
$databaseCredential = New-Object System.Management.Automation.PSCredential -argumentList "sa", (ConvertTo-SecureString -String "P@ssword1" -AsPlainText -Force)
$dbPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($databaseCredential.Password))
$dbserverid = docker run -d -e sa_password="$dbPassword" -e ACCEPT_EULA=Y -v "${hostFolder}:C:/temp"
$databaseServer = $dbserverid.SubString(0,12)
$databaseInstance = ""

$databaseName = "Demo Database NAV (11-0)"
$databaseServerInstance = @{ $true = "$databaseServer$databaseInstance"; $false = "$databaseServer"}["$databaseInstance" -ne ""]
$RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("${databaseName}_Data", "c:\temp\${databaseName}_Data.mdf")
$RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("${databaseName}_Log", "c:\temp\${databaseName}_Log.ldf")
Restore-SqlDatabase -ServerInstance $databaseServerInstance -Database $databaseName -BackupFile "C:\temp\$databaseName.bak" -Credential $databaseCredential -RelocateFile @($RelocateData,$RelocateLog)

After this, the SQL Server is ready to use from a NAV container and the variables $databaseServer, $databaseInstance, $databaseName and $databaseCredential are the parameters used to start up a NAV container to use this new database server.

Enjoy

Freddy Kristiansen
Technical Evangelist

8 thoughts on “NavContainerHelper – Create a SQL Server container and restore a .bak file

  1. Hello,
    I have the following error whe i run the complete sample described above:
    See ‘docker run –help’.
    Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
    Run a command in a new container

    I think it could be the following cmd :

    $dbserverid = docker run -d -e sa_password=”$dbPassword” -e ACCEPT_EULA=Y -v “${hostFolder}:C:/temp”

    Like

    • ContainerHelper is hardcoded to run on C:
      Docker can change to other drives – I haven’t tried this myself.
      in ContainerHelper – you can run Flush-ContainerHelperCache to remove all redundant cache information. Some things will then be slower next time around, but unused stuff will not be back.

      Like

      • Ok I tried. I set up data-root on a network drive.
        I run the new-navcontainer cmdlet and I got his error message ;
        DockerDo : failed to register layer: re-exec error: exit status 1: output: BackupWrite \\?\Z:\docker\windowsfilter\fd21893974d7389f66f94eabb458c0f140e8afdee07832173ca75d5873117c71\Files: The specified account
        does not exist.
        ExitCode: 1
        Commandline: docker pull microsoft/dynamics-nav:2018-cu6-fr
        Au caractère C:\Program Files\WindowsPowerShell\Modules\navcontainerhelper\0.6.5.0\ContainerHandling\New-NavContainer.ps1:442 : 13
        + DockerDo -command pull -imageName $imageName | Out-Null
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
        + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,DockerDo

        Did you see what is exactly this error ?Credentials problem ?

        Like

  2. Hello,

    I tried to run the script and I have the following error :
    docker : “docker run” requires at least 1 argument.
    Au caractère Ligne:5 : 15
    + … bserverid = docker run -d -e sa_password=”$dbPassword” -e ACCEPT_EULA …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (“docker run” re…ast 1 argument.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    See ‘docker run –help’.
    Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
    Run a command in a new container
    Impossible d’appeler une méthode dans une expression Null.
    Au caractère Ligne:6 : 1
    + $databaseServer = $dbserverid.SubString(0,12)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation : (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    I think it is related to the docker run with parameters -d. It should be filled : what i supposed to enter for this command ?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s