.NET Dockerfile Collections

2023-09-10

筆記各式 .NET 相關的 Dcokerfile,作為開發與部署的參考。

logo

說明

使用 Visual Studio 2022 建立的專案,搭配啟用 Docker 選項,會自動建立的 Dockerfile (WebAPI, RazorPage, MVC 專案內容都相同):

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["WEBAPI.csproj", "."]
RUN dotnet restore "./WEBAPI.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WEBAPI.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WEBAPI.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WEBAPI.dll"]

微服務無限前端的連續使用 dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT [ "dotnet", "WEBAPI.dll" ]

前端靜態網站搭配 nginx 的 dockerfile:

FROM node:alpine as builder

WORKDIR /usr/app
COPY . .

RUN npm install
RUN npm run build --output-path=./dist/ --prod

FROM nginx:alpine
COPY --from=builder /usr/app/dist/ /usr/share/nginx/html
COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf