In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to simplify Docker image". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Introduction
Some time ago, NetEase Honeycomb launched a honeycomb Logo T-shirt, which is made of a Docker image. The most amazing thing is that its final image size is only 585 bytes.
$docker images | grep hub.c.163.com/public/logoREPOSITORY TAG IMAGE ID CREATED SIZEhub.c.163.com/public/logo latest 6fbdd13cd204 11 days ago 585B
Click here to learn about the production process of the image, which uses a lot of techniques to simplify the image, especially for the optimization and simplification of the C program. But we usually develop not only in C language, but even some images are not packaged by ourselves (such as downloading public images), is there any general means to simplify Docker images? The answer is yes, and some images can even be streamlined by 98%. The benefits of reducing image size are self-evident, saving storage space, saving bandwidth and speeding up transmission. Well, next please follow me to learn how to streamline Docker images step by step.
Mirror layer (Layers)
Before you start making mirrors, first understand how mirroring works, and the most important concept is the mirror layer (Layers). The mirror layer relies on a range of underlying technologies, such as file system (filesystems), write-time replication (copy-on-write), joint mount (union mounts), etc. Fortunately, you can learn these techniques in many places, so I won't go into the technical details here.
In general, you need to remember this most:
In Dockerfile, each instruction creates a mirror layer, which in turn increases the size of the overall mirror.
For example:
FROM busyboxRUN mkdir / tmp/fooRUN dd if=/dev/zero of=/tmp/foo/bar bs=1048576 count=100RUN rm / tmp/foo/bar
The above Dockerfile does these things:
Based on an official basic image busybox (only more than 1m)
Create a folder (/ tmp/foo) and a file (bar)
The file is assigned a size of 100m
And then delete this big file.
In fact, it didn't do anything in the end, so let's build it into a mirror image (you can refer to the first installment for the build):
Docker build-t busybox:test.
Let's compare the original busybox image size with the image size we generated:
$docker images | grep busyboxbusybox test 896c63dbdb96 2 seconds ago 106 MBbusybox latest 2b8fd9751c4c 9 weeks ago 1.093 MB
Unexpectedly, it generated a mirror image of 106 MB.
Why is it that there is an extra 100 M? This is similar to git (both using Copy-On-Write technology). I used git to do the following two submissions (added and deleted). Is A_VERY_LARGE_FILE still in the git warehouse?
$git add variegated LARGENFILES $git commit$ git rm AOBERYVERYLARGENFILES $git commit
The answer is: yes, and it will occupy the size of the warehouse. Git saves each committed version of the file, and each instruction in Dockerfile may increase the size of the overall image, even if it ends up doing nothing.
Simplify the steps
Understanding the knowledge of the mirror layer will help us to simplify the mirror image. Here, let's take the most commonly used open source caching software Redis as an example to introduce how to make a more concise Docker image step by step.
Step 1: initialize the build Redis image
Go directly to Dockerfile:
FROM ubuntu:trustyENV VER 3.0.0ENV TARBALL http://download.redis.io/releases/redis-$VER.tar.gz# = > Install curl and helper tools...RUN apt-get updateRUN apt-get install-y curl make gcc# = = > Download, compile And install...RUN curl-L $TARBALL | tar zxvWORKDIR redis-$VERRUN makeRUN make install#...# = = > Clean up...WORKDIR / RUN apt-get remove-y-- auto-remove curl make gccRUN apt-get cleanRUN rm-rf / var/lib/apt/lists/* / redis-$VER#...CMD ["redis-server"]
Combined with comments, it is not difficult to read, using a few regular commands, briefly introduced as follows:
FROM: write headers to specify a basic image, which is based on ubuntu:trusty
ENV: sets the environment variable, where two environment variables, VER and TARBALL, are set
RUN: the most commonly used Dockerfile instruction, which is used to run various commands. The RUN instruction is called eight times.
WORKDIR: specify the working directory, which is equivalent to the instruction cd
CMD: specifies the command that the image executes by default. Here, the redis-server command is executed by default to start redis.
Perform the build:
$docker build-t redis:lab-1.
Note: domestic network, update and download may be slow
View size:
| | Lab | iamge | Base | Lang | .red [*] | Size (MB) | Memo | |:-- |:: |:-- |-|:-| -| | 1 | redis | `ubuntu` | C | dyn | 347.3 | base ubuntu |
Easily has the size of more than 300 M, can not bear, let's start to optimize step by step.
Step 2: optimize the basic image
Method: choose a smaller base image.
The commonly used Linux system images are generally ubuntu, centos and debian, in which debian is lighter and sufficient. The comparison is as follows:
REPOSITORY TAG IMAGE ID VIRTUAL SIZE--centos 7 214a4932132a 215.7 MBcentos 6 f6808a3e4d9e 202.6 MBubuntu trusty d0955f21bf24 188.3 MBubuntu precise 9c5e4be642b7 131.9 MBdebian jessie 65688f7c61c4 122.8 MBdebian wheezy 1265e16d0c28 84.96 MB
Replace debian:jessie as our base mirror.
Optimize Dockerfile:
FROM debian:jessie#...
Perform the build:
$docker build-t redis:lab-2.
View size:
| | Lab | image | Base | Lang | .red [*] | Size (MB) | Memo | |:-- |:: |:-- |-|:-| -| | 01 | redis | `ubuntu` | C | dyn | 347.3 | base ubuntu | | 02 | redis | `debian` | C | dyn | 305.7 | base debian |
Reduced by 42m, slightly effective, but not obvious. Careful students should find that only 122 MB of debian basic image, built to 305 MB, it seems that there must be room for optimization, how to optimize will need to use the Image Layer knowledge we mentioned at the beginning.
Step 3: concatenate Dockerfile instructions
Method: concatenate your Dockerfile instructions (usually RUN instructions).
The RUN instruction in Dockerfile can sometimes achieve unexpected streamlining by concatenating commands through & & and / support.
Optimize Dockerfile:
FROM debian:jessieENV VER 3.0.0ENV TARBALL http://download.redis.io/releases/redis-$VER.tar.gzRUN echo "= > Install curl and helper tools..." & &\ apt-get update & &\ apt-get install-y curl make gcc &\\ echo "= > Download, compile And install... "& &\ curl-L $TARBALL | tar zxv & &\ cd redis-$VER & &\ make & &\ make install &\... Echo "= > Clean up..." & &\ apt-get remove-y-- auto-remove curl make gcc & &\ apt-get clean &\ rm-rf / var/lib/apt/lists/* / redis-$VER#...CMD ["redis-server"]
Build:
$docker build-t redis:lab-3.
View size:
| | Lab | Image | Base | Lang | .red [*] | Size (MB) | Memo | |:-- |:: |:-- |-|:-| -| | 01 | redis | `ubuntu` | C | dyn | 347.3 | base ubuntu | | 02 | redis | `debian` | C | dyn | 305.7 | base debian | | 03 | redis | `debian` | C | dyn | 151.4 | cmd chaining |
Wow! It has been reduced by 50% all at once, and the effect is obvious. This is the most commonly used means of simplification.
Step 4: compress your image
Method: try to compress your image with a command or tool.
Some commands that come with docker can also help compress images, such as export and import
$docker run-d redis:lab-3$ docker export 71b1c0ad0a2b | docker import-redis:lab-4
But the trouble is that you need to run the container first, and in the process you will lose some of the original mirror information, such as: export port, environment variables, default instructions.
So it is generally experimental to simplify images through the command line, so here is another gadget: docker-squash. It is more simple and convenient to use, and will not lose the information of the original image.
Download and install:
Compression operation:
$docker save redis:lab-3\ | sudo docker-squash-verbose-t redis:lab-4\ | docker load
Note: this tool does not work well under Mac, please use it under Linux
Comparison size:
| | Lab | Image | Base | PL | .red [*] | Size (MB) | Memo | |:-- |:: |:-- |-|:-| -| | 01 | redis | `ubuntu` | C | dyn | 347.3 | base ubuntu | | 02 | redis | `debian` | C | dyn | 305.7 | base debian | 03 | redis | `debian` | C | dyn | 151.4 | cmd chaining | 04 | redis | `debian` | C | dyn | 151.4 | docker-squash | | this is the end of the introduction to "how to simplify Docker Images". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.