Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Multiple Main Methods from the same Dockerfile

I have a large scale java application with 5 Main methods in different classes. I want to run this application as a docker container. From DockerHub OpenJDK Image, I started my Dockerfile as follows

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

and I want to add the lines to run the main methods. Without Docker, I run the app using the below lines

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'`

Is it possible to run the above scenario in one docker container? If possible, how can it be done while there can only be one ENTRYPOINT and CMD instructions in a Dockerfile ?

like image 367
Randa ElBehery Avatar asked Jan 21 '26 03:01

Randa ElBehery


1 Answers

The usual answer to "how do I run multiple processes from one image" is to run multiple containers. Given the Dockerfile you show this is fairly straightforward:

# 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

Since all of the commands are pretty similar, you could write a script to run them

#!/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 that into the image

COPY run_main.sh /usr/local/bin

and then when you launch the containers just run that wrapper

docker run -d --name app1 run_main.sh class1
like image 146
David Maze Avatar answered Jan 22 '26 17:01

David Maze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!