With a containerised version of SQL Server, you can run the database server anywhere you can install docker. To install SQL Server 2022 in a container, you need to create a docker-compose.yml file and once the file is ready you can deploy the SQL Server in a container by giving the command docker-compose up. Docker should be installed on the machine, where you run the command.

Setup and run MSSQL-2022 container

I use Visual Studio Code to create a docker-compose file but you can use any editor to create the file. There is already a Docker Desktop running on my computer.

Follow the following easy steps to create docker-compose.yml and deploy the SQL Server 2022 on a container:

Step 1: Open Visual Studio Code

Step 2: Create a file docker-compose.yml

Step 3: Write the following on the docker-compose.yml:

version: '3.4'
services:
  mssql:
    container_name: mssql-db
    hostname: mssql-db
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      ACCEPT_EULA: 'Y'
      MSSQL_SA_PASSWORD: 'Admin@123'
      MSSQL_DATA_DIR: /var/opt/mssql/data
      MSSQL_PID: 'Developer' 
      MSSQL_TCP_PORT: 1433 
    ports: 
      - "1455:1433"
    volumes:
      - ./data:/var/opt/mssql/data
      - ./log:/var/opt/mssql/log
      - ./secrets:/var/opt/mssql/secrets  

Step 4: Once the docker-compose.yml file is ready, open the terminal in the Visual Studio Code and remaining on the file directory give the command

docker-compose up -d

Step 5: After starting the container, SQL Server will start up. You can then connect to the SQL Server 2022.


How to connect to the SQL Server 2022?

Open SQL Server Management Studio (SSMS) on your computer and connect the database using the credentials which you set in the docker-compose.yml file.

Server name: localhost,1455

Authentication: SQL Server Authentication

Login: sa

Password: Admin@123

SSMS screenshot to connect to the SQL Server 2022

If you closely see the docker-compose.yml, you could see volumes. Basically, container data does not persist and when you remove the container, all the data is also lost. To keep the data persisted, you need to map the container volume in the computer. In the above, you are mapping the directory datalog, and secrets on the folder where the docker-compose.yml file is present.

Download Source Code

$ git clone https://github.com/favtuts/docker-compose-getting-started.git
$ cd mssql-2022

Leave a Reply

Your email address will not be published. Required fields are marked *