java.rmi.ConnectException:连接拒绝托管:docker 容器中的本地主机

java.rmi.ConnectException: Connection refused to host: localhost in docker containers

我正在尝试 运行 一些通过 java RMI 相互连接的 docker 容器。这在 docker 容器之外工作(使用 sudo),但是当 运行 通过 docker-compose.

进入时我得到一个 java.rmi.ConnectException: Connection refused to host: localhost 异常

我尝试了 network_mode 的多种变体,并在客户端的 java 命令中将 localhost 作为参数传递,但没有成功

Docker服务器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"]

Docker客户档案

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-编写文件

services:
    broker:
      build: broker
    customer:
      build: customer
      environment:
        SERVER_HOST: server1
      depends_on:
        - server1
      network_mode: host

错误:

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

发现我可以通过为所有模块设置 network_mode: host 来为我的本地主机用例获得有效连接。