优化 .net core 应用的 dockerfile

Posted ActivityReservation

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了优化 .net core 应用的 dockerfile相关的知识,希望对你有一定的参考价值。

优化 .net core 应用的 dockerfile

Intro

在给 .net core 应用的写 dockerfile 的时候一直有个苦恼,就是如果有很多个项目,在 dockerfile 里写起来就会很繁琐,有很多项目文件要 copy,dockerfile 还不支持直接批量复制项目文件,今天要改的一个项目也是有好多个项目文件,不想再一个一个复制,于是 google 一下看有没有比较好的解决方案,找到一个折中的解决方案,分享一下

Solution

  1. 首先把所有的项目文件拷贝到 docker 镜像内 COPY */*.csproj ./

  2. 然后根据项目文件名称创建项目文件夹,并移动项目文件到对应的项目目录下


原来的 dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine AS build-env
WORKDIR /src

# Copy csproj and restore as distinct layers
COPY ActivityReservation.Common/*.csproj ActivityReservation.Common/
COPY ActivityReservation.Models/*.csproj ActivityReservation.Models/
COPY ActivityReservation.DataAccess/*.csproj ActivityReservation.DataAccess/
COPY ActivityReservation.Business/*.csproj ActivityReservation.Business/
COPY ActivityReservation.Helper/*.csproj ActivityReservation.Helper/
COPY ActivityReservation.WechatAPI/*.csproj ActivityReservation.WechatAPI/
COPY ActivityReservation.AdminLogic/*.csproj ActivityReservation.AdminLogic/
COPY ActivityReservation.API/*.csproj ActivityReservation.API/
COPY ActivityReservation/ActivityReservation.csproj ActivityReservation/

# RUN dotnet restore ActivityReservation/ActivityReservation.csproj
## diff between netcore2.2 and netcore3.0
WORKDIR /src/ActivityReservation
RUN dotnet restore

# copy everything and build
COPY . .
RUN dotnet publish -c Release -o out ActivityReservation/ActivityReservation.csproj

# build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine

LABEL Maintainer="WeihanLi"
WORKDIR /app
COPY --from=build-env /src/ActivityReservation/out .
EXPOSE 80
ENTRYPOINT ["dotnet", "ActivityReservation.dll"]

修改之后的 dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine AS build-env
WORKDIR /src

# Copy csproj and restore as distinct layers
# https://andrewlock.net/optimising-asp-net-core-apps-in-docker-avoiding-manually-copying-csproj-files-part-2/
COPY */*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*}/ && mv $file ${file%.*}/; done

## diff between netcore2.2 and netcore3.0
WORKDIR /src/ActivityReservation
RUN dotnet restore

# copy everything and build
COPY . .
RUN dotnet publish -c Release -o out ActivityReservation/ActivityReservation.csproj

# build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine

LABEL Maintainer="WeihanLi"
WORKDIR /app
COPY --from=build-env /src/ActivityReservation/out .
EXPOSE 80
ENTRYPOINT ["dotnet", "ActivityReservation.dll"]

是不是精简了许多,来看一下修改前后的对比,更明显的对比一下:

核心代码:

 # 拷贝所有的二级目录下的项目文件
COPY */*.csproj ./
# 根据项目文件名称创建项目文件夹,并移动项目文件到对应的项目目录下
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*}/ && mv $file ${file%.*}/; done

More

注:上面的方法适用于项目文件目录名称和项目文件名称一致的情况,默认创建的项目应该都会符合这样的要求,如果你的项目文件是三级目录,如 src/*/*.csproj 的,需要自己根据项目情况调整 dockerfile

有没有学到呢~~

Reference

  • https://stackoverflow.com/questions/51372791/is-there-a-more-elegant-way-to-copy-specific-files-using-docker-copy-to-the-work

  • https://andrewlock.net/optimising-asp-net-core-apps-in-docker-avoiding-manually-copying-csproj-files-part-2/

  • https://github.com/WeihanLi/ActivityReservation/blob/dev/Dockerfile


以上是关于优化 .net core 应用的 dockerfile的主要内容,如果未能解决你的问题,请参考以下文章

linux内核网络参数优化

.Net Core简介

如何优化 .NET Core 中的 lambda 查询表达式 ?

linux内核参数优化-net

Dora.Interception, 为.NET Core度身打造的AOP框架[4]:演示几个典型应用

为什么现在已经有超过500万的开发者选择了ASP.NET Core?