在 Docker 上配置 SQL Server 容器映像Configure SQL Server container images on Docker

适用对象:是SQL Server(仅 Linux)否Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库APPLIES TO: yesSQL Server (Linux only) noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

本文介绍如何通过 Docker 配置和使用 mssql-server-linux 容器映像This article explains how to configure and use the mssql-server-linux container image with Docker.

有关其他部署方案,请参阅:For other deployment scenarios, see:

此映像包含在 Linux 上运行的 SQL Server(基于 Ubuntu 16.04)。This image consists of SQL Server running on Linux based on Ubuntu 16.04. 它可与 Linux 上或用于 Mac/Windows 的 Docker 上的 Docker 引擎 1.8+ 配合使用。It can be used with the Docker Engine 1.8+ on Linux or on Docker for Mac/Windows.

备注

本文专门重点介绍 mssql-server-linux 映像的使用。This article specifically focuses on using the mssql-server-linux image. 虽然没有介绍 Windows 映像,但可在 mssql-server-windows Docker Hub 页上找到关于它的详细信息。The Windows image is not covered, but you can learn more about it on the mssql-server-windows Docker Hub page.

重要

在选择运行 SQL Server 容器以用于生产用例之前,请查看 SQL Server 容器的支持策略,以确保在支持的配置上运行。Before choosing to run a SQL Server container for production use cases, please review our support policy for SQL Server Containers to ensure that you are running on a supported configuration.

拉取并运行容器映像Pull and run the container image

若要拉取并运行 SQL Server 2017 和 SQL Server 2019 的 Docker 容器映像,请按照以下快速入门中的先决条件和步骤执行操作:To pull and run the Docker container images for SQL Server 2017 and SQL Server 2019, follow the prerequisites and steps in the following quickstart:

本配置文章在以下部分中提供其他使用方案。This configuration article provides additional usage scenarios in the following sections.

运行基于 RHEL 的容器映像Run RHEL-based container images

SQL Server Linux 容器映像的文档指向基于 Ubuntu 的容器。The documentation for SQL Server Linux container images points to Ubuntu-based containers. 从 SQL Server 2019 开始,可使用基于 Red Hat Enterprise Linux (RHEL) 的容器。Beginning with SQL Server 2019, you can use containers based on Red Hat Enterprise Linux (RHEL). 在所有 docker 命令中将容器存储库从 mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04 更改为 mcr.microsoft.com/mssql/rhel/server:2019-RC1 。Change the container repository from mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04 to mcr.microsoft.com/mssql/rhel/server:2019-RC1 in all of your docker commands.

例如,以下命令拉取使用 RHEL 的最新 SQL Server 2019 容器:For example, the following command pulls the latest SQL Server 2019 container that uses RHEL:

sudo docker pull mcr.microsoft.com/mssql/rhel/server:2019-RC1
docker pull mcr.microsoft.com/mssql/rhel/server:2019-RC1

备注

自 SQL Server 2019 的 GA 版本开始,最新的 RHEL 容器映像仍是 RC1 版本。As of the GA release of SQL Server 2019, the latest RHEL container image is still the RC1 version. 此版本不适用于生产使用。This version is not meant for production use. 当新的 RHEL 容器映像可用时,将更新本文。This article will be updated when a newer RHEL container image becomes available.

运行生产容器映像Run production container images

上一部分中的快速入门从 Docker Hub 运行 SQL Server 的免费 Developer Edition。The quickstart in the previous section runs the free Developer edition of SQL Server from Docker Hub. 如果想要运行生产容器映像(例如 Enterprise、Standard 或 Web Edition),大部分信息仍然适用。Most of the information still applies if you want to run production container images, such as Enterprise, Standard, or Web editions. 但是,存在一些差异,此处将其列出。However, there are a few differences that are outlined here.

  • 如果拥有有效的许可证,则只能在生产环境中使用 SQL Server。You can only use SQL Server in a production environment if you have a valid license. 可在此处获取免费的 SQL Server Express 生产许可证。You can obtain a free SQL Server Express production license here. SQL Server Standard 和 Enterprise Edition 许可证可通过 Microsoft 批量许可获得。SQL Server Standard and Enterprise Edition licenses are available through Microsoft Volume Licensing.

  • Developer 容器映像也可以配置为运行生产版本。The Developer container image can be configured to run the production editions as well. 使用以下步骤运行生产版本:Use the following steps to run production editions:

查看快速入门中的要求并运行过程。Review the requirements and run procedures in the quickstart. 必须使用 MSSQL_PID 环境变量指定生产版本。You must specify your production edition with the MSSQL_PID environment variable. 以下示例介绍如何运行 Enterprise Edition 的最新 SQL Server 2017 容器映像:The following example shows how to run the latest SQL Server 2017 container image for the Enterprise Edition:

docker run --name sqlenterprise \
      -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' \
      -e 'MSSQL_PID=Enterprise' -p 1433:1433 \
      -d mcr.microsoft.com/mssql/server:2017-latest
docker run --name sqlenterprise `
      -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" `
      -e "MSSQL_PID=Enterprise" -p 1433:1433 `
      -d "mcr.microsoft.com/mssql/server:2017-latest"

重要

通过将值 Y 传递给环境变量 ACCEPT_EULA 并将版本值传递给 MSSQL_PID,你表明自己拥有打算使用的 SQL Server 版本的现行有效有许可证。By passing the value Y to the environment variable ACCEPT_EULA and an edition value to MSSQL_PID, you are expressing that you have a valid and existing license for the edition and version of SQL Server that you intend to use. 你还同意自己对在 Docker 容器映像中运行的 SQL Server 软件的使用将受 SQL Server 许可条款的约束。You also agree that your use of SQL Server software running in a Docker container image will be governed by the terms of your SQL Server license.

备注

有关 MSSQL_PID 的可能值的完整列表,请参阅在 Linux 上使用环境变量配置 SQL Server 设置For a full list of possible values for MSSQL_PID, see Configure SQL Server settings with environment variables on Linux.

连接和查询Connect and query

可从容器外部或内部对容器中的 SQL Server 进行连接和查询。You can connect and query SQL Server in a container from either outside the container or from within the container. 以下部分介绍这两种方案。The following sections explain both scenarios.

容器外的工具Tools outside the container

可从支持 SQL 连接的任何 Linux、Windows 或 macOS 外部工具连接到 Docker 计算机上的 SQL Server 实例。You can connect to the SQL Server instance on your Docker machine from any external Linux, Windows, or macOS tool that supports SQL connections. 一些常用工具包括:Some common tools include:

以下示例使用 sqlcmd 连接到在 Docker 容器中运行的 SQL Server。The following example uses sqlcmd to connect to SQL Server running in a Docker container. 连接字符串中的 IP 地址为运行容器的主机的 IP 地址。The IP address in the connection string is the IP address of the host machine that is running the container.

sqlcmd -S 10.3.2.4 -U SA -P '<YourPassword>'
sqlcmd -S 10.3.2.4 -U SA -P "<YourPassword>"

如果映射的主机端口不是默认的 1433,请将该端口添加到连接字符串中。If you mapped a host port that was not the default 1433, add that port to the connection string. 例如,如果在 docker run 命令中指定了 -p 1400:1433,则请通过显式指定端口 1400 来进行连接。For example, if you specified -p 1400:1433 in your docker run command, then connect by explicitly specify port 1400.

sqlcmd -S 10.3.2.4,1400 -U SA -P '<YourPassword>'
sqlcmd -S 10.3.2.4,1400 -U SA -P "<YourPassword>"

容器内的工具Tools inside the container

从 SQL Server 2017 开始,容器映像中加入了 SQL Server 命令行工具Starting with SQL Server 2017, the SQL Server command-line tools are included in the container image. 如果使用交互式命令提示符附加至此映像,则可在本地运行工具。If you attach to the image with an interactive command-prompt, you can run the tools locally.

  1. 使用 docker exec -it 命令在运行的容器内部启动交互式 Bash Shell。Use the docker exec -it command to start an interactive bash shell inside your running container. 在以下示例中,e69e056c702d 是容器 ID。In the following example e69e056c702d is the container ID.

    docker exec -it e69e056c702d "bash"
    

    提示

    并非始终需要指定完整的容器 ID。只需指定能够唯一标识它的足够字符即可。You don't always have to specify the entire container id. You only have to specify enough characters to uniquely identify it. 因此,在本示例中,使用 e6e69 足矣,无需使用完整 ID。So in this example, it might be enough to use e6 or e69 rather than the full id.

  2. 在容器内部使用 sqlcmd 进行本地连接。Once inside the container, connect locally with sqlcmd. 请注意,默认情况下,sqlcmd 不在路径之中,因此需要指定完整的路径。Note that sqlcmd is not in the path by default, so you have to specify the full path.

    /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourPassword>'
    
  3. 使用 sqlcmd 完成操作后,键入 exitWhen finished with sqlcmd, type exit.

  4. 使用交互式命令提示符完成操作后,键入 exitWhen finished with the interactive command-prompt, type exit. 退出交互式 Bash Shell 后,容器将继续运行。Your container continues to run after you exit the interactive bash shell.

运行多个 SQL Server 容器Run multiple SQL Server containers

Docker 支持在同一主机上运行多个 SQL Server 容器。Docker provides a way to run multiple SQL Server containers on the same host machine. 对要求在同一主机上运行多个 SQL Server 实例的方案使用此解决办法。Use this approach for scenarios that require multiple instances of SQL Server on the same host. 每个容器必须在不同的端口上公开自己。Each container must expose itself on a different port.

以下示例创建两个 SQL Server 2017 容器,并将它们映射到主机的 14011402端口。The following example creates two SQL Server 2017 containers and maps them to ports 1401 and 1402 on the host machine.

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1401:1433 -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1402:1433 -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1401:1433 -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1402:1433 -d mcr.microsoft.com/mssql/server:2017-latest

以下示例创建两个 SQL Server 2019 容器,并将它们映射到主机的 1401 和 1402 端口 。The following example creates two SQL Server 2019 containers and maps them to ports 1401 and 1402 on the host machine.

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1401:1433 -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1402:1433 -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1401:1433 -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1402:1433 -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04

现在,两个 SQL Server 实例在单独的容器内运行。Now there are two instances of SQL Server running in separate containers. 客户端可通过使用 Docker 主机的 IP 地址和容器的端口号连接到每个 SQL Server 实例。Clients can connect to each SQL Server instance by using the IP address of the Docker host and the port number for the container.

sqlcmd -S 10.3.2.4,1401 -U SA -P '<YourPassword>'
sqlcmd -S 10.3.2.4,1402 -U SA -P '<YourPassword>'
sqlcmd -S 10.3.2.4,1401 -U SA -P "<YourPassword>"
sqlcmd -S 10.3.2.4,1402 -U SA -P "<YourPassword>"

创建自定义容器Create a customized container

可以创建自己的 Dockerfile 来创建自定义 SQL Server 容器。It is possible to create your own Dockerfile to create a customized SQL Server container. 有关详细信息,请参阅组合使用 SQL Server 和节点应用程序的演示For more information, see a demo that combines SQL Server and a Node application. 如果创建了自己的 Dockerfile,请注意前台进程,因为此进程控制容器的生命周期。If you do create your own Dockerfile, be aware of the foreground process, because this process controls the life of the container. 如果它退出,容器将关闭。If it exits, the container will shutdown. 例如,如果想要运行脚本并启动 SQL Server,请确保 SQL Server 进程是最右侧的命令。For example, if you want to run a script and start SQL Server, make sure that the SQL Server process is the right-most command. 所有其他命令都会在后台运行。All other commands are run in the background. 以下命令在 Dockerfile 中对此进行说明:The following command illustrates this inside a Dockerfile:

/usr/src/app/do-my-sql-commands.sh & /opt/mssql/bin/sqlservr

如果撤销上一个示例中的命令,则容器将在 do-my-sql-commands.sh 脚本完成时关闭。If you reversed the commands in the previous example, the container would shutdown when the do-my-sql-commands.sh script completes.

保留数据Persist your data

即使通过 docker stopdocker start 重启容器,SQL Server 配置仍会更改,且数据库文件依然保留在容器中。Your SQL Server configuration changes and database files are persisted in the container even if you restart the container with docker stop and docker start. 但是,如果使用 docker rm 删除容器,则会删除容器中的所有内容,包括 SQL Server 和数据库。However, if you remove the container with docker rm, everything in the container is deleted, including SQL Server and your databases. 以下部分介绍如何使用数据卷保留数据库文件(即使关联的容器已被删除)。The following section explains how to use data volumes to persist your database files even if the associated containers are deleted.

重要

对于 SQL Server,了解 Docker 中的数据持久性至关重要。For SQL Server, it is critical that you understand data persistence in Docker. 除本部分讨论的内容外,请参阅有关如何在 Docker 容器中管理数据的 Docker 文档。In addition to the discussion in this section, see Docker's documentation on how to manage data in Docker containers.

将主机目录作为数据卷装载Mount a host directory as data volume

第一种方法是在主机上将目录作为容器中的数据卷装载。The first option is to mount a directory on your host as a data volume in your container. 为此,请将 docker run 命令与 -v <host directory>:/var/opt/mssql 标志配合使用。To do that, use the docker run command with the -v <host directory>:/var/opt/mssql flag. 这允许在容器执行之间还原数据。This allows the data to be restored between container executions.

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1433:1433 -v <host directory>:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 -v <host directory>:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1433:1433 -v <host directory>:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 -v <host directory>:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04

借助此方法,还能共享和查看 Docker 外部的主机上的文件。This technique also enables you to share and view the files on the host outside of Docker.

重要

目前不支持 Mac 上的 Docker 与 Linux 映像上的 SQL Server 之间的主机卷映射。Host volume mapping for Docker on Mac with the SQL Server on Linux image is not supported at this time. 请改为使用数据卷容器。Use data volume containers instead. 此限制特定于 /var/opt/mssql 目录。This restriction is specific to the /var/opt/mssql directory. 从已装载目录进行读取操作可正常运行。Reading from a mounted directory works fine. 例如,可在 Mac 上使用 –v 装载主机目录,并通过驻留在主机上的 .bak 文件还原备份。For example, you can mount a host directory using -v on Mac and restore a backup from a .bak file that resides on the host.

使用数据卷容器Use data volume containers

第二种方法是使用数据卷容器。The second option is to use a data volume container. 可通过指定卷名(而不是包含 -v 参数的主机目录)来创建数据卷容器。You can create a data volume container by specifying a volume name instead of a host directory with the -v parameter. 以下示例创建名为 sqlvolume 的共享数据卷。The following example creates a shared data volume named sqlvolume.

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04

备注

早期版本的 Docker 不支持通过此方法在 run 命令中隐式创建数据卷。This technique for implicitly creating a data volume in the run command does not work with older versions of Docker. 在这种情况下,请使用 Docker 文档创建和装载数据卷容器中列出的显式步骤。In that case, use the explicit steps outlined in the Docker documentation, Creating and mounting a data volume container.

即使停止并删除此容器,数据卷仍然存在。Even if you stop and remove this container, the data volume persists. 可使用 docker volume ls 命令进行查看。You can view it with the docker volume ls command.

docker volume ls

如果随后创建另一个具有相同卷名的容器,则新容器会使用卷中包含的相同 SQL Server 数据。If you then create another container with the same volume name, the new container uses the same SQL Server data contained in the volume.

若要删除数据卷容器,请使用 docker volume rm 命令。To remove a data volume container, use the docker volume rm command.

警告

如果删除数据卷容器,则该容器中的所有 SQL Server 数据都会被永久删除。If you delete the data volume container, any SQL Server data in the container is permanently deleted.

备份和还原Backup and restore

除这些容器技术外,还可使用 SQL Server 标准备份和还原技术。In addition to these container techniques, you can also use standard SQL Server backup and restore techniques. 可通过备份文件来保护数据,或将数据移至其他 SQL Server 实例。You can use backup files to protect your data or to move the data to another SQL Server instance. 有关详细信息,请参阅在 Linux 上备份和还原 SQL Server 数据库For more information, see Backup and restore SQL Server databases on Linux.

警告

如果要创建备份,请确保在容器外部创建或复制备份文件。If you do create backups, make sure to create or copy the backup files outside of the container. 否则,一旦删除容器,备份文件也将随之删除。Otherwise, if the container is removed, the backup files are also deleted.

在容器中执行命令Execute commands in a container

如果正在运行容器,则可从主机终端在容器内部执行命令。If you have a running container, you can execute commands within the container from a host terminal.

若要获取容器 ID,请运行:To get the container ID run:

docker ps

若要在容器中启动 Bash 终端,请运行:To start a bash terminal in the container run:

docker exec -it <Container ID> /bin/bash

现在,可以像在容器内的终端上那样运行这些命令。Now you can run commands as though you are running them at the terminal inside the container. 完成后,键入 exitWhen finished, type exit. 这将退出交互式命令会话,但容器会继续运行。This exits in the interactive command session, but your container continues to run.

从容器复制文件Copy files from a container

若要从容器复制文件,请使用以下命令:To copy a file out of the container, use the following command:

docker cp <Container ID>:<Container path> <host path>

示例:Example:

docker cp d6b75213ef80:/var/opt/mssql/log/errorlog /tmp/errorlog
docker cp d6b75213ef80:/var/opt/mssql/log/errorlog C:\Temp\errorlog

将文件复制到容器中Copy files into a container

若要将文件复制到容器中,请使用以下命令:To copy a file into the container, use the following command:

docker cp <Host path> <Container ID>:<Container path>

示例:Example:

docker cp /tmp/mydb.mdf d6b75213ef80:/var/opt/mssql/data
docker cp C:\Temp\mydb.mdf d6b75213ef80:/var/opt/mssql/data

配置时区Configure the timezone

若要在具有特定时区的 Linux 容器中运行 SQL Server,请配置 TZ 环境变量。To run SQL Server in a Linux container with a specific timezone, configure the TZ environment variable. 若要查找正确的时区值,请从 Linux bash 提示符运行 tzselect 命令:To find the right timezone value, run the tzselect command from a Linux bash prompt:

tzselect

选择时区后,tzselect 显示类似以下内容的输出:After selecting the timezone, tzselect displays output similar to the following:

The following information has been given:

        United States
        Pacific

Therefore TZ='America/Los_Angeles' will be used.

可使用此信息在 Linux 容器中设置相同的环境变量。You can use this information to set the same environment variable in your Linux container. 以下示例介绍如何在 Americas/Los_Angeles 时区的容器中运行 SQL Server:The following example shows how to run SQL Server in a container in the Americas/Los_Angeles timezone:

sudo docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' \
   -p 1433:1433 --name sql1 \
   -e 'TZ=America/Los_Angeles'\
   -d mcr.microsoft.com/mssql/server:2017-latest 
sudo docker run -e 'ACCEPT_EULA=Y' -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" `
   -p 1433:1433 --name sql1 `
   -e "TZ=America/Los_Angeles" `
   -d mcr.microsoft.com/mssql/server:2017-latest 
sudo docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' \
   -p 1433:1433 --name sql1 \
   -e 'TZ=America/Los_Angeles'\
   -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
sudo docker run -e 'ACCEPT_EULA=Y' -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" `
   -p 1433:1433 --name sql1 `
   -e "TZ=America/Los_Angeles" `
   -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04

运行特定 SQL Server 容器映像Run a specific SQL Server container image

在某些情况下,可能不希望使用最新的 SQL Server 容器映像。There are scenarios where you might not want to use the latest SQL Server container image. 若要运行特定 SQL Server 容器映像,请使用以下步骤:To run a specific SQL Server container image, use the following steps:

  1. 确定想要使用的版本的 Docker 标记Identify the Docker tag for the release you want to use. 若要查看可用标记,请参阅 mssql-server-linux Docker Hub 页To view the available tags, see the mssql-server-linux Docker hub page.

  2. 使用标记拉取 SQL Server 容器映像。Pull the SQL Server container image with the tag. 例如,若要拉取 RC1 映像,请将以下命令中的 <image_tag> 替换为 rc1For example, to pull the RC1 image, replace <image_tag> in the following command with rc1.

    docker pull mcr.microsoft.com/mssql/server:<image_tag>
    
  3. 若要使用该映像运行新容器,请在 docker run 命令中指定标记名称。To run a new container with that image, specify the tag name in the docker run command. 在以下命令中,将 <image_tag> 替换为想要运行的版本。In the following command, replace <image_tag> with the version you want to run.

    docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1401:1433 -d mcr.microsoft.com/mssql/server:<image_tag>
    
    docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1401:1433 -d mcr.microsoft.com/mssql/server:<image_tag>
    

