Contact Info
43 Street 3, Hiep Binh Chanh Ward
Thu Duc District, HCM City, Vietnam
[email protected]
+84 866.09.02.09

How should we call you?

    Follow Us

    Zodinet Technology Co.,Ltd

    Optimize Docker Image Size

    I. Use smaller image base

    Consider using an alpine OS as base image, it’s only about 50MB

    FROM alpine

    II. Don’t install unnecessary library

    We don’t need install any debug tool as nano, curl, telnet, vim . Developers will install it on demand when they need it

    III. Minimize your layer

    We need to merge any possible command into 1 layer to help smaller layer size. Instead of

    RUN apt-get update
    RUN apt-get install -y nodejs

    We use

    RUN apt-get update && apt-get install -y nodejs

    IV. Not cache repository when install packages

    With ubuntu

    RUN apt-get install -- no-install-recommends

    With alpine

    apk add --no-cache

    V. Remove repository list or source after setup

    After install everything in docker, we should remove package list/repository list & source code.

    With ubuntu, we can run following layer

    RUN rm -rf /var/lib/apt/lists/*

    With CentOS or Redhat core, run following layer

    RUN yum clean all

    V. Using multiple environment for purpose

    We can use multiple with Docker FROM to reduce unnecessary lib

    FROM AS base
    FROM AS build
    FROM build AS publish
    RUN dotnet publish "ZDN.Web.Api.csproj" -c Release -o /app/publish
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "ZDN.Web.Api.dll"]

    Post a Comment