美文网首页Amazing .NET.NET Core.NET
优化 .net core 应用的 dockerfile

优化 .net core 应用的 dockerfile

作者: 天天向上卡索 | 来源:发表于2019-11-01 18:46 被阅读0次

    优化 .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"]
    

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

    diff

    核心代码:

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

    More

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

    有没有学到呢~~

    Reference

    相关文章

      网友评论

        本文标题:优化 .net core 应用的 dockerfile

        本文链接:https://www.haomeiwen.com/subject/knjqbctx.html