这些步骤也可用于降级现有容器。These steps can also be used to downgrade an existing container. 例如,你可能会希望回滚或降级正在运行的容器以进行故障排除或测试。For example, you might want to rollback or downgrade a running container for troubleshooting or testing. 若要降级正在运行的容器,必须对数据文件夹使用持久性技术。To downgrade a running container, you must be using a persistence technique for the data folder. 按照升级部分所述的相同步骤进行操作,但在运行新容器时指定早期版本的标记名称。Follow the same steps outlined in the upgrade section, but specify the tag name of the older version when you run the new container.

检查容器版本Check the container version

如果想要了解正在运行的 docker 容器中的 SQL Server 的版本,请运行以下命令以显示它。If you want to know the version of SQL Server in a running docker container, run the following command to display it. <Container ID or name> 替换为目标容器 ID 或名称。Replace <Container ID or name> with the target container ID or name. <YourStrong!Passw0rd> 替换为 SA 登录名的 SQL Server 密码。Replace <YourStrong!Passw0rd> with the SQL Server password for the SA login.

sudo docker exec -it <Container ID or name> /opt/mssql-tools/bin/sqlcmd \
   -S localhost -U SA -P '<YourStrong!Passw0rd>' \
   -Q 'SELECT @@VERSION'
docker exec -it <Container ID or name> /opt/mssql-tools/bin/sqlcmd `
   -S localhost -U SA -P "<YourStrong!Passw0rd>" `
   -Q 'SELECT @@VERSION'

