Dockerfile .net core optimized applications

Dockerfile .net core optimized applications

Intro

In the time to write dockerfile .net core applications has been a troubled, that is, if there are a lot of projects to write in dockerfile years will be very complicated, there are a lot of project files to copy, dockerfile not support direct bulk copy project files, today we have to change the project is also a good number of project files, one by one, do not want to copy, then google it to see if a better solution, to find a compromise solution to share

Solution

  1. First copy all the files to the project within the docker mirror COPY */*.csproj ./
  2. Then create a project folder under the project file name, and move the project files to the corresponding directory project

The original 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 after revision

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"]

Is streamline a lot, look at the contrast is more obvious changes before and after comparison, look at:

diff

Core code:

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

More

Note: The above method is applicable to the project file directory names and file the same name of the project, the project will be created by default should comply with such requirements, if your project file is a three directories, such as src/*/*.csprojthe need to make their own adjustments depending on the project dockerfile

Have not learned it ~ ~

Reference

Guess you like

Origin www.cnblogs.com/weihanli/p/optimize-your-dockerfile-for-dotnet.html