gpt4 book ai didi

java - 从同一个 Dockerfile 运行多个主要方法

转载 作者:行者123 更新时间:2023-12-02 01:13:24 26 4
gpt4 key购买 nike

我有一个大型 java 应用程序,在不同的类中有 5 个主要方法。我想将此应用程序作为 Docker 容器运行。来自 DockerHub OpenJDK Image ,我按如下方式启动了我的 Dockerfile

FROM openjdk:latest
COPY . /usr/src/APP
WORKDIR /usr/src/APP`

我想添加行来运行主要方法。没有 Docker,我使用以下几行运行应用程序

echo 'Starting App'
nohup $JAVA_HOME/bin/java .:./App.jar path.to.main.class1 >>
/path/to/nohup/nohup.out 2>&1 &
nohup $JAVA_HOME/bin/java .:./App.jar path.to.main.class2 >>
/path/to/nohup/nohup.out 2>&1 &
nohup $JAVA_HOME/bin/java .:./App.jar path.to.main.class3 >>
/path/to/nohup/nohup.out 2>&1 &
nohup $JAVA_HOME/bin/java .:./App.jar path.to.main.class4 >>
/path/to/nohup/nohup.out 2>&1 &
nohup $JAVA_HOME/bin/java .:./App.jar path.to.main.class5 >>
/path/to/nohup/nohup.out 2>&1 &
echo 'App Started Successfully'`

是否可以在一个 docker 容器中运行上述场景?如果可以的话,在一个 Dockerfile 中只能有一个 ENTRYPOINT 和 CMD 指令的情况下如何实现?

最佳答案

“如何从一个镜像运行多个进程”的通常答案是运行多个容器。鉴于您显示的 Dockerfile,这相当简单:

# Build the image (once)
docker build -t myapp .

# Then run the five containers as background processes
docker run -d --name app1 java .:./App.jar path.to.main.class1
docker run -d --name app2 java .:./App.jar path.to.main.class2
docker run -d --name app3 java .:./App.jar path.to.main.class3
docker run -d --name app4 java .:./App.jar path.to.main.class4
docker run -d --name app5 java .:./App.jar path.to.main.class5

由于所有命令都非常相似,您可以编写一个脚本来运行它们

#!/bin/sh

# Use the first command-line argument as the main class
MAIN_CLASS="$1"
shift

# Can also set JAVA_OPTS, other environment variables, ...

# Run the application
exec java -jar App.jar "path.to.main.$MAIN_CLASS" "$@"

将其复制到图像中

COPY run_main.sh /usr/local/bin

然后当您启动容器时,只需运行该包装器

docker run -d --name app1 run_main.sh class1

关于java - 从同一个 Dockerfile 运行多个主要方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59032326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com