还可以标识目标 docker 容器映像的 SQL Server 版本和生成号。You can also identify the SQL Server version and build number for a target docker container image. 以下命令显示 mcr.microsoft.com/mssql/server:2017-latest 映像的 SQL Server 版本和生成信息 。The following command displays the SQL Server version and build information for the mcr.microsoft.com/mssql/server:2017-latest image. 它通过运行具有环境变量 PAL_PROGRAM_INFO=1 的新容器来实现此目的。It does this by running a new container with an environment variable PAL_PROGRAM_INFO=1. 生成的容器会立即退出,且 docker rm 命令会将其删除。The resulting container instantly exits, and the docker rm command removes it.

sudo docker run -e PAL_PROGRAM_INFO=1 --name sqlver \
   -ti mcr.microsoft.com/mssql/server:2017-latest && \
   sudo docker rm sqlver
docker run -e PAL_PROGRAM_INFO=1 --name sqlver `
   -ti mcr.microsoft.com/mssql/server:2017-latest; `
   docker rm sqlver

上一个命令显示类似以下输出的版本信息:The previous commands display version information similar to the following output:

sqlservr
  Version 14.0.3029.16
  Build ID ee3d3882f1c48a7a7e590a620153012eaedc2f37143d485df945a079b9d4eeea
  Build Type release
  Git Version 65d42c4
  Built at Sat Jun 16 01:20:11 GMT 2018

PAL
  Build ID 60cfcb134bbae96d311f6a4f56aeb5a685b3809de80bcb61ec587a8f58b555eb
  Build Type release
  Git Version 21a4c11
  Built at Sat Jun 16 01:18:53 GMT 2018

Packages
  system.sfp                    6.2.9200.1,21a4c1178,
  system.common.sfp             10.0.15063.540
  system.certificates.sfp       6.2.9200.1,21a4c1178,
  system.netfx.sfp              4.6.1590.0
  secforwarderxplat.sfp         14.0.3029.16
  sqlservr.sfp                  14.0.3029.16
  sqlagent.sfp                  14.0.3029.16

升级容器中的 SQL ServerUpgrade SQL Server in containers

若要使用 Docker 升级容器映像,请先确定升级版本的标记。To upgrade the container image with Docker, first identify the tag for the release for your upgrade. 使用 docker pull 命令从注册表中拉取此版本:Pull this version from the registry with the docker pull command:

docker pull mcr.microsoft.com/mssql/server:<image_tag>

