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 mcr.microsoft.com/dotnet/aspnet:5.0 AS base
...
FROM mcr.microsoft.com/dotnet/sdk:5.0 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 .
ENV ASPNETCORE_ENVIRONMENT=Internal
ENTRYPOINT ["dotnet", "ZDN.Web.Api.dll"]