Java 如何使用文件和值参数为本地应用程序创建 docker 镜像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25364940/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:13:19  来源:igfitidea点击:

How to create docker image for local application taking file and value parameters

javavirtual-machinedocker

提问by mangusbrother

I have a java application (jar file) that I want to be able to run from a docker image.

我有一个 java 应用程序(jar 文件),我希望能够从 docker 映像运行。

I have created a Dockerfile to create an image using centos as base and install java as such:

我已经创建了一个 Dockerfile 来创建一个使用 centos 作为基础的镜像并安装 java:

Dockerfile
FROM centos
RUN yum install -y java-1.7.0-openjdk

I ran docker build -t me/java7after to obtain the image me/java7

我跑去docker build -t me/java7获取图像 me/java7

however I am stuck at some dead ends.

但是我陷入了一些死胡同。

  1. How do I copy the jar file from the host into the image/container
  2. I require 2 parameters. 1 is a file, which needs to be copied into a directory into the container at runtime. The other is a number which needs to be passed to the jar file in the java -jarcommand automatically when the user runs docker runwith the parameters
  1. 如何将 jar 文件从主机复制到图像/容器中
  2. 我需要2个参数。1是一个文件,需要在运行时复制到容器的目录中。另一个是一个数字,java -jar当用户docker run带参数运行时需要自动传递给命令中的jar文件

Extra Notes:

额外说明:

The jar file is a local file. Not hosted anywhere accessible via wget or anything. The closest I have at the moment is a windows share containing it. I could also access the source from a git repository but that would involve compiling everything and installing maven and git on the image so I'd rather avoid that.

jar 文件是本地文件。未托管在任何可通过 wget 或任何东西访问的地方。我目前最接近的是包含它的 Windows 共享。我也可以从 git 存储库访问源代码,但这将涉及编译所有内容并在映像上安装 maven 和 git,因此我宁愿避免这种情况。

any help is much appreciated.

任何帮助深表感谢。

采纳答案by frvi

  1. In the Dockerfile, add a local file using ADD, e g

    ADD your-local.jar /some-container-location
    
  2. You could use volumes to put a file in the container in runtime, e g

    VOLUME /copy-into-this-dir
    

    And then you run using

    docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7
    

    You can use ENTRYPOINT and CMD to pass arguments, e g

    ENTRYPOINT ["java", "-jar", "/whatever/your.jar"]
    CMD [""]
    

    And then again run using

    docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7 --myNumber 42
    
  1. 在Dockerfile中,使用ADD添加本地文件,例如

    ADD your-local.jar /some-container-location
    
  2. 您可以使用卷在运行时将文件放入容器中,例如

    VOLUME /copy-into-this-dir
    

    然后你运行使用

    docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7
    

    您可以使用 ENTRYPOINT 和 CMD 来传递参数,例如

    ENTRYPOINT ["java", "-jar", "/whatever/your.jar"]
    CMD [""]
    

    然后再次运行使用

    docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7 --myNumber 42
    

(Have a look at the Dockerfile documentation.)

(查看Dockerfile 文档。)

回答by Riddhi Gohil

Suppose your file structure is as follow :

假设您的文件结构如下

DockerTest
        └── Dockerfile
        └── local.jar

Dockerfile content will be :

Dockerfile 内容将是

FROM centos
RUN yum install -y java-1.7.0-openjdk
EXPOSE 8080
ADD /local.jar fatJar.jar
ENTRYPOINT ["java","-jar","fatJar.jar"]

Use following command :

使用以下命令

$ cd DockerTest
$ docker build -f Dockerfile -t demo .