Skip to content

BlogTips & Tutorials

Tips & Tutorials

Optimize Docker Image Size

You are wondering about your images size can flood your server disk space and doesn’t want to optimize one by one image.So, you can use JpegOptim and PngOptim tool now on CentOS & Ubuntu.

1 min read · Estimated time

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"]

More insights from Zodinet

START WITH ZODINET

Need similar software built?

Tell us about your problem. Zodinet sends a first quote within 2 days.

Explore Zodinet

VIEN

Services

Choose the right way to deliver

See all services

Let's get started

Tell Zodinet about your problem.

Choose the way that suits you.

Send a consultation request

Contents

Jump to the part you need

  1. I. Use smaller image base
  2. II. Don’t install unnecessary library
  3. III. Minimize your layer
  4. IV. Not cache repository when install packages
  5. V. Remove repository list or source after setup
  6. V. Using multiple environment for purpose