使用 Docker 运行 SQL Server 2017 容器映像Run the SQL Server 2017 container image with Docker

本主题适用于: 是SQL Server (仅限 Linux)没有Azure SQL 数据库没有Azure SQL 数据仓库没有并行数据仓库THIS TOPIC APPLIES TO: yesSQL Server (Linux only)noAzure SQL DatabasenoAzure SQL Data WarehousenoParallel 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.

此映像包含在 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-服务器-linux映像。This quick start 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-developer Docker Hub page.

PrerequisitesPrerequisites

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

  1. 从 Docker Hub 中拉出 SQL Server 2017 Linux 容器映像。Pull the SQL Server 2017 Linux container image from Docker Hub.

    sudo docker pull microsoft/mssql-server-linux:2017-latest
    
    docker pull microsoft/mssql-server-linux:2017-latest
    

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

  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 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' \
       -p 1401:1433 --name sql1 \
       -d microsoft/mssql-server-linux:2017-latest
    
    docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" `
       -p 1401:1433 --name sql1 `
       -d microsoft/mssql-server-linux:2017-latest
    

    备注

    默认情况下,这将创建一个容器的 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 MSSQL_SA_PASSWORD =<YourStrong !Passw0rd>-e 'MSSQL_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 1401:1433-p 1401: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 容器中,并且这公开给端口 1401,主机上。In this example, SQL Server is listening on TCP 1433 in the container and this is exposed to the port, 1401, on the host.
    -名称 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.
    microsoft/mssql-server-linux:2017-latestmicrosoft/mssql-server-linux: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 服务器容器显示的列Exited,请参阅故障排除部分中的配置指南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 Server 实例在 Docker 上从任何外部的 Linux、 Windows 或 macOS 工具支持 SQL 连接。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. 运行指定的 IP 地址和端口映射到容器中的端口 1433年的 sqlcmd。Run sqlcmd specifying the IP address and the port mapped to port 1433 in your container. 在此示例中,这是端口 1401年主机计算机上。In this example, that is port 1401 on the host machine.

    sqlcmd -S 10.3.2.4,1401 -U SA -P '<YourNewStrong!Passw0rd>'
    
    sqlcmd -S 10.3.2.4,1401 -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 2017 容器映像To explore other scenarios, such as running multiple containers, data persistence, and troubleshooting, see Configure SQL Server 2017 container images on Docker.

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