此操作会更新创建的任何新容器的 SQL Server 映像,但不会更新任何正在运行的容器中的 SQL Server。This updates the SQL Server image for any new containers you create, but it does not update SQL Server in any running containers. 为此,必须使用最新的 SQL Server 容器映像创建新容器,并将数据迁移到该新容器。To do this, you must create a new container with the latest SQL Server container image and migrate your data to that new container.

  1. 确保为现有 SQL Server 容器使用一种数据持久性技术Make sure you are using one of the data persistence techniques for your existing SQL Server container. 这样便可以启动具有相同数据的新容器。This enables you to start a new container with the same data.

  2. 使用 docker stop 命令停止 SQL Server 容器。Stop the SQL Server container with the docker stop command.

  3. 使用 docker run 创建新的 SQL Server 容器,并指定映射主机目录或数据卷容器。Create a new SQL Server container with docker run and specify either a mapped host directory or a data volume container. 确保使用特定标记进行 SQL Server 升级。Make sure to use the specific tag for your SQL Server upgrade. 新容器现在使用新版 SQL Server 和现有 SQL Server 数据。The new container now uses a new version of SQL Server with your existing SQL Server data.

    重要

    目前仅支持在 RC1、RC2 和 GA 之间升级。Upgrade is only supported between RC1, RC2, and GA at this time.

  4. 在新容器中验证数据库和数据。Verify your databases and data in the new container.

  5. 使用 docker rm 删除旧容器(可选)。Optionally, remove the old container with docker rm.

生成并运行非根 SQL Server 2017 容器Build and run non-root SQL Server 2017 containers

按照以下步骤生成以 mssql(非根)用户身份启动的 SQL Server 2017 容器。Follow the steps below to build a SQL Server 2017 container that starts up as the mssql(non-root) user.

备注

SQL Server 2019 容器自动以非根身份启动,因此以下步骤仅适用于 SQL Server 2017 容器,默认情况下,该容器以根身份启动。SQL Server 2019 containers automatically start up as non-root, so the following steps only apply to SQL Server 2017 containers, which start as root by default.

  1. 下载适用于非根 SQL Server 容器的示例dockerfile,并将其另存为 dockerfileDownload the sample dockerfile for non-root SQL Server Container and save it as dockerfile.

  2. 在 dockerfile 目录的上下文中运行以下命令,生成非根 SQL Server 容器:Run the following command in the context of the dockerfile directory to build the non-root SQL Server container:

cd <path to dockerfile>
docker build -t 2017-latest-non-root .
  1. 启动容器。Start the container.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword@" --cap-add SYS_PTRACE --name sql1 -p 1433:1433 -d 2017-latest-non-root

备注

非根 SQL Server 容器需要 --cap-add SYS_PTRACE 标志来生成用于排除故障的转储。The --cap-add SYS_PTRACE flag is required for non-root SQL Server containers to generate dumps for troubleshooting purposes.

  1. 检查是否以非根用户的身份运行容器:Check that the container is running as non-root user:

docker 执行到容器中。docker exec into the container.

docker exec -it sql1 bash

运行 whoami,这将返回容器中运行的用户。Run whoami which will return the user running within the container.

whoami

在主机上以其他非根用户的身份运行容器Run container as a different non-root user on the host

若要以其他非根用户的身份运行 SQL Server 容器,请将 -u 标志添加到 docker run 命令。To run the SQL Server container as a different non-root user, add the -u flag to the docker run command. 非根容器具有以下限制:必须作为根组的一部分运行,除非已将卷装载到非根用户可以访问的“/var/opt/mssql”。The non-root container has the restriction that it must run as part of the root group unless a volume is mounted to '/var/opt/mssql' that the non-root user can access. 根组不向非根用户授予任何额外的根权限。The root group doesn’t grant any extra root permissions to the non-root user.

以具有 UID 4000 的用户身份运行Run as a user with a UID 4000

可使用自定义 UID 启动 SQL Server。You can start SQL Server with a custom UID. 例如,以下命令使用 UID 4000 启动 SQL Server:For example, the command below starts SQL Server with UID 4000:

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" --cap-add SYS_PTRACE -u 4000:0 -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

警告

确保 SQL Server 容器具有命名用户,如“mssql”或“root”,否则 SQLCMD 将无法在容器中运行。Ensure that the SQL Server container has a named user such as 'mssql' or 'root' or SQLCMD will not be able to run within the container. 可通过在容器中运行 whoami 来检查是否以命名用户的身份运行 SQL Server 容器。You can check if the SQL Server container is running as a named user by running whoami within the container.

以根用户的身份运行非根容器Run the non-root container as the root user

如果需要,以根用户的身份运行非根容器。You can run the non-root container as the root user if required. 这还会将所有文件权限自动授予容器,因为容器具有更高的特权。This would also grant all file permissions automatically to the container because it is higher privilege.

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" -u 0:0 -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

在主机计算机上以用户的身份运行Run as a user on your host machine

可使用以下命令在主机计算机上以现有用户身份启动 SQL Server:You can start SQL Server with an existing user on the host machine with the following command:

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" --cap-add SYS_PTRACE -u $(id -u myusername):0 -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

以其他用户和组的身份运行Run as a different user and group

可使用自定义用户和组启动 SQL Server。You can start SQL Server with a custom user and group. 在此示例中,已装载的卷具有为主机计算机上的用户或组配置的权限。In this example, the mounted volume has permissions configured for the user or group on the host machine.

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" --cap-add SYS_PTRACE -u (id -u myusername):(id -g myusername) -v /path/to/mssql:/var/opt/mssql -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

