How do I run SQL Server locally with Docker and keep my data when I recreate the container?

April 16, 2025

The command below starts SQL Server 2022 Developer Edition by default.1 You can use it free of charge for development, testing, and demonstrations. Its license excludes production workloads.2

Before running the command in Bash, replace <填入你的密码> with your own sa administrator password and keep the single quotes. The password must be at least eight characters long and include at least three of the following four types of character: uppercase letters, lowercase letters, numbers, and symbols.

docker run -d --name sql-dev \
  -e ACCEPT_EULA=Y \
  -e 'MSSQL_SA_PASSWORD=<填入你的密码>' \
  -p 127.0.0.1:1433:1433 \
  -v sql-dev-data:/var/opt/mssql \
  mcr.microsoft.com/mssql/server:2022-latest
  • Connecting. In your local SQL client, enter 127.0.0.1,1433 as the server and sign in as sa with the password above. If port 1433 is already in use on your machine, change the -p value to 127.0.0.1:14330:1433 and connect to 127.0.0.1,14330.
  • The data volume. Docker creates a volume named sql-dev-data and mounts it at /var/opt/mssql inside the container. If you remove the container, mount the same volume when you recreate it to keep using your existing databases.3

Footnotes

  1. Microsoft. (2026, June 11). Quickstart: Run SQL Server Linux container images with Docker. Microsoft Learn. Run the container. ↩

  2. Microsoft. (2022). SQL Server 2022 licensing datasheet. Microsoft. ↩

  3. Microsoft. (2026, July 14). Configure and customize SQL Server Docker containers. Microsoft Learn. Use a named data volume. ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-04-16). How do I run SQL Server locally with Docker and keep my data when I recreate the container?. https://caiborui.com/blog/2025-04-16-zh-sql-server-docker-local-dev-entrans

@misc{borui2025-6ee1b571,
  author = {borui},
  title = {{How do I run SQL Server locally with Docker and keep my data when I recreate the container?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-04-16-zh-sql-server-docker-local-dev-entrans}
}