[Ethereum] geth console cannot attach to geth docker container

dockergo-ethereum

I ask this question again although a similar question is asked here.
I answered that question with what I read and thought is the correct method but heck it doesn't work for me too.

Basically I run a geth instance in docker container and tried to connect geth console from my host computer.

The docker command is as follows:

if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
else
  docker container run --network etherdev_net --rm --name etherdev_$1 -p 8545:8545 -v $gethdir/$1:/root/.ethereum -v $gethdir/$1/.ethash:/root/.ethash -v $bootnodedir:/root/bootnode etherdev
fi

The docker container entry point basically invoke a shell script

geth --networkid 5493 --bootnodes "$(echo -n 'enode://'; bootnode --writeaddress -nodekey /root/bootnode/nodekeyfile | tr -d '\n'; echo '@192.168.2.2:30301')" --mine --rpc --rpcport "8545" --port "30303" --rpccorsdomain "*" --nat "any" --rpcapi eth,web3,personal,net --etherbase 0 --unlock "$(cat /root/.ethereum/geth/security/coinbase)" --password /root/.ethereum/geth/security/password.sec

then I run this command on my host to attach a geth console but it returns an error message "Fatal: Failed to start the JavaScript console: api modules: Post http://localhost:8545: EOF"

geth attach http://localhost:8545

I had verified that I can connect to geth using the same command "geth attach http://localhost:8545" if I do a "docker container exec -it … /bin/sh"

What is wrong here? Is the 8545 port published incorrectly?

Best Answer

It is because you use loopback interface when trying to connect. Just try to use:

docker run --rm -it ethereum/client-go \
attach http://$(ip route \
|grep "default via" \
|grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' \
|grep -m2 "" \
|tail -1):8545
Related Topic