为非根容器配置持久存储权限Configure persistent storage permissions for non-root containers

若要允许非根用户访问已装载的卷上的 DB 文件,请确保运行容器所用的用户/组可以接触持久文件存储。To allow the non-root user to access DB files that are on mounted volumes, ensure that the user/group you run the container under can touch the persistent file storage.

可通过此命令获取数据库文件的当前所有权。You can get the current ownership of the database files with this command.

ls -ll <database file dir>

如果 SQL Server 无权访问持久数据库文件,请运行以下命令之一。Run one of the following commands if SQL Server does not have access to persisted database files.

授予对 DB 文件的根组读/写访问权限Grant the root group r/w access to the DB files

授予对以下目录的根组权限,使非根 SQL Server 容器有权访问数据库文件。Grant the root group permissions to the following directories so that the non-root SQL Server container has access to database files.

chgroup -R 0 <database file dir>
chmod -R g=u <database file dir>

将非根用户设置为文件的所有者。Set the non-root user as the owner of the files.

这可以是默认的非根用户,也可以是要指定的任何其他非根用户。This can be the default non-root user, or any other non-root user you’d like to specify. 在此示例中,我们将 UID 10001 设置为非根用户。In this example, we set UID 10001 as the non-root user.

chown -R 10001:0 <database file dir>

更改默认文件位置Change the default file location

添加 MSSQL_DATA_DIR 变量以在 docker run 命令中更改数据目录,然后将卷装载到容器的用户有权访问的位置。Add the MSSQL_DATA_DIR variable to change your data directory in your docker run command, then mount a volume to that location that your container’s user has access to.

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" -e "MSSQL_DATA_DIR=/my/file/path" -v /my/host/path:/my/file/path -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

故障排除Troubleshooting

以下部分提供关于在容器中运行 SQL Server 的故障排除建议。The following sections provide troubleshooting suggestions for running SQL Server in containers.

Docker 命令错误Docker command errors

如果遇到任何 docker 命令错误,请确保 Docker 服务正在运行,并尝试通过提升的权限运行。If you get errors for any docker commands, make sure that the docker service is running, and try to run with elevated permissions.

例如,在 Linux 上运行 docker 命令时可能会遇到以下错误:For example, on Linux, you might get the following error when running docker commands:

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

如果在 Linux 上遇到此错误,请尝试对相同的命令加上 sudo 前缀,然后再运行。If you get this error on Linux, try running the same commands prefaced with sudo. 如果失败,请验证 Docker 服务是否正在运行;如果没有,请启动它。If that fails, verify the docker service is running, and start it if necessary.

sudo systemctl status docker
sudo systemctl start docker

在 Windows 上,验证并确保以管理员身份启动 PowerShell 或命令提示符。On Windows, verify that you are launching PowerShell or your command-prompt as an Administrator.

SQL Server 容器启动错误SQL Server container startup errors

如果 SQL Server 容器无法运行,请尝试以下测试:If the SQL Server container fails to run, try the following tests:

  • 如果遇到 “无法在网桥上创建终结点 CONTAINER_NAME。启动代理时出错: 侦听 tcp 0.0.0.0:1433 绑定: 地址已被使用。” 等错误,这表示你正尝试将容器端口 1433 映射到某个正在使用的端口。If you get an error such as 'failed to create endpoint CONTAINER_NAME on network bridge. Error starting proxy: listen tcp 0.0.0.0:1433 bind: address already in use.', then you are attempting to map the container port 1433 to a port that is already in use. 在主机上本地运行 SQL Server 时可能发生此错误。This can happen if you're running SQL Server locally on the host machine. 如果启动两个 SQL Server 容器,并尝试将它们映射到同一主机端口,也可能发生此错误。It can also happen if you start two SQL Server containers and try to map them both to the same host port. 如果发生此错误,请使用 -p 参数将容器端口 1433 映射到不同的主机端口。If this happens, use the -p parameter to map the container port 1433 to a different host port. 例如:For example:
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1400:1433 -d mcr.microsoft.com/mssql/server:2017-latest`.
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1400:1433 -d mcr.microsoft.com/mssql/server:2017-latest`.
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1400:1433 -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04`.
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1400:1433 -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04`.
  • 如果在尝试启动容器时遇到 “在 unix:///var/run/docker.sock 尝试连接到 Docker 守护程序套接字时获取权限被拒绝:获取 http://%2Fvar%2Frun%2Fdocker.sock/v1.30tdout=1&tail=all: dial unix /var/run/docker.sock: connect: 权限被拒绝” 等错误,请将用户添加到 Ubuntu 中的 docker 组。If you get an error such as 'Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.30tdout=1&tail=all: dial unix /var/run/docker.sock: connect: permission denied' when trying to start a container, then add your user to the docker group in Ubuntu. 然后注销并重新登录,因为此更改将影响新会话。Then logout and login again as this change will affect new sessions.

      usermod -aG docker $USER
    
  • 检查是否有任何来自容器的错误消息。Check to see if there are any error messages from container.

    docker logs e69e056c702d
    
  • 确保符合快速入门文章的先决条件部分中指定的最低内存和磁盘要求。Make sure that you meet the minimum memory and disk requirements specified in the prerequisites section of the quickstart article.

  • 如果使用任何容器管理软件,请确保它支持以根身份运行的容器进程。If you are using any container management software, make sure it supports container processes running as root. 容器中的 sqlservr 进程以根身份运行。The sqlservr process in the container runs as root.

  • 查看 SQL Server 安装和错误日志Review the SQL Server setup and error logs.

