简体   繁体   English

如何在Dockerfile中运行多个JAR?

[英]How can I run multiple JARs in Dockerfile?

I have a simple Java code with three main classes. 我有一个带有三个主要类的简单Java代码。 I want to build 3 different JARs out of it and then add those JARs to my Dockerfile and call each JAR in a different Docker image. 我想从中构建3个不同的JAR,然后将这些JAR添加到我的Dockerfile中,并在不同的Docker映像中调用每个JAR。 How can I do it? 我该怎么做?

The Docker run command accepts an optional COMMAND argument. Docker run命令接受可选的COMMAND参数。 You can simply add 3 JARs to the Docker image and specify which to run via Docker command. 您只需将3个JAR添加到Docker映像中,然后指定要通过Docker命令运行的JAR。

On the other hand, if you're willing to create multiple images of a single Dockerfile, docker currently supports multi-stage builds (that actually create multiple images) but does not allow you to tag every one of them. 另一方面,如果您愿意为单个Dockerfile创建多个映像,则docker当前支持多阶段构建(实际上创建多个映像),但不允许您标记每个映像。

Adding bash script to execute multiple commands and blocks: 添加bash脚本以执行多个命令和块:

#start.sh
/usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar MyFirst.jar &
/usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar MySecond.jar
... etc

Change your Dockerfile: 更改您的Dockerfile:

# base image is java:8 (ubuntu)
FROM java:8

    # add files to image 
    ADD first.jar .
    ADD second.jar .
    ...
    ADD start.sh .

    # start on run
    CMD ["bash", "start.sh"]

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

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