How to Migrate SQL Server Databases Using Docker
Running a SQL Server database with Docker is a convenient way to manage a local database. Upgrading versions of a SQL Server docker container doesn't mean you need to lose all your data. I was recently tasked with guiding my team in safely upgrading a version of SQL Server running locally in a docker container while preserving their data. I managed to figure it out by connecting to the SQL Server instance using SQL Server Management Studio to generate backup files inside the container. Then, after copying the backup files to the host machine, I started a new container and restored the backups on the new version of SQL Server. There's probably a more elegant way to accomplish this, but I'm not willing to learn more about SQL Server to find out.
Prerequisites
- SQL Server database in a Docker container with some data you don't want to lose
- Another SQL Server database in another Docker container
- SQL Server Management Studio (SSMS)
- Enough hard drive space to store three copies, at most, of your data
Backup
- Start the Docker container you want to backup.
- Open SSMS and connect to the running container.
- In the Object Explorer pane, open the Databases section.
- Right-click on the database you want to backup and select Tasks > Backup.
- Save the backup to
/var/opt/mssql/data/DatabaseName.bakon the Docker container's volume.- Using the default settings is fine.
- Find the running Docker container ID with
docker ps.- Make a note of the first 5 or 6 characters (e.g.,
7aa8fead43af).
- Make a note of the first 5 or 6 characters (e.g.,
- Run
docker cpto copy the backup file to the host system.docker cp 7aa8f:/var/opt/mssql/data/DatabaseName.bak /path/on/host/
Repeat steps 4-7 for each database.
You should now have a collection of .bak files containing backups of all your data! We'll use these to restore your data to a new SQL Server instance.
Restore
- Stop any running SQL Server containers.
- Remove the old Docker volume with
docker volume rm volumename.- DANGER: You cannot undo this. Do not proceed until you have backed up all your data to your host system.
- Set permissions on the .bak files to allow all users access.
sudo chmod 777 DatabaseName.bak
- Start the new Docker container.
- Find the running Docker container ID with
docker ps. Note the first 5-6 characters. - Copy each .bak file to the new Docker volume.
- You must do this one at a time; wildcards will not work.
- The
-aflag preserves the file permissions we set in the previous step. docker cp -a /path/to/DatabaseName.bak 3ade8:/var/opt/mssql/data/
- Open SSMS and connect to your new SQL Server instance.
- Right-click on Databases and select Restore Database....
- Under the Source section, select Device and click the ellipsis button.
- Click Add and choose one .bak file under
/var/opt/mssql/data/. - The Restore Plan menu should now appear. The default settings are fine.
- Click OK to start the restore process.
Repeat steps 6-12 for each database file.
- Remove the old .bak files from your new container's volume to save space.
docker exec -it container-name bashrm -rf /var/opt/mssql/data/DatabaseName.bak
You should now have a new Docker container with the same data that existed in your old Docker container! If you run out of disk space, you can delete the .bak files after each restore instead of waiting until the last step.