How to have two JARs start automatically on "docker run container" How to have two JARs start automatically on "docker run container" shell shell

How to have two JARs start automatically on "docker run container"


The second CMD instruction replaces the first, so you need to use a single instruction for both commands.

Easy (not so good) Approach

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

# 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 your Dockerfile 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 runCMD ["bash", "start.sh"]

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

Better Approach

To solve this, you could use Phusion:https://hub.docker.com/r/phusion/baseimage/
It has an init-system that is much easier to use than e.g. supervisord.

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

Instructions for using phusion

Sadly there is not official openjdk-8-jdk available for Ubuntu 14.04 LTS. You could try with an inofficial ppa, which is used in the following explanation.

In your case you would need to bash scripts (which act like "services"):

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

And your Dockerfile would look like this:

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

This should install two services which will be run on startup and shut down properly when using docker stop.


A Docker container has only a single process when it is started.

You can still create several processes afterward.:


You have a few options. A lot of the answers have mentioned using supervisor for this, which is a fine solution. Here are some others:

Create a short script that just kicks off both jars. Add that to your CMD. For example, the script, which we'll call run_jars.sh could look like:

/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 would be CMD sh run_jars.sh

Another alternative is just running two separate containers-- one for first.jar and the other for second.jar. You can run each one 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