在 Docker 上設定 SQL Server 容器映像Configure SQL Server container images on Docker

適用於: yesSQL Server (僅限 Linux)沒有Azure SQL Database沒有Azure SQL 資料倉儲沒有平行資料倉儲 APPLIES TO: yesSQL Server (Linux only) noAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

這篇文章說明如何設定和使用mssql server linux 容器映像使用 Docker。This article explains how to configure and use the mssql-server-linux container image with Docker. 此映像包含以 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.

提取及執行容器映像Pull and run the container image

提取及執行 Docker 容器映像,以使用 SQL Server 2017 和 SQL Server 2019 的預覽,請依照下列的必要條件和下列快速入門中的步驟:To pull and run the Docker container images for SQL Server 2017 and SQL Server 2019 preview, follow the prerequisites and steps in the following quickstart:

設定本文下列各節中的其他使用案例。This configuration article provides additional usage scenarios in the following sections.

執行 RHEL 為基礎的容器映像Run RHEL-based container images

所有 SQL Server Linux 容器映像上的文件會指向以 Ubuntu 為基礎的容器。All of the documentation on SQL Server Linux container images point to Ubuntu-based containers. 從 SQL Server 2019 preview 開始,您可以使用基礎上 Red Hat Enterprise Linux (RHEL) 的容器。Beginning with SQL Server 2019 preview, you can use containers based on Red Hat Enterprise Linux (RHEL). 變更的容器存放庫mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntumcr.microsoft.com/mssql/rhel/server:vNext-CTP2.0中所有的 docker 命令。Change the container repository from mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu to mcr.microsoft.com/mssql/rhel/server:vNext-CTP2.0 in all of your docker commands.

例如,下列命令會提取最新的 SQL Server 2019 預覽容器,使用 RHEL:For example, the following command pulls the latest SQL Server 2019 preview container that uses RHEL:

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

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

快速入門中的上一節中會執行免費的 SQL Server 的開發人員版本,從 Docker Hub。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.

  • 實際執行 SQL Server 容器映像必須取自Docker 存放區Production SQL Server container images must be pulled from Docker Store. 如果您還沒有一個 Docker 存放區上建立帳戶。If you don't already have one, create an account on Docker Store.

  • 若要執行的實際執行版本,可以設定 Docker 存放區上的開發人員容器映像。The Developer container image on Docker Store can be configured to run the production editions as well. 您可以使用下列步驟來執行生產版本:Use the following steps to run production editions:

    1. 首先,登入您的 docker 識別碼從命令列。First, log in to your docker id from the command line.

      docker login
      
    2. 接下來,您需要取得免費的開發人員 Docker 存放區上的容器映像。Next, you need to obtain the free Developer container image on Docker Store. 移至https://store.docker.com/images/mssql-server-linux,按一下 繼續簽出,並遵循指示。Go to https://store.docker.com/images/mssql-server-linux, click Proceed to Checkout, and follow the instructions.

    3. 檢閱需求,並執行程序快速入門Review the requirements and run procedures in the quickstart. 但是,有兩個差異。But there are two differences. 您必須提取映像存放區/microsoft/mssql-server-linux:<標記名稱> 從 Docker 存放區。You must pull the image store/microsoft/mssql-server-linux:<tag-name> from Docker Store. 您必須指定您的生產版本,具有MSSQL_PID環境變數。And you must specify your production edition with the MSSQL_PID environment variable. 下列範例示範如何執行 Enterprise edition 的最新的 SQL Server 2017 容器映像:The following example shows how to run the latest SQL Server 2017 container image for the Enterprise Edition:

      docker run --name sqlenterprise \
         -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' \
         -e 'MSSQL_PID=Enterprise' -p 1433:1433 \
         -d store/microsoft/mssql-server-linux:2017-latest
      
      docker run --name sqlenterprise `
         -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" `
         -e "MSSQL_PID=Enterprise" -p 1433:1433 `
         -d "store/microsoft/mssql-server-linux:2017-latest"
      

      重要

      傳遞的值,藉以Y環境變數ACCEPT_EULA和版本值以MSSQL_PID,您要表達您有有效的和現有的授權,版本和您想要使用的 SQL Server 版本。By passing the value Y to the environment variable ACCEPT_EULA and an edition value to MSSQL_PID, you are expressing that you have a valid and existing license for the edition and version of SQL Server that you intend to use. 您也同意在 Docker 容器映像中執行的 SQL Server 軟體的使用會受到您的 SQL Server 授權條款。You also agree that your use of SQL Server software running in a Docker container image will be governed by the terms of your SQL Server license.

      注意

      如需完整的可能值清單MSSQL_PID,請參閱環境變數,在 Linux 上設定 SQL Server 設定For a full list of possible values for MSSQL_PID, see Configure SQL Server settings with environment variables on Linux.

連線及查詢Connect and query

您可以連接並查詢從外部容器,或從容器中的 SQL Server 容器。You can connect and query SQL Server in a container from either outside the container or from within the container. 下列各節會說明這兩種案例。The following sections explain both scenarios.

對容器外部的工具Tools outside the container

您可以從連線到 SQL Server 執行個體 Docker 機器上支援 SQL 連線的任何外部 Linux、 Windows 或 macOS 工具。You can connect to the SQL Server instance on your Docker machine from any external Linux, Windows, or macOS tool that supports SQL connections. 一些常見的工具包括:Some common tools include:

下列範例會使用sqlcmd連線到 Docker 容器中執行的 SQL Server。The following example uses sqlcmd to connect to SQL Server running in a Docker container. 連接字串中的 IP 位址是正在執行容器主機電腦的 IP 位址。The IP address in the connection string is the IP address of the host machine that is running the container.

sqlcmd -S 10.3.2.4 -U SA -P '<YourPassword>'
sqlcmd -S 10.3.2.4 -U SA -P "<YourPassword>"

如果您對應不是預設的主機連接埠1433,將該連接埠新增至連接字串。If you mapped a host port that was not the default 1433, add that port to the connection string. 例如,如果您指定-p 1400:1433中您docker run命令,然後明確地連接指定通訊埠 1400。For example, if you specified -p 1400:1433 in your docker run command, then connect by explicitly specify port 1400.

sqlcmd -S 10.3.2.4,1400 -U SA -P '<YourPassword>'
sqlcmd -S 10.3.2.4,1400 -U SA -P "<YourPassword>"

在容器內的工具Tools inside the container

開始使用 SQL Server 2017 preview SQL Server 命令列工具包括在容器映像。Starting with SQL Server 2017 preview, 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 執行個體的案例的方法。This is the approach for scenarios that require multiple instances of SQL Server on the same host. 每個容器必須公開本身不同的連接埠。Each container must expose itself on a different port.

下列範例會建立兩個 SQL Server 2017 容器,並將它們對應至連接埠1401年1402年主機電腦上。The following example creates two SQL Server 2017 containers and maps them to ports 1401 and 1402 on the host machine.

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1401:1433 -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1402:1433 -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1401:1433 -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1402:1433 -d mcr.microsoft.com/mssql/server:2017-latest

下列範例會建立兩個 SQL Server 2019 預覽容器,並將它們對應至連接埠1401年1402年主機電腦上。The following example creates two SQL Server 2019 preview containers and maps them to ports 1401 and 1402 on the host machine.

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1401:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1402:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1401:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1402:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu

現在有兩個不同的容器中執行的 SQL Server 執行個體。Now there are two instances of SQL Server running in separate containers. 使用容器的 Docker 主機和連接埠號碼的 IP 位址,用戶端可以連線到每個 SQL Server 執行個體。Clients can connect to each SQL Server instance by using the IP address of the Docker host and the port number for the container.

sqlcmd -S 10.3.2.4,1401 -U SA -P '<YourPassword>'
sqlcmd -S 10.3.2.4,1402 -U SA -P '<YourPassword>'
sqlcmd -S 10.3.2.4,1401 -U SA -P "<YourPassword>"
sqlcmd -S 10.3.2.4,1402 -U SA -P "<YourPassword>"

建立自訂的容器Create a customized container

您可建立您自己Dockerfile來建立自訂的 SQL Server 容器。It is possible to create your own Dockerfile to create a customized SQL Server container. 如需詳細資訊,請參閱 < 結合了 SQL Server 和 Node 應用程式的示範For more information, see a demo that combines SQL Server and a Node application. 如果您建立自己的 Dockerfile,請注意前景處理序,因為此程序可控制容器的生命週期。If you do create your own Dockerfile, be aware of the foreground process, because this process controls the life of the container. 如果結束,則容器也會關閉。If it exits, the container will shutdown. 比方說,如果您想要執行指令碼,並啟動 SQL Server,請確定 SQL Server 處理序是最右邊的命令。For example, if you want to run a script and start SQL Server, make sure that the SQL Server process is the right-most command. 所有其他命令會在背景中執行。All other commands are run in the background. 下列命令在 Dockerfile 所示:This is illustrated in the following command inside a Dockerfile:

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

如果您反轉先前的範例中的命令,容器執行-我-sql-commands.sh 指令碼完成時,會關機。If you reversed the commands in the previous example, the container would shutdown when the do-my-sql-commands.sh script completes.

保存您的資料Persist your data

您的 SQL Server 組態變更和資料庫檔案會保存在容器即使您重新啟動的容器docker stopdocker startYour SQL Server configuration changes and database files are persisted in the container even if you restart the container with docker stop and docker start. 不過,如果您移除的容器docker rm,容器中的所有項目會遭到刪除,包括 SQL Server 和資料庫。However, if you remove the container with docker rm, everything in the container is deleted, including SQL Server and your databases. 下列章節將說明如何使用的資料磁碟區保存您的資料庫檔案,即使刪除相關聯的容器。The following section explains how to use data volumes to persist your database files even if the associated containers are deleted.

重要

針對 SQL Server,務必了解在 Docker 中的資料持續性。For SQL Server, it is critical that you understand data persistence in Docker. 除了本章節中討論,請參閱 Docker 文件上如何管理在 Docker 容器中的資料In addition to the discussion in this section, see Docker's documentation on how to manage data in Docker containers.

作為資料磁碟區掛接主機目錄Mount a host directory as data volume

第一個選項是您在主機上掛接目錄,為資料磁碟區容器中。The first option is to mount a directory on your host as a data volume in your container. 若要這樣做,請使用docker run命令搭配-v <host directory>:/var/opt/mssql旗標。To do that, use the docker run command with the -v <host directory>:/var/opt/mssql flag. 這可讓還原容器執行之間的資料。This allows the data to be restored between container executions.

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1433:1433 -v <host directory>:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 -v <host directory>:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1433:1433 -v <host directory>:/var/opt/mssql -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 -v <host directory>:/var/opt/mssql -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu

這項技術也可讓您共用,並檢視外部 Docker 主機上的檔案。This technique also enables you to share and view the files on the host outside of Docker.

重要

在此階段不支援在 Mac 上與 SQL Server Linux 映像上的 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. 下列範例會建立名為共用的資料量sqlvolumeThe following example creates a shared data volume named sqlvolume.

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

注意

這項技術,以隱含方式執行的命令中建立的資料磁碟區無法搭配舊版的 Docker。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 ServerFor 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 -ti <Container ID> /bin/bash

現在您可以執行命令,就好像您以在容器內的終端機中執行它們。Now you can run commands as though you are running them at the terminal inside the container. 完成後,鍵入 exitWhen finished, type exit. 這會結束互動式命令工作階段中,但您的容器會繼續執行。This exits in the interactive command session, but your container continues to run.

將檔案從一個容器Copy files from a container

若要複製出容器的檔案,請使用下列命令:To copy a file out of the container, use the following command:

docker cp <Container ID>:<Container path> <host path>

範例:Example:

docker cp d6b75213ef80:/var/opt/mssql/log/errorlog /tmp/errorlog
docker cp d6b75213ef80:/var/opt/mssql/log/errorlog C:\Temp\errorlog

將檔案複製到容器Copy files into a container

若要將檔案複製到容器,使用下列命令:To copy a file into the container, use the following command:

docker cp <Host path> <Container ID>:<Container path>

範例:Example:

docker cp /tmp/mydb.mdf d6b75213ef80:/var/opt/mssql/data
docker cp C:\Temp\mydb.mdf d6b75213ef80:/var/opt/mssql/data

執行特定的 SQL Server 容器映像Run a specific SQL Server container image

有的情況下您可能不想使用最新的 SQL Server 容器映像。There are scenarios where you might not want to use the latest SQL Server container image. 若要執行特定的 SQL Server 容器映像,請使用下列步驟:To run a specific SQL Server container image, use the following steps:

  1. 識別 Docker標記您想要使用的版本。Identify the Docker tag for the release you want to use. 若要檢視可用的標籤,請參閱mssql server linux Docker 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 映像,取代<image_tag>在下列命令中使用rc1For example, to pull the RC1 image, replace <image_tag> in the following command with rc1.

    docker pull mcr.microsoft.com/mssql/server:<image_tag>
    
  3. 若要執行該映像新的容器,指定的標記名稱docker run命令。To run a new container with that image, specify the tag name in the docker run command. 在下列命令中,取代<image_tag>與您想要執行的版本。In the following command, replace <image_tag> with the version you want to run.

    docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1401:1433 -d mcr.microsoft.com/mssql/server:<image_tag>
    
    docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1401:1433 -d mcr.microsoft.com/mssql/server:<image_tag>
    

這些步驟也可用來降級現有容器。These steps can also be used to downgrade an existing container. 比方說,您可能想要復原,或降級用於疑難排解或測試執行中的容器。For example, you might want to rollback or downgrade a running container for troubleshooting or testing. 若要降級執行中的容器,您必須使用持續性技術資料的資料夾。To downgrade a running container, you must be using a persistence technique for the data folder. 遵循相同的步驟中所述升級區段,但是當您執行新的容器指定較舊版本的標記名稱。Follow the same steps outlined in the upgrade section, but specify the tag name of the older version when you run the new container.

檢查容器版本Check the container version

如果您想要知道在執行的 docker 容器中的 SQL Server 的版本,請執行下列命令來顯示它。If you want to know the version of SQL Server in a running docker container, run the following command to display it. 取代<Container ID or name>的目標容器識別碼或名稱。Replace <Container ID or name> with the target container ID or name. 取代<YourStrong!Passw0rd>SA 登入的 SQL Server 密碼。Replace <YourStrong!Passw0rd> with the SQL Server password for the SA login.

sudo docker exec -it <Container ID or name> /opt/mssql-tools/bin/sqlcmd \
   -S localhost -U SA -P '<YourStrong!Passw0rd>' \
   -Q 'SELECT @@VERSION'
docker exec -it <Container ID or name> /opt/mssql-tools/bin/sqlcmd `
   -S localhost -U SA -P "<YourStrong!Passw0rd>" `
   -Q 'SELECT @@VERSION'

您也可以識別 SQL Server 版本,並建置目標 docker 容器映像的數字。You can also identify the SQL Server version and build number for a target docker container image. 下列命令會顯示 SQL Server 版本和組建資訊microsoft/mssql 伺服器-linux:2017-最新映像。The following command displays the SQL Server version and build information for the microsoft/mssql-server-linux:2017-latest image. 其做法是使用環境變數執行新的容器PAL_PROGRAM_INFO = 1It does this by running a new container with an environment variable PAL_PROGRAM_INFO=1. 產生的容器立即結束,而docker rm命令會移除它。The resulting container instantly exits, and the docker rm command removes it.

sudo docker run -e PAL_PROGRAM_INFO=1 --name sqlver \
   -ti mcr.microsoft.com/mssql/server:2017-latest && \
   sudo docker rm sqlver
docker run -e PAL_PROGRAM_INFO=1 --name sqlver `
   -ti mcr.microsoft.com/mssql/server:2017-latest; `
   docker rm sqlver

上述命令會顯示類似下列輸出的版本資訊:The previous commands display version information similar to the following output:

sqlservr
  Version 14.0.3029.16
  Build ID ee3d3882f1c48a7a7e590a620153012eaedc2f37143d485df945a079b9d4eeea
  Build Type release
  Git Version 65d42c4
  Built at Sat Jun 16 01:20:11 GMT 2018

PAL
  Build ID 60cfcb134bbae96d311f6a4f56aeb5a685b3809de80bcb61ec587a8f58b555eb
  Build Type release
  Git Version 21a4c11
  Built at Sat Jun 16 01:18:53 GMT 2018

Packages
  system.sfp                    6.2.9200.1,21a4c1178,
  system.common.sfp             10.0.15063.540
  system.certificates.sfp       6.2.9200.1,21a4c1178,
  system.netfx.sfp              4.6.1590.0
  secforwarderxplat.sfp         14.0.3029.16
  sqlservr.sfp                  14.0.3029.16
  sqlagent.sfp                  14.0.3029.16

在容器中的 SQL Server 升級Upgrade SQL Server in containers

若要升級使用 Docker 容器映像,您必須先找出升級版本的標記。To upgrade the container image with Docker, first identify the tag for the release for your upgrade. 從登錄提取本版docker pull命令:Pull this version from the registry with the docker pull command:

docker pull mcr.microsoft.com/mssql/server:<image_tag>

這會更新任何新的容器,您建立時,SQL Server 映像,但它不會更新任何執行中的容器中的 SQL Server。This updates the SQL Server image for any new containers you create, but it does not update SQL Server in any running containers. 若要這樣做,您必須使用最新的 SQL Server 容器映像建立新的容器,並將資料移轉至該新的容器。To do this, you must create a new container with the latest SQL Server container image and migrate your data to that new container.

  1. 請確定您使用其中一個的資料持續性技術您現有的 SQL Server 容器。Make sure you are using one of the data persistence techniques for your existing SQL Server container. 這可讓您使用相同的資料開始新的容器。This enables you to start a new container with the same data.

  2. 停止 SQL Server 容器與docker stop命令。Stop the SQL Server container with the docker stop command.

  3. 建立新的 SQL Server 容器與docker run並指定對應的主應用程式目錄或資料磁碟區容器。Create a new SQL Server container with docker run and specify either a mapped host directory or a data volume container. 請確定您的 SQL Server 升級為使用特定的標記。Make sure to use the specific tag for your SQL Server upgrade. 新的容器現在會使用與您現有的 SQL Server 資料的新版本的 SQL Server。The new container now uses a new version of SQL Server with your existing SQL Server data.

    重要

    RC1 des、desx、rc2 和 GA 之間目前只支援升級。Upgrade is only supported between RC1, RC2, and GA at this time.

  4. 請確認您的資料庫和新的容器中的資料。Verify your databases and data in the new container.

  5. (選擇性) 移除的舊容器docker rmOptionally, remove the old container with docker rm.

疑難排解Troubleshooting

下列各節提供在容器中執行 SQL Server 的疑難排解建議。The following sections provide troubleshooting suggestions for running SQL Server in containers.

Docker 命令發生錯誤Docker command errors

如果遇到任何錯誤docker命令,請確定 docker 服務正在執行,並嘗試以更高權限執行。If you get errors for any docker commands, make sure that the docker service is running, and try to run with elevated permissions.

例如,在 Linux 上,您可能會收到下列錯誤時執行docker命令:For example, on Linux, you might get the following error when running docker commands:

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

如果您在 Linux 上收到這個錯誤,請嘗試執行相同的命令前面加上sudoIf you get this error on Linux, try running the same commands prefaced with sudo. 如果失敗,請確認 docker 服務正在執行,而且必要時加以啟動。If that fails, verify the docker service is running, and start it if necessary.

sudo systemctl status docker
sudo systemctl start docker

在 Windows 中,確認您啟動 PowerShell 或以系統管理員身分您命令提示字元。On Windows, verify that you are launching PowerShell or your command-prompt as an Administrator.

SQL Server 容器啟動錯誤SQL Server container startup errors

如果 SQL Server 容器無法執行,請嘗試下列測試:If the SQL Server container fails to run, try the following tests:

  • 如果您收到錯誤,例如 ' 無法建立端點 CONTAINER_NAME 上網路橋接器。啟動 proxy 時發生錯誤: 接聽 tcp 0.0.0.0:1433 繫結: 已經在使用中的位址。 ',則您嘗試對應到已在使用連接埠的容器連接埠 1433。If you get an error such as 'failed to create endpoint CONTAINER_NAME on network bridge. Error starting proxy: listen tcp 0.0.0.0:1433 bind: address already in use.', then you are attempting to map the container port 1433 to a port that is already in use. 如果您在主機電腦,在本機執行 SQL Server,也可能會發生。This can happen if you're running SQL Server locally on the host machine. 它也可會發生,如果您啟動兩個 SQL Server 容器,並嘗試將其同時對應到相同的主機連接埠。It can also happen if you start two SQL Server containers and try to map them both to the same host port. 如果發生這種情況,使用-p參數對應到不同的主機連接埠的容器連接埠 1433。If this happens, use the -p parameter to map the container port 1433 to a different host port. 例如:For example:
```bash
docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' -p 1400:1433 -d mcr.microsoft.com/mssql/server:2017-latest`.
```

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

```PowerShell
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1400:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu`.
```
  • 請檢查是否有任何錯誤訊息,從容器。Check to see if there are any error messages from container.

    docker logs e69e056c702d
    
  • 請確定您符合指定的最小記憶體和磁碟需求需求一節。Make sure that you meet the minimum memory and disk requirements specified in the Requirements section of this article.

  • 如果您使用任何容器管理軟體,請確定它支援以 root 身分執行的容器處理序。If you are using any container management software, make sure it supports container processes running as root. 以 root 身分執行,例如出現 sqlservr 程序,容器中的執行。The sqlservr process in the container runs as root.

  • 檢閱SQL Server 安裝程式和錯誤記錄檔Review the SQL Server setup and error logs.

啟用傾印擷取Enable dump captures

如果 SQL Server 處理序無法在容器內,您應該建立新的容器,使用SYS_PTRACE啟用。If the SQL Server process is failing inside the container, you should create a new container with SYS_PTRACE enabled. 這會將追蹤處理序,也就是建立在例外狀況的傾印檔案所需的 Linux 功能。This adds the Linux capability to trace a process, which is necessary for creating a dump file on an exception. 傾印檔案可供支援,以協助疑難排解問題。The dump file can be used by support to help troubleshoot the problem. 下列 docker run 命令可讓這項功能。The following docker run command enables this capability.

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:2017-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu

SQL Server 連線失敗SQL Server connection failures

如果您無法連線到容器中執行的 SQL Server 執行個體,請嘗試下列測試:If you can't connect to the SQL Server instance running in your container, try the following tests:

  • 請確定您的 SQL Server 容器正在執行來看看狀態資料行docker ps -a輸出。Make sure that your SQL Server container is running by looking at the STATUS column of the docker ps -a output. 如果沒有,請使用docker start <Container ID>啟動它。If not, use docker start <Container ID> to start it.

  • 如果您對應至非預設的主機連接埠 (不是 1433),請確定您在連接字串中指定的連接埠。If you mapped to a non-default host port (not 1433), make sure you are specifying the port in your connection string. 您可以看到您的連接埠對應中連接埠資料行docker ps -a輸出。You can see your port mapping in the PORTS column of the docker ps -a output. 例如,下列命令會連接到 sqlcmd 接聽通訊埠 1401容器:For example, the following command connects sqlcmd to a container listening on port 1401:

    sqlcmd -S 10.3.2.4,1401 -U SA -P '<YourPassword>'
    
    sqlcmd -S 10.3.2.4,1401 -U SA -P "<YourPassword>"
    
  • 如果您使用docker run的現有對應的資料磁碟區或資料磁碟區容器,使用 SQL Server 會忽略值MSSQL_SA_PASSWORDIf you used docker run with an existing mapped data volume or data volume container, SQL Server ignores the value of MSSQL_SA_PASSWORD. 相反地,都可以從 SQL Server 中的資料的資料量或資料磁碟區容器中使用預先設定的 SA 使用者密碼。Instead, the pre-configured SA user password is used from the SQL Server data in the data volume or data volume container. 確認您要使用的資料,您會將附加至相關聯的 SA 密碼。Verify that you are using the SA password associated with the data you're attaching to.

  • 檢閱SQL Server 安裝程式和錯誤記錄檔Review the SQL Server setup and error logs.

