Docker for .NET Developers: Containerizing Applications

Docker is essential for modern .NET development. Here’s how to containerize your .NET Core applications.

Basic Dockerfile

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

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

FROM build AS publish
RUN dotnet publish -c Release -o /app/publish

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

Building and Running

docker build -t myapp .
docker run -d -p 8080:80 myapp

Best Practices

  • Use multi-stage builds to reduce image size
  • Don’t run as root
  • Use .dockerignore
  • Pin base image versions

References


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.