How to run two JARs automatically on the "docker launch container"

I want two separate JAR files to run automatically as soon as the docker run mycontainer container is called through the launch command, so when I type docker run mycontainer , they are both called. So far I have a docker file that looks like this:

 # base image is java:8 (ubuntu) FROM java:8 # add files to image ADD first.jar . ADD second.jar . # start on run CMD ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "first.jar"] CMD ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "second.jar"] 

This, however, is just beginning second.jar.

Now both banks are servers in a cycle, so I think that when one of them starts, it just blocks the terminal. If I run the container using run -it mycontainer bash and name them manually, the first one will exit it and I cannot start the other.

Is there a way to open different terminals and switch between them so that each JAR works in its own context? Preferably already in the docker file.

I don't know anything about ubuntu, but I found the xterm command that opens a new terminal, however this will not work after calling the JAR. I am looking for instructions for a docker file, for example, to open a new terminal, execute first.jar, alt-tab in the old terminal and execute second.jar, or at least achieve the same.

Thanks!

+5
source share
4 answers

The second CMD command replaces the first, so you need to use one command for both commands.

Easy (not very good) approach

You can add a bash script that executes both commands and blocks on the second:

 # start.sh /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar first.jar & /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar second.jar 

Then change the Docker file to this:

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

When using docker stop it may not work properly, see https://blog.phusion.nl/2015/01/20/docker-and-the-pid-1-zombie-reaping-problem/

Best approach

To solve this problem you can use Phusion: https://hub.docker.com/r/phusion/baseimage/
It has an initialization system, which is much easier to use than, for example, supervisord.

Here is a good starting point: https://github.com/phusion/baseimage-docker#getting_started

Phusion instructions

Unfortunately, there is no official openjdk-8-jdk for Ubuntu 14.04 LTS. You can try with unofficial ppa, which is used in the following explanation.

In your case, you will need bash scripts (which act as "services"):

 # start-first.sh (the file has to start with the following line!): #!/bin/bash usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar /root/first.jar # start-second.sh #!/bin/bash usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar /root/second.jar 

And your Docker file will look like this:

 # base image is phusion FROM phusion/baseimage:latest # Use init service of phusion CMD ["/sbin/my_init"] # Install unofficial openjdk-8 RUN add-apt-repository ppa:openjdk-r/ppa && apt-get update && apt-get dist-upgrade -y && apt-get install -y openjdk-8-jdk ADD first.jar /root/first.jar ADD second.jar /root/second.jar # Add first service RUN mkdir /etc/service/first ADD start-first.sh /etc/service/first/run RUN chmod +x /etc/service/first/run # Add second service RUN mkdir /etc/service/second ADD start-second.sh /etc/service/second/run RUN chmod +x /etc/service/second/run # Clean up RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* 

This should install two services that will start at startup and close correctly when using docker stop .

+2
source

A Docker container has only one process at startup.

After that you can create several processes .:

+2
source

You have several options. Many answers mentioned using a supervisor for this, which is a great solution. Here are some others:

Create a short script that just runs both banks. Add this to your CMD. For example, a script we will call run_jars.sh might look like this:

 /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar first.jar; /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar second.jar; 

Then your CMD will be CMD sh run_jars.sh

Another alternative is to simply run two separate containers - one for first.jar and one for second.jar . You can run each of them through docker run , for example:

docker run my_repo/my_image:some_tag /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar second.jar

+1
source

If you want to run two different processes inside the same docker container (not recommended behavior), you can use something like supervisord

0
source

Source: https://habr.com/ru/post/1234283/


All Articles