I've been trying to get a Java RMI application to work in docker as a test for some time now.
The application works when both components are on the same PC, when the "server" is in docker not.
How can I execute a remote method that I've dockerized?
There is a snippet of my Client:
Registry registry = LocateRegistry.getRegistry(DOCKER_EXPOSED_PORT);
// this prints any found registry names
String[] s = registry.list();
System.out.println(s[0]);
CommonInterface stub = (CommonInterface) registry.lookup("RegisteredStub");
int response = stub.CommonMethod();
And my "server":
CommonInterface stub = (CommonInterface) UnicastRemoteObject.exportObject(obj, 2000);
LocateRegistry.createRegistry(2000);
Registry registry = LocateRegistry.getRegistry(2000);
registry.bind("RegisteredStub", stub);
I also use System.out.println(Inet4Address.getLocalHost().getHostAddress());
to get the IP address of both components, they are the same when ran locally but diffrent when the server is in docker as expected.
My client is able to "read" the registered stub from the docker container so if I rename it to RegisteredStub2 then
String[] s = registry.list();
System.out.println(s[0]);
prints RegisteredStub2 so somehow my client can see the registry in docker but can not access the remote method.
The client crashes with: java.net.ConnectException: Connection timed out: connect java.rmi.ConnectException: Connection refused to host: DOCKER IP;
when I call the remote method int response = stub.CommonMethod(); from the desktop client.
Here are my docker file commands:
EXPOSE 2000
CMD ["./rmiregistry 2000"]
CMD ["./rmid -J-Djava.security.policy=rmid.policy -port 2000"]
Thanks!