Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java process in Docker container doesn't exit on end of main()

I have a java jar command, and I'm calling it from inside python using Popen. I'm happy that I'm doing it right, and theres lots of reasons why I'm doing it this way (its not great, but it is what it is).

When I run the Java locally, it works fine (JRE 1.8), when I run the python locally that calls the Java, it works fine. When I run it inside a docker container, it just hangs - the python on the process.communicate() and when i do a docker exec, it just does not return (the output from the command is exactly as I expect).

What I've discovered that unless theres an explicit System.exit(0); at the end of the Java, then the Java process does not end when run inside of a docker container - it just hangs, at the point of all processing being done.

To check this, I made a Hello World app consisting of 5 lines (the System.exit i comment out or in to check that this works or doesn't)

public class Hello {
    public static void main(String[] args) {
        System.out.println("Heellllooo");
        // System.exit(0);
    }
}

I bundled this into a Jar and put it in the folder where the 'real' jar sits, and execute.

Without the system.exit() the process hangs. With it it exits cleanly. In both instances, 'Heelllloooo' is printed.

Other random facts that might help diagnosis the command i'm using to check is this: (the popen in python is much more complex with the arguments I'm passing into Java).

docker exec [tag] java -jar libs/Hello.jar

If I do:

docker exec [tag] java -version

it returns immediately with the version info.

I'm running a Mac, so this is running inside of a docker-machine boot2docker Ubuntu vm thing. Other people running the same docker-images on Debian and Ubuntu machines do not have any problems with this at all.

I don't believe the problem is anything to do with Python because the symptoms are there when running it from the exec (hence me not tagging it python).

My question: Why doesn't the java process exit when the main function returns?

To be clear, I don't want to know how to kill the process (I know how to do that), what I want to know is why the process does not return. (and this seems to be a specific java / docker thing).

like image 408
Jmons Avatar asked Dec 14 '15 11:12

Jmons


1 Answers

Unfortunately, this looks like a case of https://github.com/docker/docker/issues/18180, which is still (at least as of 2015-12-18) unresolved (besides downgrading the kernel or modifying the application code).

like image 195
tianon Avatar answered Oct 13 '22 11:10

tianon