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

适用于: 是SQL Server (仅限 Linux)没有Azure SQL 数据库没有Azure SQL 数据仓库没有并行数据仓库 APPLIES TO: yesSQL Server (Linux only) noAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

此文章介绍了如何配置和使用mssql server linux 容器映像使用 Docker。This article explains how to configure and use the mssql-server-linux container image with Docker. 此映像包含在 Linux(基于 Ubuntu 16.04)上运行的 SQL Server。This image consists of SQL Server running on Linux based on Ubuntu 16.04. 它可与适用于 Linux 的 Docker 引擎 1.8 以上版本或适用于 Mac/Windows 的 Docker 配合使用。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 中心页The Windows image is not covered, but you can learn more about it on the mssql-server-windows Docker Hub page.

请求和运行容器映像Pull and run the container image

若要提取并运行 Docker 容器映像进行 SQL Server 2017 和 SQL Server 2019 CTP 2.0,请执行的先决条件和以下快速入门中的步骤:To pull and run the Docker container images for SQL Server 2017 and SQL Server 2019 CTP 2.0, 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 的容器。All of the documentation on SQL Server Linux container images point to Ubuntu-based containers. 从 SQL Server 2019 CTP 2.0 开始,可以使用基于 Red Hat Enterprise Linux (RHEL) 上的容器。Beginning with SQL Server 2019 CTP 2.0, you can use containers based on Red Hat Enterprise Linux (RHEL). 更改从容器存储库mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntumcr.microsoft.com/mssql/rhel/server:vNext-CTP2.0中的所有 docker 命令。Change the container repository from mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu to mcr.microsoft.com/mssql/rhel/server:vNext-CTP2.0 in all of your docker commands.

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

sudo docker pull mcr.microsoft.com/mssql/rhel/server:vNext-CTP2.0
docker pull mcr.microsoft.com/mssql/rhel/server:vNext-CTP2.0

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

在快速入门中上一节从 Docker 中心运行 SQL Server 免费的 Developer 的 edition。The quickstart in the previous section runs the free Developer edition of SQL Server from Docker Hub. 如果你想要运行生产容器映像,如 Enterprise、 Standard edition 或 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 edition 和 Enterprise Edition 许可证是可通过Microsoft 批量许可SQL Server Standard and Enterprise Edition licenses are available through Microsoft Volume Licensing.

  • 必须从请求生产 SQL Server 容器映像Docker 存储Production SQL Server container images must be pulled from Docker Store. 如果你还没有一个 Docker 存储上创建帐户。If you don't already have one, create an account on Docker Store.

  • Docker 应用商店上的开发人员容器映像可以配置为运行生产版。The Developer container image on Docker Store can be configured to run the production editions as well. 使用以下步骤来运行生产版:Use the following steps to run production editions:

    1. 首先,登录到 docker id 从命令行。First, log in to your docker id from the command line.

      docker login
      
    2. 接下来,您需要获取免费的开发人员在 Docker Store 上的容器映像。Next, you need to obtain the free Developer container image on Docker Store. 转到 https://store.docker.com/images/mssql-server-linux ,单击继续签出,按照说明进行操作。Go to https://store.docker.com/images/mssql-server-linux, click Proceed to Checkout, and follow the instructions.

    3. 查看要求,并运行过程快速入门Review the requirements and run procedures in the quickstart. 但是,有两个差异。But there are two differences. 必须拉取映像应用商店/microsoft/mssql server linux:<标记名称> Docker 存储区中。You must pull the image store/microsoft/mssql-server-linux:<tag-name> from Docker Store. 则必须指定你使用的生产版MSSQL_PID环境变量。And 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 store/microsoft/mssql-server-linux:2017-latest
      
      docker run --name sqlenterprise `
         -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" `
         -e "MSSQL_PID=Enterprise" -p 1433:1433 `
         -d "store/microsoft/mssql-server-linux: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. 例如,如果您指定-p 1400:1433在您docker run命令,然后显式连接指定端口 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 CTP 2.0 开始SQL Server 命令行工具包含在容器映像。Starting with SQL Server 2017 CTP 2.0, 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 实例的方案提供了解决办法。This is the 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 容器,并将它们映射到端口1401年1402年主机计算机上。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 CTP 2.0 容器,并将它们映射到端口1401年1402年主机计算机上。The following example creates two SQL Server 2019 CTP 2.0 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:vNext-CTP2.0-ubuntu
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1402:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1401:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1402:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu

现在,两个 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 和 Node 应用程序的演示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 中的以下命令所示:This is illustrated in the following command inside a Dockerfile:

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

如果撤消上一示例中的命令,容器将执行操作-我的 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

即使您重新启动的容器的容器中保持 SQL Server 配置更改和数据库文件的docker stopdocker startYour 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:vNext-CTP2.0-ubuntu
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:vNext-CTP2.0-ubuntu

通过此方法,还能共享和查看位于主机上、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. 下面的示例创建名为的共享的数据卷sqlvolumeThe 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:vNext-CTP2.0-ubuntu
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:vNext-CTP2.0-ubuntu

备注

早期版本的 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. 有关详细信息,请参阅SQL Server 备份和还原 Linux 上的数据库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 -ti <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

运行特定的 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 中心页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'

此外可以确定 SQL Server 版本和内部版本号为目标的 docker 容器映像。You can also identify the SQL Server version and build number for a target docker container image. 下面的命令显示的 SQL Server 版本和内部版本信息microsoft/mssql-server-linux:2017-最新映像。The following command displays the SQL Server version and build information for the microsoft/mssql-server-linux:2017-latest image. 这是通过使用环境变量运行新的容器PAL_PROGRAM_INFO = 1It 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 Server 升级Upgrade 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. 停止 SQL Server 容器与docker stop命令。Stop the SQL Server container with the docker stop command.

  3. 创建新的 SQL Server 容器与docker run并指定一个映射的主机目录或数据卷容器。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 rmOptionally, remove the old container with docker rm.

故障排除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 上收到此错误,请尝试运行相同命令加sudoIf 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:
```bash
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1400:1433 -d mcr.microsoft.com/mssql/server:2017-latest`.
```

```PowerShell
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1400:1433 -d mcr.microsoft.com/mssql/server:2017-latest`.
```
```bash
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1400:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu`.
```

```PowerShell
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1400:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu`.
```
  • 检查是否有任何来自容器的错误消息。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 Requirements section of this 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:vNext-CTP2.0-ubuntu

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:

  • 请确保 SQL Server 容器正在运行通过查看状态docker ps -a输出。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_PASSWORDIf 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.

  • 显式设置在容器主机名-h YOURHOSTNAME参数的docker run命令。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

您可以查看 SQL Server 安装程序和错误日志 /var/opt/mssql/logYou 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创建容器时,您可以改为查看日志映射的路径在主机上的子目录。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