Need to work with
Microsoft SQL Server (MSSQL) but don’t want the hassle of installing it directly on your system? Docker offers a quick and clean way to get MSSQL up and running without affecting your main operating system.
In this guide, you’ll learn how to install
MSSQL using Docker, run it with the right configurations, and connect to it using tools like SQL Server Management Studio (SSMS) or any other database client.
Let’s get started!
Why Use Docker for MSSQL?
Using Docker for MSSQL gives you:
- Easy setup and teardown
- No permanent changes to your operating system
- The ability to run multiple instances or versions side by side
- A consistent environment across development machines
Prerequisites
Make sure you have
Docker installed before continuing.
For Windows/macOS:
Download Docker Desktop from:
https://www.docker.com/products/docker-desktop
For Ubuntu/Linux:
sudo apt update && sudo apt install docker.io
Check Docker version:
docker --version
Step 1: Pull the MSSQL Docker Image
Microsoft provides an official image for SQL Server on Linux, which works great in Docker.
To pull the image, run:
docker pull mcr.microsoft.com/mssql/server:2022-latest
Replace 2022-latest
with a different version if needed (e.g., 2019-latest
).
Step 2: Run the MSSQL Container
Start a new container with this command:
docker run -e "ACCEPT_EULA=Y" \
-e "SA_PASSWORD=YourStrong!Passw0rd" \
-p 1433:1433 \
--name mssql-container \
-d mcr.microsoft.com/mssql/server:2022-latest
What each part means:
-e "ACCEPT_EULA=Y"
→ Accepts Microsoft’s license agreement
-e "SA_PASSWORD=..."
→ Sets the sa
(system admin) password (must be strong!)
-p 1433:1433
→ Exposes the default MSSQL port
--name mssql-container
→ Names the container for easy reference
-d
→ Runs in detached (background) mode
Step 3: Persist Data with Docker Volumes (Optional but Recommended)
If you want to keep your data even after restarting or removing the container, mount a volume:
docker run -e "ACCEPT_EULA=Y" \
-e "SA_PASSWORD=YourStrong!Passw0rd" \
-p 1433:1433 \
--name mssql-container \
-v mssql-data:/var/opt/mssql \
-d mcr.microsoft.com/mssql/server:2022-latest
This uses a Docker volume called
mssql-data
to store your database files.
Step 4: Check That MSSQL Is Running
Run this command to see your container:
docker ps
If successful, you’ll see your
mssql-container
running and listening on port
1433
.
Step 5: Connect to SQL Server
Option 1: Use SQL Server Management Studio (SSMS)
- Server name:
localhost,1433
- Authentication: SQL Server Authentication
- Login:
sa
- Password:
YourStrong!Passw0rd
- Replace with the values you used during container setup.
Option 2: Use Azure Data Studio or DBeaver
These tools also support MSSQL. Use the same connection info as above.
Option 3: Use sqlcmd
(if installed)
sqlcmd -S localhost -U sa -P YourStrong!Passw0rd
Now you're connected to the database shell!
Step 6: Manage the Container
Here are some handy Docker commands:
Action |
Command |
Stop the container |
docker stop mssql-container |
Start the container again |
docker start mssql-container |
Remove the container |
docker rm -f mssql-container |
View logs |
docker logs mssql-container |
Step 7: Clean Up (If Needed)
To remove everything:
docker stop mssql-container && docker rm mssql-container && docker volume rm mssql-data
This deletes the container and any persistent data (only do this if you're sure!).
Docker Compose Example
If you prefer using Docker Compose, here’s a
docker-compose.yml
:
version: '3.8'
services:
mssql:
image: mcr.microsoft.com/mssql/server:2022-latest
container_name: mssql-container
environment:
ACCEPT_EULA: Y
SA_PASSWORD: YourStrong!Passw0rd
ports:
- "1433:1433"
volumes:
- mssql-data:/var/opt/mssql
volumes:
mssql-data:
Run it with:
docker-compose up -d
Best Practices
- Always use a strong password for the
sa
account.
- Use volumes to persist your database data.
- Don’t expose port
1433
to the public in production environments.
- Use Docker Compose for multi-service setups or easier management.
image quote pre code