简体   繁体   中英

How I can build small dotnet core app docker images

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?

You can make the image smaller by publishing your .NET Core app in "self-contained" form. This comes with the caveat that your app must specify a runtime when published. This isn't a big deal, since you're targeting a specific container base image (Linux). The smallest Linux base image available is Alpine, so you can target that when publishing with this:

dotnet publish --runtime alpine-x64 -c Release --self-contained true -o ./publish

You'll need to change your base image in your Dockerfile like so:


FROM alpine:3.9.4

# Add some libs required by .NET runtime 
# https://github.com/dotnet/core/blob/master/Documentation/build-and-install-rhel6-prerequisites.md#troubleshooting
RUN apk add --no-cache \ 
    openssh libunwind \
    nghttp2-libs libidn krb5-libs libuuid lttng-ust zlib \
    libstdc++ libintl \
    icu

And that should just about do it. You should be left with an image that is only a dozen megabytes.

For reference, this page from ITNext is where I pulled most of this information. You should be able to follow along there if you have issues.

Just found this how-to-build-smaller-and-secure-docker-images-for-net5

and it works for me:

REPOSITORY                           TAG                                                     IMAGE ID       CREATED             SIZE
user135/article                       v1.0.3                                                  4d1997ebec84   31 minutes ago      61.4MB
user135/catalog                       v1.0.0                                                  89886216a80f   About an hour ago   256MB
user135/article                       v1.0.2                                                  44946055dac7   About an hour ago   256MB
user135/catalog                       v1.0.1                                                  f17990b865c6   About an hour ago   61.4MB
user135/article                       v1.0.1                                                  f17990b865c6   About an hour ago   256MB
user135/article                       v1.0.0                                                  d2acc4a7beb9   2 hours ago         256MB

this my dockerfile look like:

FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS publish
WORKDIR /src
COPY ["Article.Api.csproj", "./"]


RUN dotnet restore "Article.Api.csproj" --runtime alpine-x64

COPY . .
RUN dotnet publish "Article.Api.csproj" -c Release -o /app/publish \
    --no-restore \
    --runtime alpine-x64 \
    --self-contained true \
    /p:PublishTrimmed=true \
    /p:PublishSingleFile=true

FROM mcr.microsoft.com/dotnet/runtime-deps:5.0-alpine AS final

RUN adduser --disabled-password \
    --home /app \
    --gecos '' dotnetuser && chown -R dotnetuser /app

RUN apk upgrade musl

USER dotnetuser
WORKDIR /app
EXPOSE 5000

COPY --from=publish /src/article.db .
COPY --from=publish /app/publish .
ENTRYPOINT ["./Article.Api", "--urls", "http://0.0.0.0:5000"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM