Skip to content

Running SQL Server in Docker

Published: at 02:18 AM

Overview

This guide will walk you through the steps to run SQL Server in a Docker container. You will learn how to set up the environment, configure the server, and perform backup and restore operations.

Prerequisites

Setting Up SQL Server in Docker

First, pull the latest SQL Server Docker image:

docker pull mcr.microsoft.com/mssql/server:2019-latest

Next, run the SQL Server container with the following command:

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<password>' --shm-size 1g -p 1434:1434 -v <host directory>/backups:/var/opt/mssql/backups -v <host directory>/data:/var/opt/mssql/data -v <host directory>/log:/var/opt/mssql/log -v <host directory>/secrets:/var/opt/mssql/secrets --name sql19 --hostname sql19 -e 'TZ=America/Los_Angeles' -d mcr.microsoft.com/mssql/server:2019-latest

Replace <password> with a strong password and <host directory> with the appropriate directories on your host machine.

Enabling Container Shared Memory

To enable container shared memory, add the following line to your configuration:

[memory]
enablecontainersharedmemory = true

Backup and Restore Operations

Backup

To back up a database, use the following command:

sqlcmd -S localhost -U sa -Q "BACKUP DATABASE [demodb] TO DISK = N'/var/opt/mssql/data/demodb.bak' WITH NOFORMAT, NOINIT, NAME = 'demodb-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10"

Restore

To restore a database, use the following command:

sqlcmd -S localhost -U sa -Q "RESTORE DATABASE [demodb] FROM DISK = N'/var/opt/mssql/data/demodb.bak' WITH FILE = 1, NOUNLOAD, REPLACE, NORECOVERY, STATS = 5"

Additional References

For more details, visit the Microsoft documentation.


Previous Post
Calculating File Hashes with PowerShell and Python
Next Post
Understanding GSM vs Non-GSM Characters