Java Docker 图像 - 类型。修身 vs 修身拉伸 vs 拉伸 vs 高山
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54954187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker images - types. Slim vs slim-stretch vs stretch vs alpine
提问by mailtobash
I am looking to pick up a docker image to build a java app and looking at the variants of the OpenJDK images available. I am looking here https://github.com/docker-library/openjdk/tree/master/8/jdkand see alpine, slim and windows. What are the differences between these and what does each variant give?
我正在寻找一个 docker 镜像来构建一个 java 应用程序并查看可用的 OpenJDK 镜像的变体。我在这里查看https://github.com/docker-library/openjdk/tree/master/8/jdk并查看 alpine、slim 和 windows。这些之间有什么区别以及每个变体给出了什么?
采纳答案by acdcjunior
Per docker library docs (quote and links below), here's a summary:
根据 docker 库文档(下面的引用和链接),这是一个摘要:
openjdk:<version>
openjdk:<version>
The defacto image. Use it if unsure.
事实上的形象。如果不确定,请使用它。
openjdk:<version>-buster
,openjdk:<version>-stretch
andopenjdk:<version>-jessie
openjdk:<version>-buster
,openjdk:<version>-stretch
和openjdk:<version>-jessie
buster
, jessie
or stretch
are the suite code names for releases of Debianand indicate which releasethe image is based on.
buster
,jessie
或者stretch
是Debian发行版的套件代码名称,并指示映像基于哪个发行版。
openjdk:<version>-alpine
openjdk:<version>-alpine
Similarly, this image is based on the Alpine Linux, thus being a very small base image. It is recommended if you need an image size is as small as possible. The caveat is that it uses some unusual libs, but shouldn't be a problem for most software. In doubt, check the official docs below.
同样,这个镜像是基于Alpine Linux 的,因此是一个非常小的基础镜像。如果您需要图像尺寸尽可能小,建议使用。需要注意的是,它使用了一些不寻常的库,但对于大多数软件来说应该不是问题。如有疑问,请查看下面的官方文档。
openjdk:<version>
(from 12 onwards),openjdk:<version>-oracle
andopenjdk:<version>-oraclelinux7
openjdk:<version>
(从 12 日起),openjdk:<version>-oracle
以及openjdk:<version>-oraclelinux7
Starting with openjdk:12
the default image as well as the -oracle
and -oraclelinux7
variants are based on the official Oracle Linux 7 image.
The OpenJDK binaries in the default image as well as the -oracle
and -oraclelinux7
variants are built by Oracle and are sourced from the OpenJDK community.
从openjdk:12
默认映像开始,以及-oracle
和-oraclelinux7
变体都基于官方Oracle Linux 7 映像。默认映像中的 OpenJDK 二进制文件以及-oracle
和-oraclelinux7
变体由 Oracle 构建并来自OpenJDK 社区。
openjdk:<version>-slim
openjdk:<version>-slim
This image only contains the minimal packages needed to run Java (and is missing many of the UI-related Java libraries, for instance). Unless you are working in an environment where onlythe openjdk
image will be deployed and you have space constraints, the default image is recommended over this one.
此映像仅包含运行 Java 所需的最少包(例如,缺少许多与 UI 相关的 Java 库)。除非你是在一个环境中工作,其中仅该openjdk
图像将被部署,你有空间的限制,默认的图像,建议在这一个。
openjdk:<version>-windowsservercore
openjdk:<version>-windowsservercore
This image is based on Windows Server Core (microsoft/windowsservercore
).
此映像基于Windows Server Core ( microsoft/windowsservercore
)。
Full docs (version shown below here, latest version here):
Image Variants
The
openjdk
images come in many flavors, each designed for a specific use case.
openjdk:<version>
This is the defacto image. If you are unsure about what your needs are, you probably want to use this one. It is designed to be used both as a throw away container (mount your source code and start the container to start your app), as well as the base to build other images off of.
Some of these tags may have names like jessie or stretch in them. These are the suite code names for releases of Debianand indicate which release the image is based on.
openjdk:<version>-alpine
This image is based on the popular Alpine Linux project, available in the
alpine
official image. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.This variant is highly recommended when final image size being as small as possible is desired. The main caveat to note is that it does use musl libcinstead of glibc and friends, so certain software might run into issues depending on the depth of their libc requirements. However, most software doesn't have an issue with this, so this variant is usually a very safe choice. See this Hacker News comment threadfor more discussion of the issues that might arise and some pro/con comparisons of using Alpine-based images.
To minimize image size, it's uncommon for additional related tools (such as
git
orbash
) to be included in Alpine-based images. Using this image as a base, add the things you need in your own Dockerfile (see thealpine
image descriptionfor examples of how to install packages if you are unfamiliar).
openjdk:<version>-windowsservercore
This image is based on Windows Server Core (
microsoft/windowsservercore
). As such, it only works in places which that image does, such as Windows 10 Professional/Enterprise (Anniversary Edition) or Windows Server 2016.For information about how to get Docker running on Windows, please see the relevant "Quick Start" guide provided by Microsoft:
openjdk:<version>-slim
This image installs the
-headless
package of OpenJDK and so is missing many of the UI-related Java libraries and some common packages contained in the default tag. It only contains the minimal packages needed to run Java. Unless you are working in an environment where onlytheopenjdk
image will be deployed and you have space constraints, we highly recommend using the default image of this repository.
图像变体
这些
openjdk
图像有多种风格,每种风格都是为特定用例设计的。
openjdk:<version>
这是事实上的形象。如果您不确定自己的需求是什么,您可能想使用这个。它旨在用作一次性容器(安装您的源代码并启动容器以启动您的应用程序),以及构建其他镜像的基础。
其中一些标签可能有像 jessie 或 stretch 这样的名字。这些是Debian发行版的套件代码名称, 并指示映像基于哪个发行版。
openjdk:<version>-alpine
该图像是基于流行的高山Linux项目,可用的
alpine
官方图片。Alpine Linux 比大多数发行版基础镜像(~5MB)小得多,因此通常会导致镜像更薄。当需要尽可能小的最终图像大小时,强烈建议使用此变体。需要注意的主要警告是它确实使用musl libc而不是glibc 和朋友,因此某些软件可能会遇到问题,具体取决于其 libc 要求的深度。然而,大多数软件都没有这个问题,所以这个变体通常是一个非常安全的选择。有关可能出现的问题的更多讨论以及使用基于 Alpine 的图像的一些利弊比较,请参阅此 Hacker News 评论线程。
为了最小化图像大小,在基于 Alpine 的图像中包含其他相关工具(例如
git
或bash
)并不常见。使用这个镜像作为基础,在你自己的 Dockerfile 中添加你需要的东西(如果你不熟悉,请参阅alpine
镜像描述以获取如何安装包的示例)。
openjdk:<version>-windowsservercore
此映像基于Windows Server Core (
microsoft/windowsservercore
)。因此,它仅适用于该映像适用的地方,例如 Windows 10 Professional/Enterprise(周年纪念版)或 Windows Server 2016。有关如何在 Windows 上运行 Docker 的信息,请参阅 Microsoft 提供的相关“快速入门”指南:
openjdk:<version>-slim
此映像安装了
-headless
OpenJDK的包,因此缺少许多与 UI 相关的 Java 库和默认标记中包含的一些常见包。它只包含运行 Java 所需的最少的包。除非你是在一个环境中工作,其中 仅该openjdk
图像将被部署,你有空间的限制,我们强烈建议您使用此库的默认图像。
回答by Ghasem Sadeghi
Choose a base docker image that fits your needsand please keep in mind that Image sizeis an important aspectalso.
选择适合您需求的基本泊坞窗图像,请记住图像大小也是一个重要方面。
Image can be considered as a set of instructions on how to create the container. In Docker, one image could be inherited from (or based on) another image, adding additional instructions on top of base ones. Each image consists of multiple layers, which are effectively immutable.
镜像可以被视为一组关于如何创建容器的说明。在 Docker 中,一个镜像可以从(或基于)另一个镜像继承,在基础镜像之上添加额外的指令。每个图像由多个层组成,这些层实际上是不可变的。
Plase read Crafting the perfect Java Docker build flowarticle.
请阅读打造完美的 Java Docker 构建流程文章。
Docker image size is actually very important. The size has an impact on:
- network latency: need to transfer Docker image over the web
- storage: need to store all these bits somewhere
- service availability and elasticity: when using a Docker scheduler, like Kubernetes, Swarm, Nomad, DC/OS or other (the scheduler can move containers between hosts)
- security: do you really, I mean really need the libpng package with all its CVE vulnerabilities for your Java application?
- development agility: small Docker images == faster build time and faster deployment
Docker 镜像大小实际上非常重要。大小对以下方面有影响:
- 网络延迟:需要通过网络传输 Docker 镜像
- 存储:需要将所有这些位存储在某处
- 服务可用性和弹性:当使用 Docker 调度程序时,如 Kubernetes、Swarm、Nomad、DC/OS 或其他(调度程序可以在主机之间移动容器)
- 安全性:您真的,我的意思是真的需要为您的 Java 应用程序提供带有所有 CVE 漏洞的 libpng 包吗?
- 开发敏捷性:小型 Docker 镜像 == 更快的构建时间和更快的部署
To run a java application you need JRE at least. For example, for a spring project your image can be based on slim Alpine Linux with OpenJDK JRE
:
要运行 Java 应用程序,您至少需要 JRE。例如,对于 spring 项目,您的图像可以基于slim Alpine Linux with OpenJDK JRE
:
#simple dockerFile for java app:
#here we are using Base Alpine Linux based image with OpenJDK JRE only
#For Java 8, try this
FROM openjdk:8-jre-alpine
#For Java 11, try this
#FROM adoptopenjdk/openjdk11:alpine-jre
#copy application WAR/JAR (with libraries inside)
COPY target/spring-boot-*.war/jar yourName.war/jar
# specify default command
CMD ["/usr/bin/java", "-jar", "/yourName.war/jar"]
Also you can use docker history yourImageName
to see all layers (and their size) that makes your image.
您还可以使用docker history yourImageName
查看构成图像的所有图层(及其大小)。