Spring Boot的Docker多阶段构建指南

[

[

Catherine Edelveis

](https://medium.com/@cat.edelveis?source=post_page-----08e3a64c9812--------------------------------)

](https://medium.com/@cat.edelveis?source=post_page-----08e3a64c9812--------------------------------)

·

·

Follow

关注

There are several tried-and-true methods of reducing the size of Docker container images. But your container image may still bloat with time if you frequently introduce updates.

有几种经过验证的减小Docker容器镜像大小的方法。但是,如果您经常引入更新,您的容器镜像可能会随着时间的推移而膨胀。

So how to keep your containers as neat as a pin at all times? The answer is: multi-stage builds. Plus, if you work with Spring Boot, the framework offers a mighty feature, layered jars, which complements multi-stage builds and will help you organize the layers in your final image nicely.

那么如何始终保持容器整洁呢?答案是:多阶段构建。此外,如果您使用Spring Boot,该框架提供了一个强大的功能,即分层的jar文件,它可以补充多阶段构建,并帮助您在最终镜像中组织层次结构。

Before multi-stage builds: layer upon layer upon layer

在多阶段构建之前:层叠层叠层

Each command in a Dockerfile creates a new layer, which is then added to the final container image. Developers have to navigate between Scylla and Charybdis and choose between the lower number of layers and more pull-efficient layers.

Dockerfile中的每个命令都会创建一个新的层,然后将其添加到最终的容器镜像中。开发人员必须在Scylla和Charybdis之间进行选择,选择层数较少但拉取效率更高的层。

One feasible solution to the issue is to write separate Dockerfiles, but it makes the process of building the image more complex: you have to maintain several Dockerfiles and plus use specific scripts to tie them all together.

解决这个问题的一种可行方法是编写单独的Dockerfile,但这会使构建镜像的过程更加复杂:您必须维护多个Dockerfile,并使用特定的脚本将它们全部连接在一起。

But starting with Docker 17.05, we can use one Dockerfile to control all images!

但是从Docker 17.05开始,我们可以使用一个Dockerfile来控制所有镜像!

How multi-stage builds change the game

多阶段构建如何改变游戏规则

So how exactly do multi-stage Docker builds work?

那么多阶段的Docker构建是如何工作的呢?

You write one Dockerfile that contains several FROM statements (the number is not limited). Each FROM statement uses its own base (you can use the same base image or different ones depending on your purposes) and starts a new build phase. Each phase ca...

开通本站会员,查看完整译文。

首页 - Wiki
Copyright © 2011-2024 iteam. Current version is 2.132.0. UTC+08:00, 2024-09-20 01:06
浙ICP备14020137号-1 $访客地图$