#Sitecore #Docker Containers Syncing Database Changes the Lazy Way

I have been using Docker/Containers for about six months. I have to tell you I really like using them. A recent project I am helping architect the solution for I decided to have the other developers use them. There were some hiccups along the way getting some developers setup, but so far it has worked out well. I think I convinced them change is good.

Ted Lasso Memes At Your Service!

Anyway one of the things I have learned is how easy it is to deploy things to containers. One of them being databases. Normally you would install Sitecore and serialize items or use Sitecore packages for changes. We are doing that. However we had multiple sites/tenants to create in SXA and wanted to make sure the team was in sync from the start. With the out of the box Sitecore tools for Docker it is easy to do.

If you look into your docker\data\mssql folder (could be a different folder depending how you setup your volume, but in this case I am using the default) you will find the ldf and mdf files for each Sitecore database. Just copy the ones for the database you want to share and put it in a shared folder for other developers. I actually stored them in Teams.

The developer who is installing the database should do a docker-compose down first. Then copy the database files to their local data\mssql folder. Once they are a copied to the other developer’s local data\mssql folder they will need to do a docker-compose up. Now they will have the same database changes as the other developer for their containerized Sitecore instance. BTW inside the mssql image you can view the database files.

So what happened? Was it magic? Well if you think PowerShell is magic then it was. As with custom web changes databases are also deployed into the images using the built in Sitecore tools. So as soon as the files are copied the watcher does its thing.

So one catch with this that I noticed recently. If you clean out your Docker Data\Deploy folders by running something like the clean.ps1 script it clears out the data\mssql folder. Which means the database changes could be lost. I will look for a workaround for this, but one thing that I did was create a clean script that keeps the database folder contents. See below for an example:

# Clean data folders
Get-ChildItem -Path (Join-Path $PSScriptRoot "\docker\data") -Directory | ForEach-Object {
    $dataPath = $_.FullName

    Get-ChildItem -Path $dataPath -Exclude ".gitkeep", "license.xml", "*.ldf", "*.mdf" -Recurse | Remove-Item -Force -Recurse -Verbose
}

# Clean deploy folders
Get-ChildItem -Path (Join-Path $PSScriptRoot "\docker\deploy") -Directory | ForEach-Object {
    $deployPath = $_.FullName

    Get-ChildItem -Path $deployPath -Exclude ".gitkeep", "license.xml", "*.ldf", "*.mdf" -Recurse | Remove-Item -Force -Recurse -Verbose
}

I will update this blog post I am sure as things evolve with Docker/Containers, but I hope for now this will give one way to share database changes. If you have a better way I would love to know.