Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to simplify Docker mirrors

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to streamline Docker images". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to simplify Docker images".

Introduction

Looking at some images in docker hub, we can sometimes see a lot of tiny images, such as:

REPOSITORY TAG VIRTUAL SIZE LICENSE--busybox latest 2.43 MB GPLv2progrium/busybox latest 4.785 MB GPLv2alpine 3.1 5.025 MB GPLv2

How is such a small mirror image made? Following the above, let's introduce some of the ultimate ways to streamline Docker mirroring!

Step 5: use the most streamlined base image

Method: use scratch or busybox as the base image.

About scratch:

An empty image, which can only be used to build an image, via FROM scratch

It is very useful to build some basic images, such as debian and busybox.

Used to build very few images, such as building a binary file containing all libraries

About busybox

It is only the size of 1m to 5m

Contains commonly used UNIX tools

It is very convenient to build a small image.

These ultra-small basic images, combined with compiled languages that can generate static native ELF files, such as Cmax Cure images, such as Go, are especially convenient to build ultra-small images.

Cloudcomb-logo (C language development) uses this principle to build a 585-byte image.

Redis is also developed in C language, and there seems to be a lot of room for optimization. In this experiment, let's introduce the specific operation methods.

Step 6: extract the dynamically linked .so file

Lab context:

$cat / etc/os-releaseNAME= "Ubuntu" VERSION= "14.04.2 LTS, Trusty Tahr" $uname-aLinux localhost 3.13.0-46-generic # 77-Ubuntu SMPMon Mar 2 18:23:39 UTC 2015x86_64 x86'64 x 86'64 GNU/Linux

Grand launch of ldd: dependent libraries for print sharing

$ldd redis-3.0.0/src/redis-server linux-vdso.so.1 = > (0x00007fffde365000) libm.so.6 = > / lib/x86_64-linux-gnu/libm.so.6 (0x00007f307d5aa000) libpthread.so.0 = > / lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f307d38c000) libc.so.6 = > / lib/x86_64-linux-gnu/libc.so.6 (0x00007f307cfc6000) / lib64/ld-linux-x86-64.so.2 (0x00007f307d8b9000)

Package all required .so files:

$tar ztvf rootfs.tar.gz4485167 2015-04-21 22:54 usr/local/bin/redis-server1071552 2015-02-25 16:56 lib/x86_64-linux-gnu/libm.so.6 141574 2015-02-25 16:56 lib/x86_64-linux-gnu/libpthread.so.01840928 2015-02-25 16:56 lib/x86_64-linux-gnu/libc.so.6 149120 2015-02-25 16:56 lib64/ld-linux-x86-64.so.2

And then make it into Dockerfile:

FROM scratchADD rootfs.tar.gz / COPY redis.conf / etc/redis/redis.confEXPOSE 6379CMD ["redis-server"]

Perform the build:

$docker build-t redis-05.

View size:

| | Lab | | 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 | | 05 | redis | `roomch` | C | dyn | 7.73 | rootfs: .so |

Wow! Significantly improved!

Test it:

$docker run-d-- name redis-05 redis-05$ redis-cli-h\ $(docker inspect-f'{.NetworkSettings.IPAddress}} 'redis-05) $redis-benchmark-h\ $(docker inspect-f' {{.NetworkSettings.IPAddress} 'redis-05)

To sum up:

Use ldd to find the required .so files

Compress all dependencies into rootfs.tar or rootfs.tar.gz, and then enter the basic image of scratch

Step 7: build a thin image for the Go application

The Go language is inherently convenient for building compact images because it can be easily packaged into binary files containing static links.

For example, you have a 4 MB Go binary with static links, and you put it into a basic image like scratch, and you get an image of a mere 4 MB. This is 1% of Ruby programs that contain the same function.

Here I would like to introduce a very easy to use open source Go compilation tool: golang-builder, and show you an actual example

Program code:

Package main / / import "github.com/CenturyLinkLabs/hello" import "fmt" func main () {fmt.Println ("Hello World")}

Dockerfile:

FROM scratchCOPY hello / ENTRYPOINT ["/ hello"]

Package it into an image through golang-builder:

Docker run-- rm\-v $(pwd): / src\-v / var/run/docker.sock:/var/run/docker.sock\ centurylink/golang-builder

Check the image size (tested under Mac):

Docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEhello latest 1a42948d3224 24 seconds ago 1.59 MB so far, I believe you have a deeper understanding of "how to streamline Docker images". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report