Categories
Docker Dynamics NAV HOW TO Microsoft

HOW TO Refresh NAV database in NAV Container without the need to Recreate the container

This post actually started with an Issue I created in NavContainerHelper GitHub.

💡 I’m working in a script for Continuous Integration and although the creation of a container requires a few minutes I was looking at how to optimize the time of execution of the whole build process.

Freddy Kristiansen is a Microsoft Evangelist that is working hard to make it easier to work with NAV using Docker Containers, thanks again, Freddy.

Freddy initially marked my issue with label “WONTFIX” 🙁 but after I explained to him my motivation he changed the label to “ENHANCEMENT” 😀

He did not include any new functionality to NavContainerHelper but he kindly provided me with the script to copy and paste, and as I always do, I like to share good things.

Solution:
Right after the creation of a NAV Container, run the following script:

# To add just after the creation of the NAV Container
$containerName = "scadev"
$config = Get-NavContainerServerConfiguration -ContainerName $containerName
Invoke-ScriptInNavContainer -containerName $containerName -scriptblock { 
  Param($DatabaseServer, $DatabaseInstance, $DatabaseName, $NewDatabaseName)
    Copy-navDatabase -DatabaseServer $DatabaseServer -DatabaseInstance 
  $DatabaseInstance -SourceDatabaseName $DatabaseName -DestinationDatabaseName 
  $NewDatabaseName
} -argumentList $config.DatabaseServer, $config.DatabaseInstance, $config.DatabaseName, "backup"

So next time you run the build process, just verify that the Container exists and in case replace the database with the backup taken previously (it takes 10-20 seconds)

$containerName = "scadev"
if (Test-NavContainer $containerName) {
  $config = Get-NavContainerServerConfiguration -ContainerName $containerName
  Invoke-ScriptInNavContainer -containerName $containerName -scriptblock { 
    Param($DatabaseServer, $DatabaseInstance, $DatabaseName, $NewDatabaseName)
    Copy-navDatabase -DatabaseServer $DatabaseServer -DatabaseInstance 
    $DatabaseInstance -SourceDatabaseName $DatabaseName -DestinationDatabaseName 
    $NewDatabaseName
  } -argumentList $config.DatabaseServer, $config.DatabaseInstance, "backup", $config.DatabaseName
} else {
# Creation of NAV Container
}

Source: https://github.com/Microsoft/navcontainerhelper/issues/315

Update 08/09/2019: With the release of ContainerHelper 0.6.4.1 the logic described in this post has been integrated as a new feature as described in Freddy’s post section “Speed up repetitive container generation
https://freddysblog.com/2019/09/08/containerhelper-0-6-4-1/

Did my HOW TO help you? Leave a reply.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.