Docker Compose配置容器静态IP后跨容器通信失败问题求助
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:
- Check if the static IP is assigned:
You should seeip addr192.168.1.21listed for the network interface connected tomy-network. - Ping the
ethcontainer’s static IP to confirm network reachability:ping 192.168.1.17 - 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

