Setup MSSQL Server with Docker-Compose

Jan 03, 2024

This guide describes how to setup an instance of MSSQL Server for local development. Please make sure to check for additional best practices and security details, if you want to use this instance as a production server.

Preparing the environment

Create a new folder anywhere you like. Within it, create 4 new folders and change the owner to 1001 so that the container can write data to it.

mkdir mssql
cd mssql
mkdir sqlsystem
mkdir sqldata
mkdir sqllog
mkdir sqlbackup
chown 10001:10001 sqlsystem/
chown 10001:10001 sqldata/
chown 10001:10001 sqllog/
chown 10001:10001 sqlbackup/

docker-compose.yml

To setup MSSQL Server with Docker-Compose, use this docker-compose.yml file. Save this file in the previously created root-folder.

version: '3'
services:
  db:
    ports:
     - 1433:1433
    environment:
      ACCEPT_EULA: "Y"
      SA_PASSWORD: yourVeryStrongPassword
      MSSQL_DATA_DIR: /var/opt/sqlserver/data
      MSSQL_LOG_DIR: /var/opt/sqlserver/log
      MSSQL_BACKUP_DIR: /var/opt/sqlserver/backup
    volumes:
      - ./sqlsystem:/var/opt/mssql
      - ./sqldata:/var/opt/sqlserver/data
      - ./sqllog:/var/opt/sqlserver/log
      - ./sqlbackup:/var/opt/sqlserver/backup
    image: mcr.microsoft.com/mssql/server
    restart: always
    healthcheck:
        test: ["CMD-SHELL", "/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P example_123 -Q 'SELECT 1' || exit 1"]
        interval: 10s
        retries: 10
        start_period: 10s
        timeout: 3s

Starting/Stopping

Run the following bash commands to start or stop the container.

# Start the docker container ("-d" to run it as a daemon)
docker-compose up -d
# stop the docker container
docker-compose down