在 Docker 上設定 SQL Server 容器映像Configure SQL Server container images on 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 設定和使用 mssql-server-linux 容器映像 (英文)。This article explains how to configure and use the mssql-server-linux container image with Docker.

如需了解其他部署案例,請參閱:For other deployment scenarios, see:

此映像包含以 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.

注意

此文章特別著重於 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.

這段 6 分鐘的影片會介紹如何在容器上執行 SQL Server:This 6-minute video provides an introduction into running SQL Server on containers:

提取及執行容器映像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-CU1-rhel-8Change the container repository from mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04 to mcr.microsoft.com/mssql/rhel/server:2019-CU1-rhel-8 in all of your docker commands.

例如,下列命令會為使用 RHEL 8 的 SQL Server 2019 容器提取累積更新 1:For example, the following command pulls the Cumulative Update 1 for SQL Server 2019 container that uses RHEL 8:

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

執行生產容器映像Run production container images

上一節中的快速入門會從 Docker Hub 執行免費的 SQL Server 開發人員版本。The quickstart in the previous section runs the free Developer edition of SQL Server from Docker Hub. 如果您想要執行生產容器映像 (例如 Enterprise、Standard 或 Web 版本),大部分的資訊仍然適用。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.

  • 開發人員容器映像也可以設定為執行生產版本。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 殼層。Use the docker exec -it command to start an interactive bash shell inside your running container. 在下列範例中,e69e056c702d 是容器識別碼。In the following example e69e056c702d is the container ID.

    docker exec -it e69e056c702d "bash"
    

    提示

    您不一定要指定整個容器識別碼。您只需指定足夠的字元來唯一識別它。You don't always have to specify the entire container id. You only have to specify enough characters to uniquely identify it. 因此,在此範例中,使用 e6e69 可能就已足夠,而不需使用完整識別碼。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 殼層後,容器會繼續執行。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 容器,並將它們對應至主機電腦上的連接埠 14011402The 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 容器,並將容器對應至主機電腦上的連接埠 14011402The 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 和 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 的內部進行示範: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

您的 SQL Server 設定變更和資料庫檔案都會保存於容器中,即使您使用 docker stopdocker start 來將容器重新啟動也一樣。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>/data:/var/opt/mssql/data -v <host directory>/log:/var/opt/mssql/log -v <host directory>/secrets:/var/opt/mssql/secrets -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>/data:/var/opt/mssql/data -v <host directory>/log:/var/opt/mssql/log -v <host directory>/secrets:/var/opt/mssql/secrets -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>/data:/var/opt/mssql/data -v <host directory>/log:/var/opt/mssql/log -v <host directory>/secrets:/var/opt/mssql/secrets -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>/data:/var/opt/mssql/data -v <host directory>/log:/var/opt/mssql/log -v <host directory>/secrets:/var/opt/mssql/secrets -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.

重要

Windows 上的 Docker 的主機磁碟區對應,目前不支援對應完整 /var/opt/mssql 目錄。Host volume mapping for Docker on Windows does not currently support mapping the complete /var/opt/mssql directory. 不過,您可以將子目錄 (例如 /var/opt/mssql/data) 對應到您的主機電腦。However, you can map a subdirectory, such as /var/opt/mssql/data to your host machine.

重要

目前不支援使用 Linux 上的 SQL Server 映像,針對 Mac 上的 Docker 進行主機磁碟區對應。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 搭配運作。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.

若要取得容器識別碼,請執行: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 映像,請以 rc1 取代下列命令中的 <image_tag>For 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>Replace <Container ID or name> with the target container ID or name. 以用來進行 SA 登入的 SQL Server 密碼取代 <YourStrong!Passw0rd>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 容器會自動以非根身分啟動,因此下列步驟僅適用於預設以 root 身分啟動的 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

注意

--cap-add SYS_PTRACE 旗標是非根 SQL Server 容器產生傾印以用於疑難排解用途的必要項目。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 exec 進入容器。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.

chgrp -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。啟動 Proxy 時發生錯誤: listen tcp 0.0.0.0:1433 bind: 位址已在使用中。」 ,則您正嘗試將容器連接埠 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 daemon 通訊端時發生權限遭拒:取得 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.

  • 如果您使用任何容器管理軟體,請確定它支援以 root 身分執行容器程序。If you are using any container management software, make sure it supports container processes running as root. 容器中的 sqlservr 程序會以 root 身分執行。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 來指定它,則會預設為容器識別碼。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,則可改為查看主機中對應路徑上的記錄子目錄。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