Dockerfile 概觀Dockerfile overview

Dockerfile，是用於建立最終 Docker 映像的配方，會新增至專案根目錄。A Dockerfile, the recipe for creating a final Docker image, is added to the project root.請參閱 Dockerfile 參考，以了解其內的命令。Refer to Dockerfile reference for an understanding of the commands within it.此特定 Dockerfile 使用多階段建置，包含四個不同的具名建置階段：This particular Dockerfile uses a multi-stage build with four distinct, named build stages:

核取新專案對話方塊的 [設定 HTTPS] 核取方塊時，Dockerfile 會提供兩個連接埠。When the new project dialog's Configure for HTTPS check box is checked, the Dockerfile exposes two ports.其中一個連接埠用於 HTTP 流量，另一個連接埠則用於 HTTPS。One port is used for HTTP traffic; the other port is used for HTTPS.如果未選取該核取方塊，則會為 HTTP 流量提供單一連接埠 (80)。If the check box isn't checked, a single port (80) is exposed for HTTP traffic.

如果映像會推送至登錄，會在映像名稱前加上 Docker Hub 使用者名稱 (例如，dockerhubusername/hellodockertools)。Prefix the image name with the Docker Hub username (for example, dockerhubusername/hellodockertools) if the image is pushed to the registry.或者，根據設定將映像名稱變更為包含私人登錄 URL (例如，privateregistry.domain.com/hellodockertools)。Alternatively, change the image name to include the private registry URL (for example, privateregistry.domain.com/hellodockertools) depending on the configuration.

如果您需要基於組建設定的不同行為 (例如偵錯或發行)，請新增特定於設定的 docker-compose 檔案。If you want different behavior based on the build configuration (for example, Debug or Release), add configuration-specific docker-compose files.應根據組建設定來命名檔案 (例如 docker-compose.vs.debug.yml 和 docker-compose.vs.release.yml) 並放在與 docker-compose-override.yml 檔案相同的位置。The files should be named according to the build configuration (for example, docker-compose.vs.debug.yml and docker-compose.vs.release.yml) and placed in the same location as the docker-compose-override.yml file.

使用特定於設定的覆寫檔案，可以為偵錯和發行組建設定指定不同的組態設定 (例如環境變數或進入點)。Using the configuration-specific override files, you can specify different configuration settings (such as environment variables or entry points) for Debug and Release build configurations.

將 Dockerfile 與 .dockerignore 檔案，新增至 ASP.NET Core 專案。Adds a Dockerfile and a .dockerignore file to the ASP.NET Core project.如果 ASP.NET Core 專案中已存在 Dockerfile，則會重新命名為 Dockerfile.original。If a Dockerfile already exists in the ASP.NET Core project, it's renamed to Dockerfile.original.會建立類似如下的新 Dockerfile：A new Dockerfile, similar to the following, is created:

# See https://aka.ms/containerimagehelp for information on how to use Windows Server 1709 containers with Service Fabric.
# FROM microsoft/aspnetcore:2.0-nanoserver-1709
FROM microsoft/aspnetcore:2.0-nanoserver-sac2016
ARG source
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "HelloDockerTools.dll"]

偵錯Debug

從工具列的偵錯下拉式清單中選取 [Docker]，然後開始對應用程式進行偵錯。Select Docker from the debug drop-down in the toolbar, and start debugging the app.[輸出] 視窗的 Docker 檢視會顯示下列將採取的動作：The Docker view of the Output window shows the following actions taking place:

在容器內，ASPNETCORE_ENVIRONMENT 環境變數設定為 Development。The ASPNETCORE_ENVIRONMENT environment variable is set to Development within the container.

會提供兩個動態指派的連接埠：一個用於 HTTP，另一個用於 HTTPS。Two dynamically assigned ports are exposed: one for HTTP and one for HTTPS.可使用 docker ps 命令，查詢指派到 localhost 的連接埠。The port assigned to localhost can be queried with the docker ps command.

應用程式會複製至容器。The app is copied to the container.

預設瀏覽器會在偵錯工具使用動態指派的連接埠附加至容器的情況下啟動。The default browser is launched with the debugger attached to the container using the dynamically assigned port.