启用转储捕获Enable dump captures

如果 SQL Server 进程在容器内失败,则应创建一个启用了 SYS_PTRACE 的新容器。If the SQL Server process is failing inside the container, you should create a new container with SYS_PTRACE enabled. 此操作可添加用于跟踪进程的 Linux 功能,该功能是在出现异常时创建转储文件所必需的。This adds the Linux capability to trace a process, which is necessary for creating a dump file on an exception. 支持人员可以使用转储文件来帮助对问题进行故障排除。The dump file can be used by support to help troubleshoot the problem. 以下 docker run 命令可启用此功能。The following docker run command enables this capability.

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04

SQL Server 连接故障SQL Server connection failures

如果无法连接到在容器中运行的 SQL Server 实例,请尝试以下测试:If you can't connect to the SQL Server instance running in your container, try the following tests:

  • 通过查看 docker ps -a 输出的“状态”列,确保 SQL Server 容器正在运行 。Make sure that your SQL Server container is running by looking at the STATUS column of the docker ps -a output. 如果未在运行,则使用 docker start <Container ID> 启动它。If not, use docker start <Container ID> to start it.

  • 如果映射到非默认主机端口(非 1433),请确保在连接字符串中指定端口。If you mapped to a non-default host port (not 1433), make sure you are specifying the port in your connection string. 可在 docker ps -a 输出的“端口”列中查看端口映射 。You can see your port mapping in the PORTS column of the docker ps -a output. 例如,以下命令将 sqlcmd 连接至正在侦听端口 1401 的容器:For example, the following command connects sqlcmd to a container listening on port 1401:

    sqlcmd -S 10.3.2.4,1401 -U SA -P '<YourPassword>'
    
    sqlcmd -S 10.3.2.4,1401 -U SA -P "<YourPassword>"
    
  • 如果将 docker run 和现有的映射数据卷或数据卷容器配合使用,SQL Server 会忽略 MSSQL_SA_PASSWORD 的值。If you used docker run with an existing mapped data volume or data volume container, SQL Server ignores the value of MSSQL_SA_PASSWORD. 改为使用来自 SQL Server 数据(数据卷或数据卷容器中)的预配置 SA 用户密码。Instead, the pre-configured SA user password is used from the SQL Server data in the data volume or data volume container. 验证所使用的 SA 密码是否与附加到的数据相关联。Verify that you are using the SA password associated with the data you're attaching to.

  • 查看 SQL Server 安装和错误日志Review the SQL Server setup and error logs.

SQL Server 可用性组SQL Server Availability Groups

如果将 Docker 与 SQL Server 可用性组配合使用,则还有两个附加要求。If you are using Docker with SQL Server Availability Groups, there are two additional requirements.

  • 映射用于副本通信的端口(默认为 5022)。Map the port that is used for replica communication (default 5022). 例如,将 -p 5022:5022 指定为 docker run 命令的一部分。For example, specify -p 5022:5022 as part of your docker run command.

  • 使用 docker run 命令的 -h YOURHOSTNAME 参数显式设置容器主机名。Explicitly set the container host name with the -h YOURHOSTNAME parameter of the docker run command. 配置可用性组时会使用此主机名。This host name is used when you configure your Availability Group. 如果不使用 -h 来指定它,它会默认使用容器 ID。If you don't specify it with -h, it defaults to the container ID.

SQL Server 安装和错误日志SQL Server setup and error logs

可在 /var/opt/mssql/log 中查看 SQL Server 安装和错误日志。You can look at the SQL Server setup and error logs in /var/opt/mssql/log. 如果容器未在运行,请先启动它。If the container is not running, first start the container. 然后使用交互式命令提示符来检查日志。Then use an interactive command-prompt to inspect the logs.

docker start e69e056c702d
docker exec -it e69e056c702d "bash"

从容器内的 bash 会话运行以下命令:From the bash session inside your container, run the following commands:

cd /var/opt/mssql/log
cat setup*.log
cat errorlog

提示

如果在创建容器时将主机目录装载到了 /var/opt/mssql,可改为在主机上映射路径的 log 子目录中进行查找。If you mounted a host directory to /var/opt/mssql when you created your container, you can instead look in the log subdirectory on the mapped path on the host.

后续步骤Next steps

通过查看快速入门,开始在 Docker 上使用 SQL Server 2017 容器映像。Get started with SQL Server 2017 container images on Docker by going through the quickstart.

另请查看 mssql-docker GitHub 存储库,获取资源、反馈和已知问题。Also, see the mssql-docker GitHub repository for resources, feedback, and known issues.

探索 SQL Server 容器的高可用性Explore high availability for SQL Server containers