I am trying to run a number of docker containers that connect to each other via java RMI. This works outside of the docker containers (with sudo) but I get an java.rmi.ConnectException: Connection refused to host: localhost
exception when running it in via docker-compose
.
I have tried a number of variations of network_mode
and passing localhost
as an argument in the java command of the client to no success
Dockerfile of Server 1
FROM openjdk:8-jre-alpine
COPY target/server1-1.0.0-jar-with-dependencies.jar /server1-1.0.0-jar-with-dependencies.jar
CMD ["/usr/bin/java", "-cp", "/server1-1.0.0-jar-with-dependencies.jar", "Server"]
Dockerfile of Customer
FROM openjdk:8-jre-alpine
COPY target/customer-1.0.0-jar-with-dependencies.jar /customer-1.0.0-jar-with-dependencies.jar
CMD ["/usr/bin/java", "-cp", "/customer-1.0.0-jar-with-dependencies.jar", "Main","localhost"]
Docker-compose file
services:
broker:
build: broker
customer:
build: customer
environment:
SERVER_HOST: server1
depends_on:
- server1
network_mode: host
Error:
Successfully tagged project_customer:latest
Starting project_server1_1 ... done
Recreating quoco-rmi_client_1 ... done
Attaching to project_server1_1, project_customer_1
server1_1 | waiting for connection
customer_1 | Exception in thread "main" java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
customer_1 | java.net.ConnectException: Connection refused (Connection refused)
customer_1 | at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
customer_1 | at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
customer_1 | at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
customer_1 | at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338)
customer_1 | at sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:112)
customer_1 | at Main.main(Main.java:24)
customer_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
customer_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
customer_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
customer_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
customer_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
customer_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
customer_1 | at java.net.Socket.connect(Socket.java:589)
customer_1 | at java.net.Socket.connect(Socket.java:538)
customer_1 | at java.net.Socket.<init>(Socket.java:434)
customer_1 | at java.net.Socket.<init>(Socket.java:211)
customer_1 | at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
customer_1 | at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
customer_1 | at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
customer_1 | ... 5 more
project_customer_1 exited with code 1
CodePudding user response:
Found I could get a valid connection for my localhost usecase by setting network_mode: host
for all modules.