How to run Microsoft SQL Server database using docker and docker-compose

In this post I will show you how you can easily run Microsoft SQL Server Database using docker and docker-compose.

Microsoft provides official images in docker hub, so you can just pull and create container based on them.

Create docker-compose.yml

Firstly we will create the recipe for docker-compose. Let’s create YML file and put inside the below configuration. In this example we will take the latest tag of SQL Server 2019.

Configuration

Additionally we must set the password which we will use to connect to database, accept the EULA and provide information which Product ID you want to use. Options are:

  • Developer : This will run the container using the Developer Edition (this is the default if no MSSQL_PID environment variable is supplied)
  • Express : This will run the container using the Express Edition
  • Standard : This will run the container using the Standard Edition
  • Enterprise : This will run the container using the Enterprise Edition
  • EnterpriseCore : This will run the container using the Enterprise Edition Core : This will run the container with the edition that is associated with the PID (all the information is coming from official website on DockerHub)
version: "3"
services:
  sql-server:
    image: mcr.microsoft.com/mssql/server:2019-latest
    hostname: sql-server
    container_name: sql-server
    ports:
      - "1433:1433"
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=yourStrong(!)Password
      - MSSQL_PID=Express

Run container

Tu run container please just execute command below. Docker image will be downloaded and run.

docker-compose up

Pulling jenkins (mcr.microsoft.com/mssql/server:2019-latest)...
2019-latest: Pulling from mssql/server
5b7339215d1d: Already exists
14ca88e9f672: Already exists
a31c3b1caad4: Already exists
b054a26005b7: Already exists
59f979819d9b: Pull complete
29eb18117119: Pull complete
6de3c1cf897f: Pull complete
172ea0155639: Pull complete
Digest: sha256:e064843673f08f22192c044ffa6a594b0670a3eb3f9ff7568dd7a65a698fc4d6
Status: Downloaded newer image for mcr.microsoft.com/mssql/server:2019-latest

Connect to database

Now I will show you how you can connect to SQL Server Database. In my case I will use the DBeaver Community.

Let’s open DBeaver, choose “create new connection”, select SQL Server from the list and click Next.

In next window provide information like:

  • Host: localhost
  • Port: 1433
  • Database/Schema: master
  • Authentication: SQL Server Authentication
  • User name: sa
  • Password: yourStrong(!)Password (from docker-compose.yml file)

Test connection

Now we can test our connection using “Test Connection …” button in left bottom part of window. If everything was set correctly you should see information like below:

Server:
Microsoft SQL Server 15.00.4023
Microsoft SQL Server 2019 (RTM-CU3) (KB4538853) - 15.0.4023.6 (X64) 
	Mar  4 2020 00:59:26 
	Copyright (C) 2019 Microsoft Corporation
	Express Edition (64-bit) on Linux (Ubuntu 18.04.4 LTS) <X64>
Driver:
Microsoft JDBC Driver 8.2 for SQL Server 8.2.0.0

Run test SQL statement

At the and of this post we will execute simple SQL query to get some data from database. We will use existing table from master database.

SELECT * FROM master.dbo.spt_values;

If you enjoyed this post please add the comment below or share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments