0

Even though docker is meant to run a single service in one container, for some reason I want to run two services that communicate with each other in one container. I have two spring boot services: Demo and Demo2

To do that, I made a shell script to run two jars of two applications and then pass that script to Dockerfile's Entrypoint.

shell script (invoke.sh)

#!/bin/bash
java -jar app.jar && java -jar app2.jar

Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp

COPY demo/demo/target/demo-0.0.1-SNAPSHOT.jar app.jar
COPY demo2/demo2/target/demo2-0.0.1-SNAPSHOT.jar app2.jar

COPY invoke.sh invoke.sh

ENTRYPOINT ["sh","/invoke.sh"]

Image creation is successful. But when I run that image it only starts the first spring boot application. command prompt  output

Why it doesn't start the app2.jar? What shoud I do to start both applications? Any help to solve this issue would be highly appriciated.

  • you can run single service on single port.If you want to run two services which are communicating each other,better to use docker-compose.so the your services also get communicates with each other because of same network. – Bhagyashri Machale Jun 21 '20 at 03:58
  • @BhagyashriMachale But if I use docker-compose it creates two containers right? what I want is one container running two services – Nishadi Wickramanayaka Jun 21 '20 at 05:35
  • you can run multiple services in single container using Supervisord.. [link](https://stackoverflow.com/a/19978556/11204536) – Bhagyashri Machale Jun 21 '20 at 06:42
  • https://docs.docker.com/config/containers/multi-service_container/ – Zeitounator Jun 21 '20 at 06:43
  • You'd generally run two separate services like this in two separate containers. Why do you specifically want them in one? If you do run them in one container, what will you do if one of the processes fails? – David Maze Jun 21 '20 at 10:37
  • @BhagyashriMachale thank you for the reference. But using Supervisord is a heavy process. This method also should work right? – Nishadi Wickramanayaka Jun 21 '20 at 10:38
  • @DavidMaze Yes, the ideal case is running two services in two containers. This is not for any commercial application and this is related to my research work. So I really want to examine this scenario too for some purpose. – Nishadi Wickramanayaka Jun 21 '20 at 10:47
  • @Zeitounator Thank you for the reference. Literally I'm trying the first method of that reference page. – Nishadi Wickramanayaka Jun 21 '20 at 10:49

2 Answers2

0

The second application will start once the first application exits with the exit status 0.

 #!/bin/bash
java -jar app.jar && java -jar app2.jar

This guarantees the sequential execution of the jar files.

0

Replace the double ampersand with single one:

java -jar app.jar & java -jar app2.jar

It will run both applications in parallel. Of course there will be little bit chaos in the log.

czs
  • 185
  • 2
  • 12