SQL Server 2017 in a Container – Part 3

In Part 2 I showed how to pull down the latest SQL 2017 image.

In this blog I will show how we can create a container.

Creating a Container

There are two ways to create a container:

  • docker create
  • docker run

docker create is used for creating the container in a stopped state. You then need to run docker start in order to make it available.

docker run is the same as running docker create followed by docker start.

Before we create a container lets list what containers we already have, if any.

docker container ls –all

The –all switch will show all containers that are running or stopped. Without it, you will only see running containers. You can also use the docker ps and docker ps –all commands.

The output above shows one container with an ID bb25a0904fa4. These IDs are unwieldly so it is a good idea to name your container. Look to the far right. In this example the container is called SQL2017.

Let’s create the simplest possible container. The password has to be strong or we get a silent failure and will not be able to connect.

docker create –name Demo -p 1433:1433 -e sa_password=Password1#### -e ACCEPT_EULA=Y microsoft/mssql-server-windows-developer:1703

Let’s start the container. We can use the name Demo instead of the ID which is easier.

docker start Demo

Let’s check the running state

docker ps

Status says it’s healthy. Lastly we can view the container logs.

docker logs Demo

So now we have a running container, we need to connect to it.

Executing a command inside a Container

You cannot run a UI application inside a container. Our image is based on Windows Server Core which does not allow it.

We can launch a command prompt to run inside our container as follows. –it is interactive.

docker exec –it Demo cmd

Noticed my command prompt changed because I am running it within the container. I run hostname and whoami to verify that I am connected.

The hostname is the same as the Container ID shown with docker ps. When Microsoft creates the base Windows Server Core image they add the default user account containeradministrator. Any process you start in a container will use that user account.

I switched to C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL. This is where SQL Server is installed.

We can view the errorlog from the Log folder.

We can also launch powershell as well

docker exec –it Demo powershell

Lastly we can run sqlcmd from our powershell or cmd session.

All this is inside the container. In the next blog I am going to you how to setup the network side in order to connect to the container from the container host and a Hyper-v VM running on the container host.