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

適用於: 是SQL Server (僅限 Linux) 否Azure SQL Database 否Azure Synapse Analytics (SQL DW) 否平行處理資料倉儲 APPLIES TO: yesSQL Server (Linux only) noAzure SQL Database noAzure Synapse Analytics (SQL DW) 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 run SQL Server 2019 containers, see the SQL Server 2019 version of this article.

在本快速入門中,您將使用 Docker 來提取與執行 SQL Server 2019 容器映像 mssql-serverIn this quickstart, you use Docker to pull and run the SQL Server 2019 container image, mssql-server. 然後與 sqlcmd連線來建立您的第一個資料庫並執行查詢。Then connect with sqlcmd to create your first database and run queries.

提示

本快速入門會建立 SQL Server 2019 容器。This quickstart creates SQL Server 2019 containers. 如果您想要建立 SQL Server 2017 容器,請參閱本文的 SQL Server 2017 版本。If you prefer to create SQL Server 2017 containers, see the SQL Server 2017 version of this article.

此映像包含以 Ubuntu 16.04 為基礎,在 Linux 上執行的 SQL Server。This image consists of SQL Server running on Linux based on Ubuntu 16.04. 您可於適用於 Mac/Windows 的 Docker 上將其與 Docker 引擎 1.8 以上版本搭配使用。It can be used with the Docker Engine 1.8+ on Linux or on Docker for Mac/Windows. 本快速入門將著重於在 linux 映像上使用 SQL Server。This quickstart specifically focuses on using the SQL Server on linux image. Windows 映像則不涵蓋在內,但您可於 mssql-server-windows-developer Docker Hub 頁面進一步加以了解。The Windows image is not covered, but you can learn more about it on the mssql-server-windows-developer Docker Hub page.

必要條件Prerequisites

  • 在任何支援的 Linux 發行版本或適用於 Mac/Windows 上的 Docker 安裝 Docker 引擎 1.8 以上版本。Docker Engine 1.8+ on any supported Linux distribution or Docker for Mac/Windows. 如需詳細資訊,請參閱安裝 DockerFor more information, see Install Docker.
  • Docker overlay2 儲存體驅動程式。Docker overlay2 storage driver. 這是大部分使用者的預設值。This is the default for most users. 如果您發現您未使用此儲存提供者且需要進行變更,請參閱用於設定 overlay2 的 Docker 文件中指示和警告。If you find that you are not using this storage provider and need to change, please see the instructions and warnings in the docker documentation for configuring overlay2.
  • 至少 2 GB 的磁碟空間。Minimum of 2 GB of disk space.
  • 至少 2 GB 的 RAM。Minimum of 2 GB of RAM.
  • Linux 上的 SQL Server 系統需求 System requirements for SQL Server on Linux.

提取及執行容器映像

Pull and run the container image

開始下列步驟之前,請確定您已在本文頂端選取您慣用的 Shell (Bash、PowerShell 或 cmd)。Before starting the following steps, make sure that you have selected your preferred shell (bash, PowerShell, or cmd) at the top of this article.

  1. 從 Microsoft Container Registry 提取 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
    
    docker pull mcr.microsoft.com/mssql/server:2017-latest
    

    提示

    如果您想要執行 SQL Server 2019 容器,請參閱本文的 SQL Server 2019 版本If you want to run SQL Server 2019 containers, see the SQL Server 2019 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-ubuntu)。If 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 Hub 頁面To see all available images, see the mssql-server Docker hub page.

    本文中的 Bash 命令會使用 sudoFor the bash commands in this article, sudo is used. 在 MacOS 上,可能不需要 sudoOn 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. 如需詳細資訊,請參閱 Post-installation steps for Linux (適用於 Linux 的安裝後步驟)。For more information, see Post-installation steps for Linux.

  2. 若要以 Docker 執行容器映像,您可以從 Bash 殼層 (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
    
    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 個字元,包含下列四種集合的其中三種字元:大寫字母、小寫字母、以 10 為底數的數字,以及符號。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 Developer 版本的映像。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 描述Description
    -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= " 指定您自己的強式密碼,該密碼長度至少需為 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.
    -d mcr.microsoft.com/mssql/server:2017-latest
    -d mcr.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
    
    docker ps -a
    

    您應該會看到類似下列螢幕擷取畫面的結果:You should see output similar to the following screenshot:

    Docker ps 命令輸出

  4. STATUS 欄位顯示的狀態含 Up,表示 SQL Server 正在容器中執行且接聽於 PORTS 欄位中指定的連接埠。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 容器的 STATUS 欄位顯示 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.

提取及執行容器映像

Pull and run the container image

開始下列步驟之前,請確定您已在本文頂端選取您慣用的 Shell (Bash、PowerShell 或 cmd)。Before starting the following steps, make sure that you have selected your preferred shell (bash, PowerShell, or cmd) at the top of this article.

  1. 從 Docker Hub 提取 SQL Server 2019 Linux 容器映像。Pull the SQL Server 2019 Linux container image from Docker Hub.

    sudo docker pull mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
    
    docker pull mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
    
    docker pull mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
    

    提示

    本快速入門使用 SQL Server 2019 Docker 映像。This quickstart uses the SQL Server 2019 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 容器映像。The previous command pulls the SQL Server 2019 container image based on Ubuntu. 若要改為使用以 RedHat 為基礎的容器映像,請參閱執行 RHEL 型的容器映像To instead use container images based on RedHat, see Run RHEL-based container images. 若要查看所有可用的映像,請參閱 mssql-server-linux Docker Hub 頁面To see all available images, see the mssql-server-linux Docker hub page.

    本文中的 Bash 命令會使用 sudoFor the bash commands in this article, sudo is used. 在 MacOS 上,可能不需要 sudoOn 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. 如需詳細資訊,請參閱 Post-installation steps for Linux (適用於 Linux 的安裝後步驟)。For more information, see Post-installation steps for Linux.

  2. 若要以 Docker 執行容器映像,您可以從 Bash 殼層 (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:2019-GA-ubuntu-16.04
    
    docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong@Passw0rd>" `
       -p 1433:1433 --name sql1 `
       -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
    
    docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong@Passw0rd>" `
       -p 1433:1433 --name sql1 `
       -d mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
    

    注意

    密碼應遵循 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 個字元,包含下列四種集合的其中三種字元:大寫字母、小寫字母、以 10 為底數的數字,以及符號。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 Developer 版本的容器。By default, this creates a container with the Developer edition of SQL Server 2019.

    下表提供了前述 docker run 範例的參數描述:The following table provides a description of the parameters in the previous docker run example:

    參數Parameter 描述Description
    -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= " 指定您自己的強式密碼,該密碼長度至少需為 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:2019-GA-ubuntu-16.04
    mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04 SQL Server 2019 Ubuntu Linux 容器映像。The SQL Server 2019 Ubuntu Linux container image.
  3. 若要檢視 Docker 容器,請使用 docker ps 命令。To view your Docker containers, use the docker ps command.

    sudo docker ps -a
    
    docker ps -a
    
    docker ps -a
    

    您應該會看到類似下列螢幕擷取畫面的結果:You should see output similar to the following screenshot:

    Docker ps 命令輸出

  4. STATUS 欄位顯示的狀態含 Up,表示 SQL Server 正在容器中執行且接聽於 PORTS 欄位中指定的連接埠。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 容器的 STATUS 欄位顯示 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 $SA_PASSWORD,即可探索您指定的 SA_PASSWORD 環境變數。After creating your SQL Server container, the SA_PASSWORD environment variable you specified is discoverable by running echo $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> 取代為您自己的密碼值。In the following example, replace the old password, <YourStrong!Passw0rd>, and the new password, <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>'"
    
    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 殼層。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"
    
    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. 鍵入 GO 以執行上述命令:Type GO to execute the previous commands:

    GO
    

選取資料Select data

現在,執行查詢以從 Inventory 資料表傳回資料。Now, run a query to return data from the Inventory table.

  1. sqlcmd 命令提示字元,輸入查詢以從 Inventory 資料表傳回 quantity (數量) 大於 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 殼層後,容器會繼續執行。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 principles 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 工具。For this example, install the sqlcmd tool on your client machine. 如需詳細資訊,請參閱在 Windows 上安裝 sqlcmd在 Linux 上安裝 sqlcmdFor more information, see Install sqlcmd on Windows or Install sqlcmd on Linux.

  3. 執行 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 <ip_address>,1433 -U SA -P "<YourNewStrong@Passw0rd>"
    
    sqlcmd -S <ip_address>,1433 -U SA -P "<YourNewStrong@Passw0rd>"
    
    sqlcmd -S <ip_address>,1433 -U SA -P "<YourNewStrong@Passw0rd>"
    
  4. 執行 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
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.

Dock 示範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.