Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Docker Building a SQL Server image

$
0
0

There are several Microsoft SQL Server container images available for download. These consists of SQL Server 2017 and now SQL Server 2019 CTP2.1. Most of those can be found here . However, what if you need to customize an image by adding additional features? Docker provides a command line utility to build new images using Dockerfiles. A Dockerfileis a text document that contains all the commands a user could call on the command line to assemble an image. For more information see here .

Docker build

Using docker build, we’ll create an image based on the Red Hat Enterprise linux OS. The Dockerfile we’ll use for all examples can be found in the https://github.com/sqlservermigrations/containers repository. Using Git, we’ll clone the repo and build the image.

Prerequisites You will need access to the Red Hat Container Catalog via a Red Hat subscription. A free developer subscription is available. You’ll need a Red Hat Enterprise Linux VM (or physical machine) that has been registered to your subscription. The Red Hat Container Developer Kit can be utilized as well (method not shown here). If using a RHEL VM or physical machine, Docker must be installed. git is needed to clone this repository. Prior to building the image, the password within the setup-tools.sh file, line 9, must be set to the same password used when building the image. Building the image using a RHEL VM SSH to the RHEL VM Verify docker is running. sudo systemctl status docker If the status does not show active, start docker.
sudo systemctl start docker Make a directory to clone the repository to and then clone using git. If git isn’t installed, use sudo yum - y install git to install.
mkdir ~/sqlservermigrations cd ~/sqlservermigrations git clone https://github.com/sqlservermigrations/containers Build the image. This will take approximately 10 15 minutes (maybe longer) depending on your internet connection. -t is used to name and tag the image (registry/name:tag). For full list of options which can be used with docker build, see here .
cd ~/sqlservermigrations/containers sudo docker build . -t sqlservermigrations/mssqlserver:2019_CTP2.1 Verify the image was created successfully.
sudo docker images
Docker   Building a SQL Server image
Starting a mssql-server instance Use docker run to create a container from the new image. Docker run creates a process, known as a container, is isolated, contains its own file system, has its own networking, and has its own isolated process tree separate from the host.
sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=YourStrongP@ssword' --name mssql2019 -p 1433:1433 -d sqlservermigrations/mssqlserver:2019_CTP2.1 The command finishes and returns the unique container ID. Next, run sudo docker ps ( docker ps )to ensure the container is running.
Docker   Building a SQL Server image

The docker run command above uses several parameters:

-e The image requires at least two environment variables to be provided. Those are provided via the “-e” flag. ACCEPT_EULA Must be equal to Y. SA_PASSWORD Must be set to a strong password. Full list of environment variables can be found here . name Provides a name for the new container. Otherwise all references made to the container in proceeding steps would need to reference the container ID. -p Specifies the port in which SQL Server is listening on internally and the host port to listen on. For example, if you wanted to run multiple containers you’d specify a different host port for each (5002:1433). If connecting remotely be sure any firewalls on the host are configured to allow traffic on the specified host port. -d Starts the container in detached mode. Viewing the container logs

Docker logs provides a way for viewing logs generated within containers. Results can then be piped to tools such as grep for doing additional filtering. Let’s take a look.

Run docker logs and provide the name of container.
sudo docker logs mssql2019 This should print out the SQL Server error log.

To filter for a specific text within the log, pipe the results to grep. We’ll see how many CPU cores SQL Server is using.

sudo docker logs mssql2019 | grep "sockets"
Docker   Building a SQL Server image

The screenshot above shows SQL Server has detected 2 sockets with 8 cores per socket.

Connect to Microsoft SQL Server running in a container SQLCMD

This particular image includes the mssql-tools package. SQLCMD can be used as a quick test for connectivity. We’ll use docker exec to do connect to the container and run a query using sqlcmd.

Run docker exec in interactive mode (-it).

sudo docker exec -it mssql2019 /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P YourStrongP@ssword -Q 'SELECT @@VERSION'
Docker   Building a SQL Server image
Azure Data Studio

If the host port is allowed through the firewall then you can connect remotely using other tools. We’ll use Azure Data Studio to connect.

Grab the host IP. Open Azure Data Studio and create a new connection. Specify the host IP, port, user name, and password. If port 1433 (default) is used, no need to add it to the connection properties.
Docker   Building a SQL Server image

After connecting, you can run queries, view the SQL agent jobs (if the SQL Server Agent extension is installed), and view the objects within the DBA database.


Docker   Building a SQL Server image

Viewing all articles
Browse latest Browse all 3160

Latest Images

Trending Articles