【Azure Developer】已發布好的.NET Core項目文件如何打包為Docker鏡像文件


問題描述

在博文(【Azure App Service For Container】創建ASP.NET Core Blazor項目並打包為Linux鏡像發布到Azure應用服務)中我們通過VS 2019可以為項目添加Dockerfile並自動生成Docker Image文件。但是如果不借助於VS2019我們如何來操作呢?

 

解決步驟

准備Dockerfile

進入項目文件夾中,創建Dockerfile並COPY以下內容:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
ENV ASPNETCORE_URLS=http://+:8000 
WORKDIR /app EXPOSE 8000 EXPOSE 5000 COPY . /app/ ENTRYPOINT ["dotnet", "MyLife.Blazor.Server.dll"]
  • FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base  和 FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build 可以在Docker Hub中查詢到對於的版本鏡像(https://hub.docker.com/_/microsoft-dotnet-sdk)
  • COPY . /app/ 即把dockerfile所在的目錄中所有文件復制到鏡像中app目錄中

 

生成鏡像

通過CMD進入到當前目錄, 使用 docker build -t mywebimages (特別注意:在命令中必須要點.,黃色高亮的部分替代為自定義的鏡像名。詳細的命令參考Docker說明:https://docs.docker.com/engine/reference/commandline/build/

 

當命令運行完成后,在Docker Desktop頁面中可以看見當前Images

 

運行鏡像,驗證項目運行成功

在Docker Desktop中Run當前Image或者通過docker run命令啟動Container: docker run --name testweb -p 8080:8000 mywebimages

 命令啟動輸出:

C:\MyCode\MyLife\MyLife.Blazor\MyLife.Blazor\Server\bin\Release\net5.0\publish>docker run --name testapidemo -p 8080:8000 mywebimages
warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
Storing keys in a directory 'C:\Users\ContainerUser\AppData\Local\ASP.NET\DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:8000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\app
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.

 

 

訪問Docker Container指定的端口8080結果為:

 

附錄:如何Push本地Docker中的鏡像到ACR(Azure Container Registry)中?

在Azure Container Registry中,有快速入門的文檔(Quick Start),參考里面的步驟即可。只是在登錄的時候說明以下。如果使用Access Keys中的用戶名和密碼。這可以包含在Docker Login命令中。如:

docker login myregistry.azurecr.cn --username 00000000-0000-0000-0000-000000000000 --password eyJhbGciOiJSUzI1NiIs[...]24V7wA

docker tag mywebimages myregistry.azurecr.cn/mywebimages

docker push myregistry.azurecr.cn/mywebimages

詳細的步驟見文檔:

 

參考資料:

Docker Hub: https://hub.docker.com/_/microsoft-dotnet-sdk

創建ASP.NET Core Blazor項目並打包為Linux鏡像發布到Azure應用服務: https://www.cnblogs.com/lulight/p/14315383.html

windows上用VS2019開發的 .NETCore項目如何打包部署到linux Docker中: https://blog.csdn.net/weixin_48000648/article/details/106524426

Push the image to your registry: https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-docker-cli#push-the-image-to-your-registry


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM