Docker制作dotnet core程序镜像



Dockerfile

右键项目→添加Docker支持,目标OS选择Linux
1.gif

添加docker支持后,vs2019会自动帮我们创建Dockerfile文件。Dockerfile就是用来构建镜像的文件,其中包含了各种指令。以下是Dockerfile指令详解

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

#使用aspnet:5.0作为基础镜像,起一个别名为base
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
#设置容器的工作目录为/app
WORKDIR /app
#暴露80端口
EXPOSE 80


#使用dotnet sdk:5.0作为基础镜像,起一个别名为build
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
#设置容器的工作目录为/src
WORKDIR /src
#拷贝src/Service/01.IdentityServer/Imtudou.IdentityServer.WebAPI/Imtudou.IdentityServer.WebAPI.csproj项目文件到容器中的src/Service/01.IdentityServer/Imtudou.IdentityServer.WebAPI/目录
COPY ["src/Service/01.IdentityServer/Imtudou.IdentityServer.WebAPI/Imtudou.IdentityServer.WebAPI.csproj", "src/Service/01.IdentityServer/Imtudou.IdentityServer.WebAPI/"]
COPY ["src/Core/Imtudou.Core.Extensions/Imtudou.Core.Extensions.csproj", "src/Core/Imtudou.Core.Extensions/"]
COPY ["src/Core/Imtudou.Core/Imtudou.Core.csproj", "src/Core/Imtudou.Core/"]
COPY ["src/Core/Imtudou.Core.Logs/Imtudou.Core.Logs.csproj", "src/Core/Imtudou.Core.Logs/"]
COPY ["src/Core/Imtudou.Core.Cache/Imtudou.Core.Cache.csproj", "src/Core/Imtudou.Core.Cache/"]

#执行dotnet restore命令,相当于平时用vs还原nuget包
RUN dotnet restore "src/Service/01.IdentityServer/Imtudou.IdentityServer.WebAPI/Imtudou.IdentityServer.WebAPI.csproj"
#拷贝当前目录的文件到到容器的/src目录
COPY . .
#设置容器的工作目录为/src/src/Service/01.IdentityServer/Imtudou.IdentityServer.WebAPI
WORKDIR "/src/src/Service/01.IdentityServer/Imtudou.IdentityServer.WebAPI"
#执行dotnet build命令,相当于平时用vs生成项目。以Release模式生成到容器的/app/build目录
RUN dotnet build "Imtudou.IdentityServer.WebAPI.csproj" -c Release -o /app/build

#将上面的build(dotnet sdk:5.0)作为基础镜像,又重命名为publish
FROM build AS publish
#执行dotnet publish命令,相当于平时用vs发布项目。以Release模式发布到容器的/app/publish目录
RUN dotnet publish "Imtudou.IdentityServer.WebAPI.csproj" -c Release -o /app/publish

#将上面的base(dotnet sdk:5.0)作为基础镜像,又重命名为final
FROM base AS final
#设置容器的工作目录为/app
WORKDIR /app
#拷贝/app/publish目录到当前工作目录
COPY --from=publish /app/publish .
#指定容器入口命令,容器启动时会运行dotnet Imtudou.IdentityServer.WebAPI.dll
ENTRYPOINT ["dotnet", "Imtudou.IdentityServer.WebAPI.dll"]

Build & Run

来到项目根目录(带.sin这一级目录),启动PowerShell或cmd执行docker命令。
image.png

构建镜像:
docker build -t identityserverapi -f .\src\Service\01.IdentityServer\Imtudou.IdentityServer.WebAPI\Dockerfile .参数-f是指定Dockerfile所在的目录。
image.png
使用 docker images 查看本地镜像,identityserverapi就是上面构建完成的镜像:
image.png
启动容器: docker run -d -p 5000:80 --name identityserverapi01 identityserverapi

  • -p: 指定端口映射,格式为:主机(宿主)端口:容器端口

image.png
使用docker ps 查看正在运行中的容器
image.png
浏览器访问[http://localhost:5000/swagger/index.html](http://localhost:5000/swagger/index.html)
image.png
正常显示一个简单的asp.net core web应用就成功运行于docker之中

使用vs2019将项目设置为Docker启动

image.png
注意,首次容器工具首次加载会比较慢。。。启动完成后会自动打开浏览器,并绑定了一个随机端口:

可能会遇见的错误:“未启用共享卷。。。”
image.png
解决方法:https://docs.microsoft.com/zh-cn/visualstudio/containers/troubleshooting-docker-errors?view=vs-2022
如下配置即可:image.png

参考:https://www.cnblogs.com/xhznl/p/13353095.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章