[Ethereum] How to set up Geth in a Docker container on a VM such that RPC works

dockergo-ethereumjson-rpc

The Geth team conveniently provides a Docker container for the latest Geth builds, but when using Docker on Windows or Mac OS (where Docker is using a virtual machine to host containers) how to run it such that RPC calls can be seen on the local workstation? Running docker run -p 8545:8545 --name geth ethereum/cilent-go --rpc doesn't seem to do it.

Testing with CURL:

curl -X POST -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "id":"curltest", "method": "web3_clientVersion", "params": [] }' \
  "http://192.168.99.100:8545"

I get no response from the above command, it just spins until it times out.

Best Answer

The issue is that the default RPC server is "localhost", which doesn't seem to work properly in a Docker container in a VM. Instead, the server name of "0.0.0.0" will bind to all IP addresses the container is given, which will work with Docker's networking better.

So, the one-line command to run would be:

docker run -p 8545:8545 --name geth ethereum/client-go --rpc --rpcaddr 0.0.0.0

For better security, your 'go to' command to get Geth up-and-running should probably not have the --rpc flag on by default. Instead, try doing something like:

docker run -td -p 8545:8545 -p 30303:30303 --name geth ethereum/client-go
docker exec -it geth /user/bin/geth attach

That will open up the sharing port such that other Ethereum nodes can find it, but not start the RPC server right away. That docker exec command will get you into the Geth console, where you can then run admin.startRPC('0.0.0.0', 8545). If you want access to the running logs of the Geth node, run docker logs -f geth.

Related Topic