Losing weight is something I know well but can’t do anything about. Although I can’t lose weight, I can lose my Docker mirror image.

If you look at the previous article on building a Python development environment with Docker, you will see an obvious problem, which is that the image is too large, as shown in the bottom right corner:

Why so fat?

Emm… Is usually eat more and do not like to do sports, lying on the bed lying on the sofa comfortable…

Before we talk about how to lose weight, let’s take a closer look at why you gain weight. Everything happens for a reason. Dockerfile = myProject; Dockerfile = myProject; Dockerfile = myProject

FROM Python :3.7 RUN PIP install requests CMD ["python3"]
Copy the code

With my keen intuition, I have found that the problem is definitely in the first line, don’t ask why, let me pretend X once.

Let’s run Docker Images Python :3.7 and see:

Python :3.7, the base image of MyProject, is already large, so the image we build on it will only be larger (regardless of deleting files inside the base image). So why is Python :3.7 so large? We run Docker History Python :3.7 to see the creation history of the base image:

Docker history - no - trunc python: 3.7

As you can see in History, the creation history of an image is layer by layer. Does it feel like Git commits are layered on top of each other? Each layer corresponds to each row of the Dockerfile, each layer is independent, and each layer has its own space (capacity).

Have immediate effect method reducing weight?

Emm… For myself, maybe not, but for Docker images, yes.

We already know that the base image of Python :3.7 is pretty fat on its own, so can we not use it? The answer is of course, we can completely customize our own image from scratch, feel like a brand new server to compile and install their own software, environment, but we now need to immediately, from scratch to create their own image seems to take a bit of time, So we replaced the Python :3.7 image with the Python :3.7-alpine image, and the contents of the Dockerfile were updated to:

FROM Python :3.7- Alpine RUN PIP install requests CMD ["python3"]
Copy the code

Then we run docker build-t myProject :alpine. To see how big the image is, run Docker Images MyProject:

After testing, myProject code also works, everything is perfect, right?

What about other ways to lose weight?

If only for MyProject, I am satisfied after the above slimming, but if there are other images that are more complex than MyProject, we can:

Use a small base image whenever possible and preferably an official one

As mentioned, we have already done so.

Minimize the number of mirrored layers

Myproject’s Dockerfile does not demonstrate this method. Now suppose we have an image called Demo, Dockerfile, which looks like this:

FROM Python :3.7-alpine RUN apk Update RUN apk add build-base RUN PIP install requests RUN PIP install flask RUN PIP install gunicorn CMD ["python3"]
Copy the code

To take a look at the creation history of this image, run the Docker History demo:

RUN
CMD
RUN

FROM Python :3.7-alpine RUN apk update \ && apk add build-base \ && PIP Install requests Flask Gunicorn CMD ["python3"]
Copy the code

View the creation history of the image after the rebuild:

Pack as little content as possible

For example: when we container for a project, often not all files of the project need to be packaged into the image, we can package the unnecessary files to ignore, you can use.dockerignore, do not need to package into the image files, directories filled in the inside,.gitignore both visual sense.

DockerSlim

dockersl.im/

Take a screenshot from the official website to see what it can do

Write in the last

This paper introduces a number of ways to lose weight, some of which are easy to understand and convenient to implement, some may need to pay some attention to study, and some may have side effects but amazing effects. Yes, this is similar to losing weight for human beings. My original intention is also to use weight loss to make people (unfamiliar with Docker) easier to digest and understand. Here’s wishing you all success in losing weight.