SQL Server 可用性群組SQL Server Availability Groups

如果您使用 Docker 與 SQL Server 可用性群組,有兩個額外的需求。If you are using Docker with SQL Server Availability Groups, there are two additional requirements.

  • 將對應的連接埠,可用於複本通訊 (預設值 5022)。Map the port that is used for replica communication (default 5022). 例如,指定-p 5022:5022一部分您docker run命令。For example, specify -p 5022:5022 as part of your docker run command.

  • 明確地設定在容器主機名稱-h YOURHOSTNAME參數docker run命令。Explicitly set the container host name with the -h YOURHOSTNAME parameter of the docker run command. 設定您的可用性群組時,會使用此主機名稱。This host name is used when you configure your Availability Group. 如果您未指定它與-h,它會預設為容器識別碼。If you don't specify it with -h, it defaults to the container ID.

SQL Server 安裝程式和錯誤記錄檔SQL Server setup and error logs

您可以查看 SQL Server 安裝程式和錯誤記錄檔 /var/opt/mssql/logYou can look at the SQL Server setup and error logs in /var/opt/mssql/log. 如果容器不在執行中,第一次啟動容器。If the container is not running, first start the container. 若要檢查記錄檔,然後使用互動式命令提示字元。Then use an interactive command-prompt to inspect the logs.

docker start e69e056c702d
docker exec -it e69e056c702d "bash"

從您容器內的 bash 工作階段,請執行下列命令:From the bash session inside your container, run the following commands:

cd /var/opt/mssql/log
cat setup*.log
cat errorlog

提示

如果掛接主機目錄來 /var/opt/mssql當您建立您的容器,您可以改為查看記錄主機上的對應路徑的子目錄。If you mounted a host directory to /var/opt/mssql when you created your container, you can instead look in the log subdirectory on the mapped path on the host.

後續步驟Next steps

開始使用 Docker 上的 SQL Server 2017 容器映像經過快速入門Get started with SQL Server 2017 container images on Docker by going through the quickstart.

此外,請參閱mssql-docker GitHub 存放庫的資源、 意見反應和已知的問題。Also, see the mssql-docker GitHub repository for resources, feedback, and known issues.

探索 SQL Server 容器的高可用性Explore high availability for SQL Server containers