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

Ran*_*ery 3 java docker dockerfile

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

FROM openjdk:latest
COPY . /usr/src/APP
WORKDIR /usr/src/APP`
Run Code Online (Sandbox Code Playgroud)

我想添加行来运行主要方法。没有 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'`
Run Code Online (Sandbox Code Playgroud)

是否可以在一个 docker 容器中运行上述场景?如果可以的话,一个 Dockerfile 中只能有一条指令,ENTRYPOINT怎么办?CMD

Dav*_*aze 5

“如何从一个映像运行多个进程”的通常答案是运行多个容器。鉴于您显示的 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
Run Code Online (Sandbox Code Playgroud)

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

#!/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" "$@"
Run Code Online (Sandbox Code Playgroud)

将其复制到图像中

COPY run_main.sh /usr/local/bin
Run Code Online (Sandbox Code Playgroud)

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

docker run -d --name app1 run_main.sh class1
Run Code Online (Sandbox Code Playgroud)