Skip to main content

Dockerfile

Basic instructions

InstructionDescription
COPYCopy files and directories.
ENTRYPOINTSpecify default executable.
ENVSet environment variables.
EXPOSESet the ports on which the application listens.
FROMCreate a new build stage from a base image.
LABELAdd metadata to an image.
RUNExecute build commands.
WORKDIRSet working directory.

Checklist for Dockerfile

  • Use UPPERCASE for instructions
  • Use multiple small build stages instead of one large one
  • Use clear names for each stage
  • Combine multiple RUN blocks into one if possible
  • Use slash (\) to split multiline commands
  • Use .dockerignore to exclude unnecessary files

How to run ASP.NET application?

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS base
LABEL project="My ASP.NET project" \
version="1.0"
WORKDIR /app
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS build
WORKDIR /src

COPY ["nuget.config", "."]
COPY ["MyAspNetProject/MyAspNetProject.csproj", "MyAspNetProject/"]

RUN dotnet restore

COPY . .
WORKDIR "/src/MyAspNetProject"

RUN dotnet build "MyAspNetProject.csproj" \
--configuration Release \
--output "/app/build"

FROM build AS publish
ARG VERSION
RUN dotnet publish "MyAspNetProject.csproj" --configuration Release \
--output "/app/publish" \
--property:Version=$(VERSION)

FROM base AS final
ENV ASPNETCORE_URLS="http://+:8081"
WORKDIR /app
COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "MyAspNetProject.dll"]

How to build a separate Node.js and ASP.NET projects?

FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
WORKDIR /app
EXPOSE 8081

FROM node:20 AS npm-build
WORKDIR /build
COPY ["MyFrontendProject/src", "."]
RUN npm ci && npm run build

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS publish
ARG VERSION
WORKDIR /src
COPY . .
COPY --from=npm-build "/build/dist" "./MyAspNetProject/wwwroot/js"
RUN dotnet restore "MyAspNetProject/MyAspNetProject.csproj"
RUN dotnet publish "MyAspNetProject.csproj" \
--configuration Release \
--output "/app/publish" \
--property:Version=$(VERSION)

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "MyAspNetProject.dll"]