简体   繁体   中英

How can I run multiple JARs in Dockerfile?

I have a simple Java code with three main classes. 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. How can I do it?

The Docker run command accepts an optional COMMAND argument. You can simply add 3 JARs to the Docker image and specify which to run via Docker command.

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.

Adding bash script to execute multiple commands and blocks:

#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:

# 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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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