Introduction
When a container is removed, its writable container filesystem is removed as well. That behavior is useful for temporary application files, but it can cause serious data loss when database files, user records, or generated application data are stored there. If SQL Server writes its files only to the container layer, running docker rm can remove the databases along with the container.
In this lesson, we will store SQL Server data in a Docker-managed named volume. We will then compare that approach with a bind mount. You will stop and remove the database container, recreate it with the same storage, verify that the data is still available, and create a backup archive of the volume. Finally, we will review how to remove unused volumes safely.
The examples use the SQL Server 2022 Linux container image. Commands are written for a Bash-compatible terminal. If you use Windows PowerShell, adapt line-continuation characters and host paths to your environment.
1. The container filesystem and persistent storage
A Docker image is made of read-only layers. When a container starts, Docker adds a writable layer above those image layers. Applications can write to that layer, but the data disappears when the container is removed. A volume is different because its lifecycle is independent of the container that uses it.
We will use two storage approaches:
- Named volume: A storage area created and managed by Docker. Its physical location on the host is abstracted from the application.
- Bind mount: A specific directory on the host is mounted into a directory inside the container. The files can be accessed directly from the host.
| Characteristic | Named volume | Bind mount |
|---|---|---|
| Management | Managed by Docker | Managed through a host directory |
| Portability | Usually more portable | Depends on the host path structure |
| Host inspection | Direct access is not recommended | Files are directly visible on the host |
| Typical use | Database and service data | Source code, configuration, and development files |
For SQL Server database files, a named volume is usually the cleaner default for development and test scenarios. A bind mount is useful when you need to inspect files on the host, integrate with a host-based backup system, or place data on a specific disk or filesystem.
2. Creating a named volume
Start by creating a volume with a meaningful project-oriented name:
docker volume create sqlserver-data
List Docker volumes to verify that it exists:
docker volume ls
To inspect its metadata, run:
docker volume inspect sqlserver-data
The Mountpoint value shows where Docker stores the volume on the Docker host. Avoid writing directly to that path. Accessing the data through a container is safer, and with Docker Desktop the path may exist inside a Linux virtual machine rather than directly on the native host.
3. Running SQL Server with a named volume
Now mount the sqlserver-data volume at the SQL Server data root. SQL Server Linux containers use /var/opt/mssql as the primary directory for database files and related runtime data.
docker run -d \ --name sqlserver-volume-demo \ --hostname sqlserver-volume-demo \ -e ACCEPT_EULA=Y \ -e MSSQL_PID=Developer \ -e MSSQL_SA_PASSWORD='Str0ngPassword!2026' \ -p 1433:1433 \ --mount type=volume,source=sqlserver-data,target=/var/opt/mssql \ mcr.microsoft.com/mssql/server:2022-latest
The --mount form is explicit and easy to read. The shorter syntax performs the same mount:
docker run -d \ --name sqlserver-volume-demo \ -e ACCEPT_EULA=Y \ -e MSSQL_PID=Developer \ -e MSSQL_SA_PASSWORD='Str0ngPassword!2026' \ -p 1433:1433 \ -v sqlserver-data:/var/opt/mssql \ mcr.microsoft.com/mssql/server:2022-latest
Do not hard-code passwords in real projects. Use the environment-variable, secret, or secure local configuration techniques discussed in the previous lesson. The password in this example is included only for a self-contained demonstration.
Check that the container is running and inspect its logs:
docker ps docker logs sqlserver-volume-demo
You may need to wait several seconds before SQL Server is ready to accept connections. A running container does not always mean that the database engine has completed startup.
4. Creating a test database and record
To prove that data survives container recreation, create a small database and table. The location of sqlcmd can vary between SQL Server images, so use SQL Server Management Studio, Azure Data Studio, or a locally installed sqlcmd client for this step.
If sqlcmd is installed on your host, connect with:
sqlcmd -S localhost,1433 -U sa -P 'Str0ngPassword!2026' -C
After connecting, execute these statements:
CREATE DATABASE DockerPersistenceDb; GO USE DockerPersistenceDb; GO CREATE TABLE Notes ( Id INT IDENTITY(1,1) PRIMARY KEY, Text NVARCHAR(200) NOT NULL, CreatedAt DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME() ); GO INSERT INTO Notes (Text) VALUES (N'Volume persistence verified'); GO SELECT Id, Text, CreatedAt FROM Notes; GO
The database files are now stored in the named volume. Restarting the container would already preserve the data, but we will perform a stronger test by deleting the entire container.
5. Removing and recreating the container
Stop and remove the container:
docker stop sqlserver-volume-demo docker rm sqlserver-volume-demo
This removes the container but does not remove the named volume called sqlserver-data. Confirm that the volume still exists:
docker volume ls
Start SQL Server again with the same volume attached:
docker run -d \ --name sqlserver-volume-demo-2 \ --hostname sqlserver-volume-demo-2 \ -e ACCEPT_EULA=Y \ -e MSSQL_PID=Developer \ -e MSSQL_SA_PASSWORD='Str0ngPassword!2026' \ -p 1433:1433 \ --mount type=volume,source=sqlserver-data,target=/var/opt/mssql \ mcr.microsoft.com/mssql/server:2022-latest
After SQL Server is ready, connect with the same credentials and query the database:
USE DockerPersistenceDb; GO SELECT Id, Text, CreatedAt FROM Notes; GO
If you can see the Volume persistence verified row, the data survived the container lifecycle. This demonstrates the central purpose of a volume: the container can be replaced while the data remains in a separate storage layer.
6. Comparing a bind mount
If you want to use a directory on the host instead of a named volume, create a bind mount. On Linux or macOS, create the following directories from the project folder:
mkdir -p ./docker-data/sqlserver/data mkdir -p ./docker-data/sqlserver/log mkdir -p ./docker-data/sqlserver/secrets
Start another SQL Server container and map the SQL Server directories separately:
docker run -d \ --name sqlserver-bind-demo \ -e ACCEPT_EULA=Y \ -e MSSQL_PID=Developer \ -e MSSQL_SA_PASSWORD='Str0ngPassword!2026' \ -p 1434:1433 \ --mount type=bind,source="$(pwd)/docker-data/sqlserver/data",target=/var/opt/mssql/data \ --mount type=bind,source="$(pwd)/docker-data/sqlserver/log",target=/var/opt/mssql/log \ --mount type=bind,source="$(pwd)/docker-data/sqlserver/secrets",target=/var/opt/mssql/secrets \ mcr.microsoft.com/mssql/server:2022-latest
Permissions on the host directories matter when using bind mounts. If the SQL Server container runs as a non-root user, it may not have permission to write to the mounted paths. Inspect the logs:
docker logs sqlserver-bind-demo
You can also verify the mount definitions with:
docker inspect sqlserver-bind-demo
The main benefit of a bind mount is that files are visible on the host. The trade-off is that the container becomes dependent on a particular host path and permission model. This can be convenient during development, but deleting or replacing the host directory accidentally can cause data loss.
7. Backing up a volume
A volume archive is only one part of a database backup strategy. In production, also use SQL Server's native BACKUP DATABASE mechanism. When creating a file-level backup of a volume, stop SQL Server first so that the copied files are in a consistent state.
Stop the demonstration container and create a local backup directory:
docker stop sqlserver-volume-demo-2 mkdir -p ./backups
Use a temporary Alpine container to archive the volume contents:
docker run --rm \ --volumes-from sqlserver-volume-demo-2 \ --mount type=bind,source="$(pwd)/backups",target=/backup \ alpine \ tar czf /backup/sqlserver-data-$(date +%Y%m%d-%H%M%S).tgz -C /var/opt/mssql .
The --volumes-from option gives the temporary container the volume mounts of the existing container. The archive is written to the host's backups directory. Inspect the archive with:
tar tzf ./backups/sqlserver-data-*.tgz | head
Copy the backup to a separate protected storage location and perform restoration tests regularly. The existence of an archive alone does not prove that the backup is usable.
8. Safe cleanup and common mistakes
List existing volumes with:
docker volume ls
Before deleting a particular volume, check which containers reference it:
docker ps -a --filter volume=sqlserver-data
Remove the containers that use the volume, or migrate them to another volume, before deleting the volume itself:
docker rm -f sqlserver-volume-demo-2 docker volume rm sqlserver-data
The following command can remove all unused volumes:
docker volume prune
Do not run this command without reviewing the confirmation prompt on a production or shared development machine. Common mistakes include:
- Starting the container without a volume and assuming the database is persistent.
- Failing to check which volumes a container-removal or Compose cleanup command will delete.
- Copying active SQL Server files at an inconsistent moment and treating the copy as a reliable backup.
- Forgetting to grant the required permissions to bind-mount directories.
- Treating a named volume and a host directory as the same thing; Docker manages the location of a named volume.
- Deleting a volume without checking docker volume inspect and the list of attached containers.
Short practice exercise
- Create a new named volume called inventory-data.
- Run a SQL Server 2022 container with this volume mounted at /var/opt/mssql.
- Create a database called InventoryDb and at least one table.
- Stop and remove the container, then create a new container using the same volume.
- Verify that the table row is still present.
- Stop SQL Server and create a tar archive of the volume.
- Before deleting the volume, explain which containers reference it and why that check is necessary.
Conclusion
This lesson demonstrated the difference between a temporary container filesystem and persistent storage. By using a named volume, we separated SQL Server data from the container lifecycle and verified that the database survived container deletion and recreation. We also saw that bind mounts provide direct access to host files, but increase responsibility for host paths, permissions, portability, and accidental modification.
When creating volume backups, stop SQL Server first and validate backups through restoration tests. In the next stage, we will move this configuration into Docker Compose so that the API, SQL Server, and storage definitions can be managed together from one project file.