How to create the smallest docker image

About the smallest image

The smallest image can be pulled is alpine

Alpine Linux is a Linux distribution based on busybox and Musl Libc. Its biggest advantage is its small size. When I write this article, the latest version is 3.12.4, with a size of only 2.67M

https://hub.docker.com/_/alpine

Alpine has developed a lot of community versions, basically aiming at small

  • docker pull alpine/git 11.39M
  • docker pull alpine/socat 3.22M
  • docker pull python:rc-alpine 16.54M

In fact, alpine is not the smallest image. There is also an empty image called scratch

Scratch doesn’t even provide the most basic / bin, / usr, / lib, / dev

Through the go language to write a HelloWorld and the code compiled into binary format, directly use the

CMD “./hello”

The following error will be reported because the scratch image does not even provide the most basic sh

In this case, some changes to the format of the CMD are required

FROM library/golang:alpine as base
COPY hello.go /hello.go
WORKDIR /
RUN go build /hello.go

FROM scratch
COPY --from=base /hello /hello
CMD [ "/hello" ]

The mirror constructed in this way is only 1.94mb

Send a Message