開始使用 Visual Studio for Mac 中的 DockerGet started with Docker in Visual Studio for Mac

使用 Visual Studio for Mac,您可以輕鬆地建置、進行偵錯,以及執行容器化的 ASP.NET Core 應用程式並將它們發行至 Azure。With Visual Studio for Mac, you can easily build, debug, and run containerized ASP.NET Core apps and publish them to Azure.

先決條件Prerequisites

安裝和設定Installation and Setup

若要安裝 Docker,請檢閱安裝適用於 Mac 的 Docker Desktop,並遵循其中的資訊。For Docker installation, review and follow the information at Install Docker Desktop for Mac.

建立 ASP.NET Core Web 應用程式並新增 Docker 支援Creating an ASP.NET Core Web Application and Adding Docker Support

  1. 藉由移至 [檔案] > [新增解決方案] 來建立新解決方案。Create a new solution by going to File > New Solution.
  2. 在 [ .Net Core > 應用 程式] 下,選擇 [ Web 應用程式 ] 範本:  建立新的 ASP.NET 應用程式Under .NET Core > App choose the Web Application template: Create a new ASP.NET application
  3. 選取目標 Framework。Select the target framework. 在此範例中,我們將使用 .NET Core 2.2:  設定目標 frameworkIn this example we will use .NET Core 2.2: Set target framework
  4. 輸入專案詳細資料,例如,名稱 (此範例中為 DockerDemo )。Enter the project details, such as name ( DockerDemo in this example). 所建立的專案包含建置和執行 ASP.NET Core 網站所需的所有基本項目。The created project contains all the basics you need to build and run an ASP.NET Core web site.
  5. 在 [方案] 視窗中,以滑鼠右鍵按一下 >dockerdemo 專案,然後選取 [ 新增] > 新增 Docker 支援 : ![ 新增 docker 支援]](media/docker-quickstart-3.png)In the Solution Window, right click the DockerDemo project and select Add > Add Docker Support : Add docker support

Visual Studio for Mac 會將稱為 docker-compose 的專案自動新增到解決方案,並將 Dockerfile 新增到您的現有專案。Visual Studio for Mac will automatically add a new project to your solution called docker-compose and add a Dockerfile to your existing project.

產生的 Docker 支援檔案

Dockerfile 概觀Dockerfile Overview

Dockerfile 是用於建立最終 Docker 映像的配方。A Dockerfile is the recipe for creating a final Docker image. 請參閱 Dockerfile 參考 ,以瞭解其內的命令。Refer to Dockerfile reference for an understanding of the commands within it.

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY DockerDemo/DockerDemo.csproj DockerDemo/
RUN dotnet restore DockerDemo/DockerDemo.csproj
COPY . .
WORKDIR /src/DockerDemo
RUN dotnet build DockerDemo.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish DockerDemo.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DockerDemo.dll"]

上述的 Dockerfilemicrosoft/aspnetcore 映像為基礎,其中包含藉由建置專案並將其新增至容器來修改基底映像的指示。The preceding Dockerfile is based on the microsoft/aspnetcore image, and includes instructions for modifying the base image by building your project and adding it to the container.

注意

由 Visual Studio for Mac 中建立的預設 Dockerfile 為 HTTP 流量公開連接埠 80。The default Dockerfile created by Visual Studio for Mac exposes Port 80 for HTTP traffic. 若要啟用 HTTPS 流量,請將 Expose 443 新增至 Dockerfile。To enable HTTPS traffic, add Expose 443 to the Dockerfile.

偵錯Debugging

選取 docker-compose 專案作為 [啟始專案] 並開始偵錯 ([執行] > [開始偵錯])。Select the docker-compose project as the Startup Project and start debugging ( Run > Start Debugging ). 這會在容器中建置、部署並啟動 ASP.NET 專案。This will build, deploy and launch the ASP.NET project in a container.

提示

在安裝 Docker Desktop 後第一次執行時,您可能會在進行偵錯時收到下列錯誤:Cannot start service dockerdemo: Mounts deniedOn the first run after installing Docker Desktop, you may receive the following error when trying to debug: Cannot start service dockerdemo: Mounts denied

請將 /usr/local/share/dotnet/sdk/NuGetFallbackFolder 新增到 Docker Desktop 中的 [File Sharing] (檔案共用) 索引標籤:Add /usr/local/share/dotnet/sdk/NuGetFallbackFolder to the File Sharing tab in Docker Desktop:

將 [NuGetFallbackFolder] 資料夾新增至 [File Sharing] (檔案共用)

當組建完成時,應用程式會在 Safari 中啟動:When the build is completed, the application will be launched in Safari:

在 Safari 中執行的預設 Docker 專案

請注意,容器會接聽連接埠 (例如,http://localhost:32768),而此連接埠可能會不同。Note that the container will be listening on a port, http://localhost:32768 for example, and this port may vary.

若要查看執行中容器的清單,請在 [終端機] 中使用 docker ps 命令。To see the list of running containers, use the docker ps command in Terminal.

請注意以下螢幕擷取畫面中的連接埠轉送 (在 PORTS 底下)。Note the port relay in the screenshot below (under PORTS ). 此圖顯示容器正在接聽之前在 Safari 中所見的連接埠,並將要求轉送到內部 Webserver 的連接埠 80 (如 Dockerfile 中所定義) 上。This shows that the container is listening on the port we saw in Safari above and relaying requests to the internal webserver on port 80 (as defined in the Dockerfile). 從應用程式的觀點來看,它在接聽連接埠 80:From the application's perspective, it is listening on port 80:

Docker 容器清單