简体   繁体   English

为什么我们需要 java 应用程序的 dockerfile 中的 FROM?

[英]Why do we need FROM in dockerfile for a java app?

I have a very basic question about FROM line in dockerfile for a java application, like a spring boot app.对于 java 应用程序,例如 spring 启动应用程序,我有一个关于 dockerfile 中的 FROM 行的非常基本的问题。

So we write it like:所以我们这样写:

FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

In line 3 we are basically telling docker to use the application jar inside target folder for running the app inside container.在第 3 行中,我们基本上是告诉 docker 使用目标文件夹内的应用程序 jar 在容器内运行应用程序。 Now when a jar is build for a java application, build process takes into account all the needed dependencies like those from jdk or those defined in POM etc.现在,当为 java 应用程序构建 jar 时,构建过程会考虑所有需要的依赖项,例如来自 jdk 或在 POM 等中定义的依赖项。

So then why do we need FROM line?那么为什么我们需要 FROM 行呢? We already have all in our jar, isn't?我们的 jar 中已经包含所有内容,不是吗?

The FROM argument describes a base image to use. FROM 参数描述要使用的基本映像。 https://docs.docker.com/engine/reference/builder/#from https://docs.docker.com/engine/reference/builder/#from

An analogy in a non containerized world would be sitting down to a computer freshly installed with some OS and various dependencies already setup for you.在非容器化世界中的一个类比是坐在一台新安装的计算机上,该计算机已经为您设置了一些操作系统和各种依赖项。 Handy!便利!

In this specific case, you are getting an image from OpenJDK.在这种特定情况下,您将从 OpenJDK 获取图像。 https://hub.docker.com/_/openjdk . https://hub.docker.com/_/openjdk

Edit to add detail: As a number of comments state, Java needs the JVM installed on some OS to run a Java program. Edit to add detail: As a number of comments state, Java needs the JVM installed on some OS to run a Java program. If you look down at image variants in the docker hub link, you'll see that you are requesting an image with Debian Linux and OpenJDK 11.如果您查看 docker 集线器链接中的图像变体,您会看到您正在请求带有 Debian Linux 和 OpenJDK 11 的图像。

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

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