使用 Docker 快速入门: 运行 SQL Server 容器映像Quickstart: Run SQL Server container images with Docker

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

在本快速入门教程中,你使用 Docker 请求和运行 SQL Server 2017 容器映像 mssql server linuxIn this quickstart, you use Docker to pull and run the SQL Server 2017 container image, mssql-server-linux. 然后使用 sqlcmd 进行连接,以便创建第一个数据库并运行查询。Then connect with sqlcmd to create your first database and run queries.

提示

如果你想要试用 SQL Server 2019 预览图像,请参阅这篇文章的 SQL Server 2019 预览版本If you want to try the SQL Server 2019 preview image, see the SQL Server 2019 preview version of this article.

在本快速入门中使用 Docker 请求和运行 SQL Server 2019 预览版容器映像, mssql serverIn this quickstart, you use Docker to pull and run the SQL Server 2019 preview container image, mssql-server. 然后使用 sqlcmd 进行连接,以便创建第一个数据库并运行查询。Then connect with sqlcmd to create your first database and run queries.

此映像包含在 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. 本快速入门专门重点介绍使用 SQL Server 上linux映像。This quick start specifically focuses on using the SQL Server on linux image. 虽然未介绍 Windows 映像,但可在 mssql-server-windows-developer Docker 中心页上找到关于它的详细信息。The Windows image is not covered, but you can learn more about it on the mssql-server-windows-developer Docker Hub page.

先决条件Prerequisites

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

  1. 从 Microsoft 容器注册表拉取 SQL Server 2017 Linux 容器映像。Pull the SQL Server 2017 Linux container image from Microsoft Container Registry.

    sudo docker pull mcr.microsoft.com/mssql/server:2017-latest
    
    docker pull mcr.microsoft.com/mssql/server:2017-latest
    

    提示

    如果你想要试用 SQL Server 2019 预览图像,请参阅这篇文章的 SQL Server 2019 预览版本If you want to try the SQL Server 2019 preview image, see the SQL Server 2019 preview version of this article.

    前一个命令请求最新的 SQL Server 2017 容器映像。The previous command pulls the latest SQL Server 2017 container image. 如果想请求某个特定映像,需添加一个冒号和标记名称(例如 mcr.microsoft.com/mssql/server:2017-GA-ubuntuIf you want to pull a specific image, you add a colon and the tag name (for example, mcr.microsoft.com/mssql/server:2017-GA-ubuntu). 若要查看所有可用映像,请参阅mssql server Docker 中心页To see all available images, see the mssql-server Docker hub page.

    在本文中,bash 命令sudo使用。For the bash commands in this article, sudo is used. 在 MacOS 上,sudo可能不需要。On MacOS, sudo might not be required. 在 Linux 上,如果不想要使用sudo若要运行 Docker,可以配置docker组,并将用户添加到该组。On Linux, if you do not want to use sudo to run Docker, you can configure a docker group and add users to that group. 有关详细信息,请参阅安装后步骤适用于 LinuxFor more information, see Post-installation steps for Linux.

  2. 要使用 Docker 运行容器映像,可以从 Bash Shell (Linux/macOS) 或提升的 PowerShell 命令提示符使用以下命令。To run the container image with Docker, you can use the following command from a bash shell (Linux/macOS) or elevated PowerShell command prompt.

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

    备注

    密码应符合 SQL Server 默认密码策略,否则容器无法设置 SQL Server,将停止工作。The password should follow the SQL Server default password policy, otherwise the container can not setup SQL server and will stop working. 默认情况下,密码必须为至少 8 个字符且包含以下四种字符中的三种:大写字母、小写字母、十进制数字、符号。By default, the password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols. 你可以通过执行 docker logs 命令检查错误日志。You can examine the error log by executing the docker logs command.

    备注

    默认情况下,这会创建一个使用 SQL Server 2017 开发人员版的容器。By default, this creates a container with the Developer edition of SQL Server 2017. 在容器中运行生产版本的过程略有不同。The process for running production editions in containers is slightly different. 有关详细信息,请参阅运行生产容器映像For more information, see Run production container images.

    下表对前一个 docker run 示例中的参数进行了说明:The following table provides a description of the parameters in the previous docker run example:

    参数Parameter DescriptionDescription
    -e 'ACCEPT_EULA=Y'-e 'ACCEPT_EULA=Y' ACCEPT_EULA 变量设置为任意值,以确认接受最终用户许可协议Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. SQL Server 映像的必需设置。Required setting for the SQL Server image.
    -e SA_PASSWORD =<YourStrong !Passw0rd>-e 'SA_PASSWORD=<YourStrong!Passw0rd>' 指定至少包含 8 个字符且符合 SQL Server 密码要求的强密码。Specify your own strong password that is at least 8 characters and meets the SQL Server password requirements. SQL Server 映像的必需设置。Required setting for the SQL Server image.
    -p 1433:1433-p 1433:1433 建立主机环境(第一个值)上的 TCP 端口与容器(第二个值)中 TCP 端口的映射。Map a TCP port on the host environment (first value) with a TCP port in the container (second value). 在此示例中,SQL Server 侦听容器中的 TCP 1433 并公开的端口 1433,在主机上。In this example, SQL Server is listening on TCP 1433 in the container and this is exposed to the port, 1433, on the host.
    --name sql1--name sql1 为容器指定一个自定义名称,而不是使用随机生成的名称。Specify a custom name for the container rather than a randomly generated one. 如果运行多个容器,则无法重复使用相同的名称。If you run more than one container, you cannot reuse this same name.
    mcr.microsoft.com/mssql/server:2017-latestmcr.microsoft.com/mssql/server:2017-latest SQL Server 2017 Linux 容器映像。The SQL Server 2017 Linux container image.
  3. 要查看 Docker 容器,请使用 docker ps 命令。To view your Docker containers, use the docker ps command.

    sudo docker ps -a
    
    docker ps -a
    

    将看到与如下屏幕截图相似的输出:You should see output similar to the following screenshot:

    Docker ps 命令输出

  4. 如果“状态”列显示“正常运行”,则 SQL Server 将在容器中运行,并侦听“端口”列中指定的端口。If the STATUS column shows a status of Up, then SQL Server is running in the container and listening on the port specified in the PORTS column. 如果 SQL Server 容器的“状态”列显示“已退出”,则参阅配置指南的疑难解答部分If the STATUS column for your SQL Server container shows Exited, see the Troubleshooting section of the configuration guide.

-h(主机名)参数也非常有用,但为了简单起见,本教程中不使用它。The -h (host name) parameter is also useful, but it is not used in this tutorial for simplicity. 这会将容器的内部名称更改为一个自定义值。This changes the internal name of the container to a custom value. 也就是以下 Transact-SQL 查询中返回的名称:This is the name you'll see returned in the following Transact-SQL query:

SELECT @@SERVERNAME,
    SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),
    SERVERPROPERTY('MachineName'),
    SERVERPROPERTY('ServerName')

-h--name 设为相同的值是一种很好的方法,可以轻松地识别目标容器。Setting -h and --name to the same value is a good way to easily identify the target container.

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

  1. 从 Docker 中心请求 SQL Server 2019 CTP 2.0 Linux 容器映像。Pull the SQL Server 2019 CTP 2.0 Linux container image from Docker Hub.

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

    提示

    本快速入门使用 SQL Server 2019 CTP 2.0 Docker 映像。This quickstart uses the SQL Server 2019 CTP 2.0 Docker image. 如果你想要运行 SQL Server 2017 映像,请参阅这篇文章的 SQL Server 2017 版本If you want to run the SQL Server 2017 image, see the SQL Server 2017 version of this article.

    前一个命令请求基于 Ubuntu 的最新 SQL Server 2019 CTP 2.0 容器映像。The previous command pulls the latest SQL Server 2019 CTP 2.0 container image based on Ubuntu. 若要改为使用基于 RedHat 的容器映像,请参阅基于运行 RHEL 的容器映像To instead use container images based on RedHat, see Run RHEL-based container images. 如果想请求某个特定映像,需添加一个冒号和标记名称(例如 mcr.microsoft.com/mssql/server:2017-GAIf you want to pull a specific image, you add a colon and the tag name (for example, mcr.microsoft.com/mssql/server:2017-GA). 要查看所有可用映像,请参阅 mssql-server-linux Docker 中心页To see all available images, see the mssql-server-linux Docker hub page.

    在本文中,bash 命令sudo使用。For the bash commands in this article, sudo is used. 在 MacOS 上,sudo可能不需要。On MacOS, sudo might not be required. 在 Linux 上,如果不想要使用sudo若要运行 Docker,可以配置docker组,并将用户添加到该组。On Linux, if you do not want to use sudo to run Docker, you can configure a docker group and add users to that group. 有关详细信息,请参阅安装后步骤适用于 LinuxFor more information, see Post-installation steps for Linux.

  2. 要使用 Docker 运行容器映像,可以从 Bash Shell (Linux/macOS) 或提升的 PowerShell 命令提示符使用以下命令。To run the container image with Docker, you can use the following command from a bash shell (Linux/macOS) or elevated PowerShell command prompt.

    sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<YourStrong!Passw0rd>' \
       -p 1433:1433 --name sql1 \
       -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
    
    docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong!Passw0rd>" `
       -p 1433:1433 --name sql1 `
       -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
    

    备注

    密码应符合 SQL Server 默认密码策略,否则容器无法设置 SQL Server,将停止工作。The password should follow the SQL Server default password policy, otherwise the container can not setup SQL server and will stop working. 默认情况下,密码必须为至少 8 个字符且包含以下四种字符中的三种:大写字母、小写字母、十进制数字、符号。By default, the password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols. 你可以通过执行 docker logs 命令检查错误日志。You can examine the error log by executing the docker logs command.

    备注

    默认情况下,这与 SQL Server 2019 CTP 2.0 的开发人员版本创建一个容器。By default, this creates a container with the Developer edition of SQL Server 2019 CTP 2.0.

    下表对前一个 docker run 示例中的参数进行了说明:The following table provides a description of the parameters in the previous docker run example:

    参数Parameter DescriptionDescription
    -e 'ACCEPT_EULA=Y'-e 'ACCEPT_EULA=Y' ACCEPT_EULA 变量设置为任意值,以确认接受最终用户许可协议Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. SQL Server 映像的必需设置。Required setting for the SQL Server image.
    -e SA_PASSWORD =<YourStrong !Passw0rd>-e 'SA_PASSWORD=<YourStrong!Passw0rd>' 指定至少包含 8 个字符且符合 SQL Server 密码要求的强密码。Specify your own strong password that is at least 8 characters and meets the SQL Server password requirements. SQL Server 映像的必需设置。Required setting for the SQL Server image.
    -p 1433:1433-p 1433:1433 建立主机环境(第一个值)上的 TCP 端口与容器(第二个值)中 TCP 端口的映射。Map a TCP port on the host environment (first value) with a TCP port in the container (second value). 在此示例中,SQL Server 侦听容器中的 TCP 1433 并公开的端口 1433,在主机上。In this example, SQL Server is listening on TCP 1433 in the container and this is exposed to the port, 1433, on the host.
    --name sql1--name sql1 为容器指定一个自定义名称,而不是使用随机生成的名称。Specify a custom name for the container rather than a randomly generated one. 如果运行多个容器,则无法重复使用相同的名称。If you run more than one container, you cannot reuse this same name.
    mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntumcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu SQL Server 2019 CTP 2.0 Linux 容器映像。The SQL Server 2019 CTP 2.0 Linux container image.
  3. 要查看 Docker 容器,请使用 docker ps 命令。To view your Docker containers, use the docker ps command.

    sudo docker ps -a
    
    docker ps -a
    

    将看到与如下屏幕截图相似的输出:You should see output similar to the following screenshot:

    Docker ps 命令输出

  4. 如果“状态”列显示“正常运行”,则 SQL Server 将在容器中运行,并侦听“端口”列中指定的端口。If the STATUS column shows a status of Up, then SQL Server is running in the container and listening on the port specified in the PORTS column. 如果 SQL Server 容器的“状态”列显示“已退出”,则参阅配置指南的疑难解答部分If the STATUS column for your SQL Server container shows Exited, see the Troubleshooting section of the configuration guide.

-h(主机名)参数也非常有用,但为了简单起见,本教程中不使用它。The -h (host name) parameter is also useful, but it is not used in this tutorial for simplicity. 这会将容器的内部名称更改为一个自定义值。This changes the internal name of the container to a custom value. 也就是以下 Transact-SQL 查询中返回的名称:This is the name you'll see returned in the following Transact-SQL query:

SELECT @@SERVERNAME,
    SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),
    SERVERPROPERTY('MachineName'),
    SERVERPROPERTY('ServerName')

-h--name 设为相同的值是一种很好的方法,可以轻松地识别目标容器。Setting -h and --name to the same value is a good way to easily identify the target container.

更改 SA 密码Change the SA password

SA 帐户是安装过程中在 SQL Server 实例上创建的系统管理员。The SA account is a system administrator on the SQL Server instance that gets created during setup. 创建 SQL Server 容器后,通过在容器中运行 echo $MSSQL_SA_PASSWORD,可发现指定的 MSSQL_SA_PASSWORD 环境变量。After creating your SQL Server container, the MSSQL_SA_PASSWORD environment variable you specified is discoverable by running echo $MSSQL_SA_PASSWORD in the container. 出于安全考虑,请考虑更改 SA 密码。For security purposes, change your SA password.

  1. 选择 SA 用户要使用的强密码。Choose a strong password to use for the SA user.

  2. 使用 docker exec 运行sqlcmd,以使用 Transact-SQL 更改密码。Use docker exec to run sqlcmd to change the password using Transact-SQL. <YourStrong!Passw0rd><YourNewStrong!Passw0rd> 替换为自己的密码值。Replace <YourStrong!Passw0rd> and <YourNewStrong!Passw0rd> with your own password values.

    sudo docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd \
       -S localhost -U SA -P '<YourStrong!Passw0rd>' \
       -Q 'ALTER LOGIN SA WITH PASSWORD="<YourNewStrong!Passw0rd>"'
    
    docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd `
       -S localhost -U SA -P "<YourStrong!Passw0rd>" `
       -Q "ALTER LOGIN SA WITH PASSWORD='<YourNewStrong!Passw0rd>'"
    

连接到 SQL ServerConnect to SQL Server

下列步骤在容器内部使用 SQL Server 命令行工具 sqlcmd 来连接 SQL Server。The following steps use the SQL Server command-line tool, sqlcmd, inside the container to connect to SQL Server.

  1. 使用 docker exec -it 命令在运行的容器内部启动交互式 Bash Shell。Use the docker exec -it command to start an interactive bash shell inside your running container. 在下面的示例中,sql1 是在创建容器时由 --name 参数指定的名称。In the following example sql1 is name specified by the --name parameter when you created the container.

    sudo docker exec -it sql1 "bash"
    
    docker exec -it sql1 "bash"
    
  2. 一旦位于容器内部,使用 sqlcmd 进行本地连接。Once inside the container, connect locally with sqlcmd. 默认情况下,sqlcmd 不在路径之中,因此需要指定完整路径。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 '<YourNewStrong!Passw0rd>'
    

    提示

    可以省略命令行上提示要输入的密码。You can omit the password on the command-line to be prompted to enter it.

  3. 如果成功,应会显示 sqlcmd 命令提示符:1>If successful, you should get to a sqlcmd command prompt: 1>.

创建和查询数据Create and query data

以下部分将引导你使用 sqlcmd 和 Transact-SQL 完成新建数据库、添加数据并运行简单查询的整个过程。The following sections walk you through using sqlcmd and Transact-SQL to create a new database, add data, and run a simple query.

新建数据库Create a new database

以下步骤创建一个名为 TestDB 的新数据库。The following steps create a new database named TestDB.

  1. 在 sqlcmd 命令提示符中,粘贴以下 Transact-SQL 命令以创建测试数据库:From the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:

    CREATE DATABASE TestDB
    
  2. 在下一行中,编写一个查询以返回服务器上所有数据库的名称:On the next line, write a query to return the name of all of the databases on your server:

    SELECT Name from sys.Databases
    
  3. 前两个命令没有立即执行。The previous two commands were not executed immediately. 必须在新行中键入 GO 才能执行以前的命令:You must type GO on a new line to execute the previous commands:

    GO
    

插入数据Insert data

接下来创建一个新表 Inventory,然后插入两个新行。Next create a new table, Inventory, and insert two new rows.

  1. 在 sqlcmd 命令提示符中,将上下文切换到新的 TestDB 数据库:From the sqlcmd command prompt, switch context to the new TestDB database:

    USE TestDB
    
  2. 创建名为 Inventory 的新表:Create new table named Inventory:

    CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
    
  3. 将数据插入新表:Insert data into the new table:

    INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
    
  4. 要执行上述命令的类型 GOType GO to execute the previous commands:

    GO
    

选择数据Select data

现在,运行查询以从 Inventory 表返回数据。Now, run a query to return data from the Inventory table.

  1. 通过 sqlcmd 命令提示符输入查询,以返回 Inventory 表中数量大于 152 的行:From the sqlcmd command prompt, enter a query that returns rows from the Inventory table where the quantity is greater than 152:

    SELECT * FROM Inventory WHERE quantity > 152;
    
  2. 执行命令:Execute the command:

    GO
    

退出 sqlcmd 命令提示符Exit the sqlcmd command prompt

  1. 要结束 sqlcmd 会话,请键入 QUITTo end your sqlcmd session, type QUIT:

    QUIT
    
  2. 要在容器中退出交互式命令提示,请键入 exitTo exit the interactive command-prompt in your container, type exit. 退出交互式 Bash Shell 后,容器将继续运行。Your container continues to run after you exit the interactive bash shell.

从容器外连接Connect from outside the container

还可以从支持 SQL 连接的任何 Linux、Windows 或 macOS 外部工具连接到 Docker 计算机上的 SQL Server 实例。You can also connect to the SQL Server instance on your Docker machine from any external Linux, Windows, or macOS tool that supports SQL connections.

以下步骤在容器外使用 sqlcmd 连接在容器中运行的 SQL Server。The following steps use sqlcmd outside of your container to connect to SQL Server running in the container. 这些步骤假定你已在容器外安装了 SQL Server 命令行工具。These steps assume that you already have the SQL Server command-line tools installed outside of your container. 使用其他工具时,同样的原则依然适用,但连接过程因工具而异。The same principals apply when using other tools, but the process of connecting is unique to each tool.

  1. 查找承载容器的计算机的 IP 地址。Find the IP address for the machine that hosts your container. 在 Linux 上,使用 ifconfigip addr。在 Windows 上,使用 ipconfigOn Linux, use ifconfig or ip addr. On Windows, use ipconfig.

  2. 运行 sqlcmd,指定 IP 地址和映射容器中的端口 1433 的端口。Run sqlcmd specifying the IP address and the port mapped to port 1433 in your container. 在此示例中,这是同一个端口,1433,在主机上。In this example, that is the same port, 1433, on the host machine. 如果主机计算机上指定其他映射的端口,你将在此处使用它。If you specified a different mapped port on the host machine, you would use it here.

    sqlcmd -S 10.3.2.4,1433 -U SA -P '<YourNewStrong!Passw0rd>'
    
    sqlcmd -S 10.3.2.4,1433 -U SA -P "<YourNewStrong!Passw0rd>"
    
  3. 运行 Transact-SQL 命令。Run Transact-SQL commands. 完成后,键入 QUITWhen finished, type QUIT.

连接到 SQL Server 的其他常见工具包括:Other common tools to connect to SQL Server include:

删除容器Remove your container

如果想删除本教程中使用的 SQL Server 容器,请运行以下命令:If you want to remove the SQL Server container used in this tutorial, run the following commands:

sudo docker stop sql1
sudo docker rm sql1
docker stop sql1
docker rm sql1

警告

停止并永久删除容器会删除容器中的所有 SQL Server 数据。Stopping and removing a container permanently deletes any SQL Server data in the container. 如果你需要保留数据,请在容器外创建并复制备份文件或使用容器数据暂留技术If you need to preserve your data, create and copy a backup file out of the container or use a container data persistence technique.

Docker 演示Docker demo

尝试对 Docker 使用 SQL Server 容器映像后,你可能想知道如何 Docker 是如何用于改进开发和测试的。After you have tried using the SQL Server container image for Docker, you might want to know how Docker is used to improve development and testing. 下面的视频介绍如何在持续集成和部署方案中使用 Docker。The following video shows how Docker can be used in a continuous integration and deployment scenario.

后续步骤Next steps

有关如何将数据库备份文件还原到容器中的教程,请参阅在 Linux Docker 容器中还原 SQL Server 数据库For a tutorial on how to restore database backup files into a container, see Restore a SQL Server database in a Linux Docker container. 若要浏览其他方案,例如运行多个容器数据暂留和故障排除,请参阅Docker 上的配置 SQL Server 容器映像To explore other scenarios, such as running multiple containers, data persistence, and troubleshooting, see Configure SQL Server container images on Docker.

并且,请查看 mssql-docker GitHub 存储库,了解资源、反馈和已知问题。Also, check out the mssql-docker GitHub repository for resources, feedback, and known issues.