執行生產容器映像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.

若要執行的生產版本，可以設定開發人員的容器映像。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:

```bash
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
```
```PowerShell
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"
```
> [!IMPORTANT]
> 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. 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.
> [!NOTE]
> For a full list of possible values for **MSSQL_PID**, see [Configure SQL Server settings with environment variables on Linux](sql-server-linux-configure-environment-variables.md).

連線及查詢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.

下列範例會使用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.

您不一定要指定整個容器的識別碼。您只需要指定字元足以唯一識別它。You don't always have to specify the entire container id. You only have to specify enough characters to uniquely identify it.因此在此範例中，它可能會不足以使用e6或e69而不是完整的識別碼。So in this example, it might be enough to use e6 or e69 rather than the full id.

進入容器後，以 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>'

完成後使用 sqlcmd，鍵入exit。When finished with sqlcmd, type exit.

完成後的互動式命令提示字元，鍵入exit。When 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.

現在有兩個不同的容器中執行的 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.

建立自訂的容器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 stop和docker 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.

作為資料磁碟區掛接主機目錄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 主機上的檔案。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 上使用 hyper-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。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.

如果您刪除資料磁碟區容器，容器中的任何 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 -ti <Container ID> /bin/bash

現在您可以執行命令，就好像您以在容器內的終端機中執行它們。Now you can run commands as though you are running them at the terminal inside the container.完成後，鍵入 exit。When 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.若要尋找正確的時區值，請執行tzselect Linux bash 提示字元命令：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.下列範例示範如何在容器中執行 SQL ServerAmericas/Los_Angeles時區：The following example shows how to run SQL Server in a container in the Americas/Los_Angeles timezone:

執行特定的 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:

提取 SQL Server 容器映像與標記。Pull the SQL Server container image with the tag.例如，若要提取的 RC1 映像，取代<image_tag>在下列命令中使用rc1。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>

若要執行該映像新的容器，指定的標記名稱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.

這些步驟也可用來降級現有容器。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.

您也可以識別 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 = 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.

在容器中的 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.

請確定您使用其中一個的資料持續性技術您現有的 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.

建立新的 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.

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

（選擇性） 移除的舊容器docker rm。Optionally, remove the old container with docker rm.

疑難排解Troubleshooting

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

如果您收到錯誤，例如 ' 無法建立端點 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:

請檢查是否有任何錯誤訊息，從容器。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.

啟用傾印擷取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.

如果您對應至非預設的主機連接埠 (不是 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 可用性群組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/log。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.