Docker Containers: Creating a SQL Server Demo Environment

Running SQL Server in a Docker Container

In order to run our SQL Server container we have to execute the following code in PowerShell:

docker run `
--name SQLserver17 `
-p 1433:1433 `
-e "ACCEPT_EULA=Y" `
-e "SA_PASSWORD=<SuperStrongPassword>" `
-d mcr.microsoft.com/mssql/server:2017-latest

 Note

The password should follow the SQL Server default password policy, otherwise the container can not setup SQL server and will stop working. By default, the password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols. You can examine the error log by executing the docker logs command.

The following table provides a description of the parameters in the previous docker run example:

ParameterDescription
-e ‘ACCEPT_EULA=Y’Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image.
-e ‘SA_PASSWORD=<YourStrong!Passw0rd>’Specify your own strong password that is at least 8 characters and meets the SQL Server password requirements. Required setting for the SQL Server image.
-p 1433:1433Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, SQL Server is listening on TCP 1433 in the container and this is exposed to the port, 1433, on the host.
–name SQLserver17Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you cannot reuse this same name.
mcr.microsoft.com/mssql/server:2017-latestThe SQL Server 2017 CTP3.0 Linux container image.

To view your Docker containers, use the docker ps command.

docker ps -a

Theps stands for Process Status, and -a will show all processes regardless of status. If the container is not running, you have to (re)start it:

docker start SQLserver17

You can now connect from SQL Server Management Studio (SSMS) or Azure Data Studio to your container as SA using your <SuperStrongPassword>:

If you specified a different port when running the container, you will have to connect using the port number, for example localhost,1401.

If you do not know your IP address, you can either use PowerShell or open a Windows Command Prompt (Run>CMD) and use the command ipconfig to look up the IP address of the Docker container:

Once connected, SSMS will present you the standard SSMS gui.

Common tools to connect to SQL Server include:

Useful Docker Commands

Create a container:

docker run CONTAINER --network NETWORK

Start a stopped container:

docker start CONTAINER NAME

Stop a running container:

docker stop

List all running containers

docker ps

List all containers including stopped ones

docker ps -a

Inspect the container configuration. For instance network settings and so on:

docker inspect CONTAINER

List all available virtual networks:

docker network ls

Create a new network:

docker network create NETWORK --driver bridge

Connect a running container to a network

docker network connect NETWORK CONTAINER

Disconnect a running container from a network

docker network disconnect NETWORK CONTAINER

Remove a network

docker network rm NETWORK

Leave a Reply

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