You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose配置容器静态IP后跨容器通信失败问题求助

Assigning Static IPs to Docker Containers via Docker Compose (and Fixing RPC Communication Issues)

Absolutely, you can assign static IPs to Docker containers using Docker Compose—your configuration is almost there! Let’s troubleshoot why your eth-java container can’t reach eth via its static IP, even though dynamic IP communication works.

First, Let’s Confirm Your Setup’s Validity

Your Compose file structure for static IPs is correct: you’ve defined a custom bridge network with an IPAM subnet, and assigned static IPs to both containers within that subnet. The issue likely stems from one of these common pitfalls:


1. Old Network Configuration Might Be Sticking

Docker retains existing networks even if you update your Compose file. If you created my-network before adding the static IP configuration, it’s using the old default settings instead of your new IPAM rules.

Fix this by deleting the old network first:

docker network rm my-network

Then restart your containers with:

docker-compose up -d

2. The Ethereum Client Might Be Restricting RPC Access

This is the most likely culprit. By default, the ethereum/client-go (geth) binds its HTTP RPC interface only to 127.0.0.1, which means it won’t accept connections from other containers on the same network—even if you use the correct static IP.

Update your eth service in the Compose file to force geth to listen on all interfaces (0.0.0.0):

eth:
  image: ethereum/client-go
  container_name: eth
  # Add this command to open RPC to all interfaces
  command: --rinkeby --http --http.addr 0.0.0.0 --http.port 8545 --ws --ws.addr 0.0.0.0
  ports:
    - "8545:8545"
    - "30303:30303"
  networks:
    my-network:
      ipv4_address: 192.168.1.17
  volumes:
    - /storage/eth/rinkeby:/root/.ethereum/rinkeby/

3. Ensure Containers Start in the Right Order

If eth-java boots up before eth finishes initializing, its first RPC call will fail. Add a depends_on directive to eth-java to guarantee eth starts first:

eth-java:
  image: registry-intl.ap-southeast-1.aliyuncs.com/einnity/coin-ethereum:1.0
  container_name: eth-java
  ports:
    - "8002:8198"
  networks:
    my-network:
      ipv4_address: 192.168.1.21
  depends_on:
    - eth

Note: depends_on only ensures startup order, not that the RPC service is fully ready. For production, you might want to add health checks to wait for geth to be responsive.

4. Verify Network Connectivity Inside the Container

After applying the fixes, jump into eth-java and test connectivity step-by-step:

  1. Check if the static IP is assigned:
    ip addr
    
    You should see 192.168.1.21 listed for the network interface connected to my-network.
  2. Ping the eth container’s static IP to confirm network reachability:
    ping 192.168.1.17
    
  3. If ping works but curl fails, double-check the geth command parameters to ensure RPC is enabled and listening on 0.0.0.0.

A Reliable Alternative to Static IPs

While static IPs work, Docker Compose automatically creates DNS entries for containers on the same network. You could just use the container name (eth) instead of the IP (e.g., curl http://eth:8545), which avoids IP changes after restarts. But I get it—if you prefer static IPs for your workflow, the fixes above should resolve your issue.

内容的提问来源于stack exchange,提问作者Charles Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:05:33