優化 .net core 應用的 dockerfile

優化 .net core 應用的 dockerfile

Intro

在給 .net core 應用的寫 dockerfile 的時候一直有個苦惱,就是若是有不少個項目,在 dockerfile 裏寫起來就會很繁瑣,有不少項目文件要 copy,dockerfile 還不支持直接批量複製項目文件,今天要改的一個項目也是有好多個項目文件,不想再一個一個複製,因而 google 一下看有沒有比較好的解決方案,找到一個折中的解決方案,分享一下html

Solution

  1. 首先把全部的項目文件拷貝到 docker 鏡像內 COPY */*.csproj ./
  2. 而後根據項目文件名稱建立項目文件夾,並移動項目文件到對應的項目目錄下

原來的 dockerfilegit

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

修改以後的 dockerfilegithub

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

是否是精簡了許多,來看一下修改先後的對比,更明顯的對比一下:docker

diff

核心代碼:app

# 拷貝全部的二級目錄下的項目文件
COPY */*.csproj ./
# 根據項目文件名稱建立項目文件夾,並移動項目文件到對應的項目目錄下
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*}/ && mv $file ${file%.*}/; done

More

注:上面的方法適用於項目文件目錄名稱和項目文件名稱一致的狀況,默認建立的項目應該都會符合這樣的要求,若是你的項目文件是三級目錄,如 src/*/*.csproj 的,須要本身根據項目狀況調整 dockerfile優化

有沒有學到呢~~ui

Reference

原文出處:https://www.cnblogs.com/weihanli/p/optimize-your-dockerfile-for-dotnet.htmlgoogle

相關文章
相關標籤/搜索