image.png

introduction

This is the third article in a series of articles on how to make a minimal Docker image. In the first article, I talked about how to create a smaller image by writing better Dockerfiles. In the second article, I discussed how to use docker-squash to compress the image layer to make a smaller image. These methods are indeed very good, but if we choose a large base image, the methods introduced in the previous two articles will not help.

Let's look at the example in the second article, the standard python image on Docker Hub. If you look at the Dockerfile, you will find that its base image is Debian jessie.

FROM buildpack-deps:jessie
# ensure local python is preferred over distribution python
ENV PATH /usr/local/bin:$PATH
...

The buildpack-deps:jessie image contains the complete Debian jessie distribution, which is huge.

~$ docker images buildpack-deps:jessie
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
buildpack-deps      jessie              3b84923989a0        3 weeks ago         614 MB

You can see that it takes up 614MB of space, even if it is compressed, it will not help, because there is a lot of data in the basic Distribution.

Alpine Linux

Alpine Linux is one of the Linux distributions, which is very small for basic installation. However, even if it is small, it still has a good package repository with many packages. It also has tools similar to apt-get or yum to easily install these packages.

~$ docker images alpine:3.6
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              3.6                 a41a7446062d        5 days ago          3.966 MB

It is only 3.96 MB, which is a huge advantage compared to the 614MB Debian jessie mirror. Many standard Docker images conveniently provide an Alpine Linux version, usually, it has the -alpine suffix. Let's look at a python example.

~$ docker images python:2.7.13-alpine
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              2.7.13-alpine       3dd614730c9c        4 days ago          72.02 MB

The Python VM occupies about 72MB of memory, which is much smaller than the 670MB of the Debian python image.

Use Alpine to build an image

Use Alpine to build the image and use the apk tool in the Dockerfile to install the Alpine software package. Check out the git repo by performing the following operations.

FROM alpine:3.6
RUN apk add --update git && \
    git clone https://github.com/example/myrepo.git && \
    rm myrepo/.git && \
    apk del git && \
    rm -rf /var/cache/apk/*
CMD ['/myrepo/myapp']

can view Alpine's available software package at 1609f78808f868 https://pkgs.alpinelinux.org/.


EngineerLeo
598 声望38 粉丝

专注于云原生、AI等相关技术