Container (3), Installing SQL Server Into Docker

This series of articles is about Docker or Container, so I cannot get a suitable category. I designate it to DevOps because the Container is used for CI/CD (Continue Integration and Continue Deployment).

A - Introduction

Contents of this article:

  • A - Introduction
  • B - Overview
    • What is Container
    • What is Container Image
  • C - Find the Image from Docker Hub
  • D - Pull Microsoft SQL Server Image into Docker
  • E - Start Docker Container for the SQL Server Image
  • F - Open Container SQL Server from SSMS

B - Overview

  • This article will discuss pulling and running an image as a container.

What is a container?

Simply put, a container is a sandboxed process on your machine isolated from all other processes on the host machine. 

  • It is a runnable instance of an image, that can be managed by either DockerAPI (Docker Desktop) or CLI: such as
    • create,
    • start,
    • stop,
    • move, or
    • delete
  • It is runnable on
    • local machines,
    • virtual machines or
    • deployed to the cloud.
  • It is portable (can be run on any OS).
  • It is isolated from other containers and runs its software, binaries, and configurations.

What is a container image?

When running a container, it uses an isolated filesystem. This custom filesystem is provided by a container image. Since the image contains the Container’s filesystem, it must contain everything needed to run an application - all dependencies, configurations, scripts, binaries, etc. The image also contains other configurations for the Container, such as environment variables, a default command to run, and other metadata.

C - Find the Image from Docker Hub

Type in docker.com, and log in, you will redirect to hub.docker.com/, or you can type in hub.docker.com/ directly. From Docker Hub, type SQL Server in the search box, and search, you will get:

  • Microsoft SQL Server

Installing SQL Server into Docker

Note: in some cases, especially recently, the search might not go to the Microsoft SQL Server directly. It will list something like this:

Installing SQL Server into Docker

But, we still can find the Microsoft SQL Server in the list:

Installing SQL Server into Docker

Click that to open the Microsoft SQL Server Page:

Installing SQL Server into Docker

Down scrolling, we can see How to use this Image:

Installing SQL Server into Docker

Note:

In this article, to set up SQL Server in Docker, we mainly follow these two articles: 

From these two articles, we can get detailed step-by-step explanations. However, for a general case, if we need to install some container image, we can follow the abovementioned method.

D - Pull Microsoft SQL Server Image into Docker:

I assume we have already installed Docker, otherwise, see: Container (2), Docker. Check the Docker version by

Docker -v

We can see that Docker is installed, the version is 20.10.23. Check the Docker images installed:

Docker images

Pull the official SQL Server Docker image from Docker Hub: by command

docker pull mcr.microsoft.com/mssql/server

Recheck Docker Images. Microsoft SQL Server image has been pulled into Docker Images:

E - Start Docker Container for the SQL Server Image:

We will use the following command to create and start a docker container:

docker run 
     --name sql_2022_1433 
     -e "ACCEPT_EULA=Y" 
     -e "SA_PASSWORD=1Secure*Password1" 
     -p 1433:1433 
     -d mcr.microsoft.com/mssql/server

where

  • docker run command
    • --name --- assign name
    • -e (--env) --- Set environment variables
      • ACCEPT_EULA --- Set to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image.
      • SA_PASSWORD --- this is the password for the default user sa, so the password must be SQL acceptable. Otherwise, the Container can be created but not be active
    • -p (--publish) --- Publish a container’s port(s) to the host
      • Push Container's port to map host port, here 1433 maps to 1433
    • -d (--detach) --- Run Container in the background and print container ID
      • assign to the image

Before running this command, we check the Docker Containers by

docker container ls

or

docker container ls -a

Installing SQL Server into Docker

The first command will show the active containers in Docker, while the second command will show all active and inactive containers. We can see there is one inactive Container named as llucid_herts, 

Now, run the command: Docker run ...

The Container is created and Active, named as sql_2022_1433

Installing SQL Server into Docker

Note:

1, For the docker pull command:

docker pull mcr.microsoft.com/mssql/server

And the docker run command

docker run --name sql_2022_1434 -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=1Secure*Password1" -p 1434:1433 -d mcr.microsoft.com/mssql/server

We can run them one by one to get the image first and then start the docker container. However, we can also use docker run. In this situation, the run command will run the pull first automatically. See: How to Set Up SQL Server Database with Docker - YouTube.

2, In some cases, the docker run command can create the Container but cannot run it,

Case 1: if the password given is not fit the SQL Server requirement

Case 2: when we run the docker run command with a single quote instead of a double quote, such as

docker run --name sql_2022_1434 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=1Secure*Password1' -p 1434:1433 -d mcr.microsoft.com/mssql/server

F - Open Container SQL Server from SSMS (SQL Server Management Studio):

For the default port 1433, we use localhost:

We see the SQL Server is on.

Suppose we use port 1434 for the Container instead of 1433. Then when login, we should use the server name as localhost,1434

and the result will be:

References


Similar Articles