產生的應用程式 Docker 映像，會標記為 dev。The resulting Docker image of the app is tagged as dev.此映像以 microsoft/dotnet 基底映像的 2.1-aspnetcore-runtime 標籤為基礎。The image is based on the 2.1-aspnetcore-runtime tag of the microsoft/dotnet base image.在 [套件管理員主控台] (PMC) 視窗中，執行 docker images 命令。Run the docker images command in the Package Manager Console (PMC) window.這會顯示電腦上的映像：The images on the machine are displayed:

在容器內，ASPNETCORE_ENVIRONMENT 環境變數設定為 Development。The ASPNETCORE_ENVIRONMENT environment variable is set to Development within the container.

連接埠 80 會公開並對應至本機主機的動態指派連接埠。Port 80 is exposed and mapped to a dynamically assigned port for localhost.連接埠是由 Docker 主機所決定，並且可以使用 docker ps 命令進行查詢。The port is determined by the Docker host and can be queried with the docker ps command.

應用程式會複製至容器。The app is copied to the container.

預設瀏覽器會在偵錯工具使用動態指派的連接埠附加至容器的情況下啟動。The default browser is launched with the debugger attached to the container using the dynamically assigned port.

編輯後繼續Edit and continue

針對靜態檔案和 Razor 檢視所做的變更會自動更新，而不需要編譯步驟。Changes to static files and Razor views are automatically updated without the need for a compilation step.進行變更並儲存，然後重新整理瀏覽器來檢視更新。Make the change, save, and refresh the browser to view the update.

程式碼檔案的修改需要編譯以及重新啟動容器內的 Kestrel。Code file modifications require compilation and a restart of Kestrel within the container.完成變更之後，請使用 CTRL+F5 來執行程序，並啟動容器內的應用程式。After making the change, use CTRL+F5 to perform the process and start the app within the container.Docker 容器不會進行重建或停止。The Docker container isn't rebuilt or stopped.在 PMC 中執行 docker ps 命令。Run the docker ps command in PMC.請注意，原始容器在 10 分鐘前仍在執行：Notice the original container is still running as of 10 minutes ago:

發行 Docker 映像Publish Docker images

當應用程式的開發和偵錯循環完畢之後，Visual Studio Tools for Docker 就會協助建立應用程式的實際執行映像。Once the develop and debug cycle of the app is completed, the Visual Studio Tools for Docker assist in creating the production image of the app.將組態下拉式清單變更為 [發行] 並建置應用程式。Change the configuration drop-down to Release and build the app.工具會從 Docker Hub (若尚未在快取中) 取得編譯/發行映像。The tooling acquires the compile/publish image from Docker Hub (if not already in the cache).映像會使用最新的標籤產生，其可推送至私人登錄或 Docker Hub。An image is produced with the latest tag, which can be pushed to the private registry or Docker Hub.

在 PMC 中執行 docker images 命令，可查看映像清單。Run the docker images command in PMC to see the list of images.會顯示類似下列的輸出：Output similar to the following is displayed:

docker images 命令會傳回存放庫名稱和標記識別為 <無> (上面未列出) 的中繼映像。The docker images command returns intermediary images with repository names and tags identified as <none> (not listed above).這些未命名映像是由多階段建置Dockerfile 所產生。These unnamed images are produced by the multi-stage buildDockerfile.它們可以改善最終映像的建置效率 — 發生變更時只會重建必要層。They improve the efficiency of building the final image—only the necessary layers are rebuilt when changes occur.當不再需要中繼映像時，請使用 docker rmi (英文) 命令予以刪除。When the intermediary images are no longer needed, delete them using the docker rmi command.

相較於 dev 映像，生產或發行映像的大小可能需要更小。There may be an expectation for the production or release image to be smaller in size by comparison to the dev image.基於磁碟區對應，偵錯工具和應用程式是從本機電腦執行，而不是在容器內執行。Because of the volume mapping, the debugger and app were running from the local machine and not within the container.「最新」映像已封裝在主機上執行應用程式所需的應用程式碼。The latest image has packaged the necessary app code to run the app on a host machine.因此，差異是應用程式碼的大小。Therefore, the delta is the size of the app code.