Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
Java RMI in docker

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!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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]);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!