如何为本地应用程序创建Docker映像文件和值参数 [英] How to create docker image for local application taking file and value parameters

查看:121
本文介绍了如何为本地应用程序创建Docker映像文件和值参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已经创建了一个Docker文件来创建一个使用centos作为基础,并安装java本身:

  Dockerfile 
FROM centos
运行yum install -y java -1.7.0-openjdk

我运行 docker build -t me / java7 之后获取图像我/ java7



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


  1. 如何将jar文件从主机复制到图像/容器中

  2. 我需要2个参数。 1是一个文件,需要在运行时将其复制到容器中的目录中。另一个是当用户运行 docker run java -jar 命令中传递给jar文件的数字$ c>带有参数

额外注意:



该jar文件是一个本地文件。通过wget或任何东西无法访问任何地方。目前最接近的是包含它的Windows共享。我也可以从git存储库访问源代码,但这将涉及到编译所有内容,并在图像上安装maven和git,所以我宁愿避免使用。



任何帮助非常感谢。

解决方案


  1. 在Dockerfile中,使用ADD添加本地文件,例如

     添加your-local.jar / some-container-location 


  2. 您可以使用卷在运行时将文件放在容器中,例如

      VOLUME / copy-into-this-dir 

    然后运行使用

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

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

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

    然后再次使用

      docker运行-v = / l ocation / of / file / local:/ copy-into-this-dir -t me / java7 --myNumber 42 


(请查看 Dockerfile文档。)


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

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

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

I ran docker build -t me/java7 after to obtain the image 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 -jar command automatically when the user runs docker run with the parameters

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.

any help is much appreciated.

解决方案

  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
    

(Have a look at the Dockerfile documentation.)

这篇关于如何为本地应用程序创建Docker映像文件和值参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