How I can build small dotnet core app docker images

  .net, asp.net-core, c#, docker, dockerfile

I have docker-images as below:

REPOSITORY                              TAG                IMAGE ID       CREATED          SIZE
user135/todoapi-Go                      latest             0b4cd5e70a9b   18 seconds ago   9.43MB
user135/todoapi                         v1.0.0             5a3a2cfe9692   2 weeks ago      210MB

I do compare the size of docker images from user135/todoapi-Go (Golang) has very small rather than user135/todoapi which is (C#/Dotnet),And also I checked my other Dotnet app was builds with docker has 200mb++ size,

I used this Dockerfile for my dotnet app, and it’s was generated from Visual Studio when I adding Docker Support for the project :

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app

ENV ASPNETCORE_URLS http://+:5000
EXPOSE 5000
EXPOSE 5001

ENV TZ=Asia/Jakarta
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

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

FROM build AS publish
RUN dotnet publish "TodoApi.csproj" -c Release -o /app/publish

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

my Dockerfile for Go App

FROM golang:alpine AS builder
RUN apk update && apk add --no-cache git
WORKDIR $GOPATH/src/mypackage/myapp/

ENV CGO_ENABLED=0
COPY . .

RUN go get -d -v
RUN go build -o /go/bin/main

FROM scratch
COPY --from=builder /go/bin/main /go/bin/main
ENTRYPOINT ["/go/bin/main"]

My question here:

  • how I can build a dotnet app with a small size docker images ?
  • does this affect performance the app or container ?

Source: Docker Questions

LEAVE A COMMENT