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!
Might be too late to be of any use, but I just recently built a multi-module maven project for a college assignment and dockerized it.
Your docker-compose.yml should look something like this:
version: '3'
services:
server:
build: server
client:
build: client
environment:
SERVER_HOST: server
depends_on:
- server
Your servers Dockerfile something like this:
FROM openjdk:8-jre-alpine
COPY target/server-jar-with-dependencies.jar /server.jar
CMD /usr/bin/java -jar /server.jar
And your clients Dockerfile something like this:
FROM openjdk:8-jre-alpine
ENV SERVER_HOST=localhost
COPY target/client-jar-with-dependencies.jar /client.jar
CMD /usr/bin/java -jar /client.jar $SERVER_HOST
Note the $SERVER_HOST variable passed to the client, this will allow you to do the following in the client code:
Registry registry = LocateRegistry.getRegistry(args[0]);