简体   繁体   English

如何减小 Java 应用程序的 docker 图像大小

[英]How to reduce docker image size for Java application

i have the following dockerfile code but the image with over 500mb is way to large.我有以下 dockerfile 代码,但超过 500mb 的图像太大了。 I tried to use multi stage build in order so reduce the size but after that the kubernetes deployment wont work.我尝试使用多阶段构建以减小尺寸,但之后 kubernetes 部署将无法正常工作。

FROM openjdk:8
ADD target/name_id.jar name_id.jar
EXPOSE 8080
ENTRYPOINT [ "java","-jar","name_id.jar" ]

With this file it works.有了这个文件,它就可以工作了。 Can someone help me?有人能帮我吗? For the project itself maven and springboot are used.对于项目本身,使用了 maven 和 springboot。 I also have to clean install the jar everytime if i change something in the code.如果我更改代码中的某些内容,我还必须每次都清理安装 jar。

Can you use openjdk:8-alpine, size of the base image will be 144.9MB.你可以使用 openjdk:8-alpine,基础镜像的大小为 144.9MB。

Use below image for less size • openjdk:8-jre-alpine: 84 MB使用下图缩小尺寸 • openjdk:8-jre-alpine: 84 MB

Or you can also try to install alpine os which is 5 MB and openjdk separately and check sizes或者您也可以尝试分别安装 5 MB 的 alpine os 和 openjdk 并检查大小

Personally, there are two things that come to mind when I think of minimizing the docker image就个人而言,当我想到最小化 docker 图像时,我想到了两件事

  1. Base Images基础图像
  2. Multistage Builds多阶段构建

Le me try to answer your question I would used the small jdk image ie openjdk:8-alpine I would used this if I wanted to create the jar file inside the Dockerfile AND execute it.我试着回答你的问题我会使用小的 jdk 图像,即 openjdk:8-alpine 如果我想在 Dockerfile 中创建 jar 文件并执行它,我会使用它。 In you case, you just want to use an already existing jar file.在您的情况下,您只想使用已经存在的 jar 文件。

Since you already have and want to use the jar file named name_id.jar, Then you just need the java runtime environment ie openjdk:8-jre-alpine rather than openjdk:8-alpine既然您已经拥有并想要使用名为 name_id.jar 的 jar 文件,那么您只需要 java 运行时环境即 openjdkk8:-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM