Running MSSQL with Docker Compose

This guide will walk you through setting up and running a Microsoft SQL Server instance using Docker Compose.


๐Ÿ“ฆ Prerequisites

Before you begin, make sure you have:

Docker Desktop installed and running

Your project already containing a docker-compose.yaml file

version: '3.8'

services:
  sqlserver:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: sqlserver
    environment:
      ACCEPT_EULA: "Y"
      MSSQL_SA_PASSWORD: "YOUR_PASSWORD"
    ports:
      - "1433:1433"
    volumes:
      - sqlserverdata:/var/opt/mssql

volumes:
  sqlserverdata:

๐Ÿš€ Step 1: Open Your Project

Open your project in your preferred code editor (e.g. Visual Studio Code).

Locate the docker-compose.yml file in the root directory.


โ–ถ๏ธ Step 2: Start the Containers

Open a terminal inside your project folder

Run:

docker-compose up -d

Wait until Docker finishes pulling the image and starts the container


๐Ÿ” Step 3: Verify Container is Running

To confirm everything is working:

docker ps

You should see your MSSQL container listed with status Up.

Also copy CONTAINER_ID.


๐Ÿ“ฆ Step 4: Transfer .bak File into the Container

First, create a backup directory inside the running container:

docker exec -it <CONTAINER_ID> mkdir -p /var/opt/mssql/backup

Then copy your .bak file from the host machine into the container:

docker cp <database_name>.bak <CONTAINER_ID>:/var/opt/mssql/backup/<database_name>.bak

๐Ÿ’ก Tip: You can get the container ID using docker ps


๐Ÿงช Step 5: Connect to the Database

You can connect using:

  • Azure Data Studio
  • SQL Server Management Studio
  • Or any SQL client

Use these connection details:

  • Server: localhost
  • Port: 1433
  • User: sa
  • Password: (the one defined in your compose file)

SQL Server Management Studio Connection String:

Data Source=localhost,1433;Persist Security Info=True;User ID=sa;Password=YOUR_PASSWORD;Pooling=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Application Name="SQL Server Management Studio";Command Timeout=0

๐Ÿ› ๏ธ Step 6: Restore the Database

  1. Open SQL Server Management Studio (SSMS)

  2. Right-click on Databases

  3. Select Restore Database...

  4. Choose:

    • Source โ†’ Device

    • Click ... and add your .bak file from:

      /var/opt/mssql/backup/<database_name>.bak
  5. Confirm and start the restore process

โš ๏ธ If the backup does not appear, ensure the path is correctly set and the container has access to the file.


โœ… You're Done

You now have a running MSSQL instance inside Docker, ready for development and testing.