如何用sh打开Docker容器中的端口?

时间:2018-06-01 07:45:10

标签: docker docker-compose port ethereum

我正在Docker容器中运行私有的以太坊网络。由于我在使用PoW,我需要生成DAG,大约需要10分钟。我需要发信号通知它已经以某种方式完成,所以我决定打开一个端口5555.所以在我的初始化脚本中我试图这样做:

搬运工-compose.yml:

version: "3"

services:
  eth_miner:
    image: ethereum/client-go:v1.7.3
    ports:
      - "8545:8545"
      - "5555:5555" # required?
    volumes:
      - ${DATA_DIR}:/root/.ethereum
      - ${HASH_DIR}:/root/.ethash
      - ${GENESIS_FILE}:/opt/genesis.json
      - ${INIT_FILE}:/opt/init-script.sh
    entrypoint: sh /opt/init-script.sh
    command: --rpc --rpcaddr=0.0.0.0 --rpcapi=db,eth,net,web3,personal --rpccorsdomain "*" --nodiscover --cache=512 --verbosity=4 --mine --minerthreads=3 --networkid 15 --etherbase="${ETHERBASE}" --gasprice=${GASPRICE}

init-script.sh:

#!/bin/sh

# check for genesis file
if [ ! -f /root/.ethereum/.init ]; then
    echo "running 'geth init /opt/genesis.json' ..."
    geth init /opt/genesis.json
    touch /root/.ethereum/.init
    echo "... done"
fi

# check for DAG generated
if [ ! -f /root/.ethash/full-R23-0000000000000000 ]; then
    echo "running 'geth makedag 10000 /root/.ethash' ..."
    geth makedag 10000 /root/.ethash
    echo "... done"
fi

echo "opening port 5555 to show DAG is ready "
nc -l 5555 &
echo "... done"

echo "running 'geth $@'"
geth "$@"

如果我在5555:5555中留下docker-compose.yml映射,那么它在启动后立即打开,无法作为信号工作:

CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                                                                                  NAMES
808a5453bf57        ethereum/client-go:v1.7.3   "sh /opt/init-script…"   4 seconds ago       Up 11 seconds       0.0.0.0:5555->5555/tcp, 8546/tcp, 0.0.0.0:8545->8545/tcp, 30303/tcp, 30303-30304/udp   7f8680be_eth_miner_1

即使没有打开(登录到docker容器):

/ # netstat -lntu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 127.0.0.11:39593        0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:42033           0.0.0.0:*               LISTEN      
tcp        0      0 :::30303                :::*                    LISTEN      
tcp        0      0 :::8545                 :::*                    LISTEN      
udp        0      0 127.0.0.11:56580        0.0.0.0:*  

如果我从docker-compose.yml删除它,那么我显然无法在测试中访问它:

java.lang.IllegalArgumentException: No internal port '5555' for container 'eth_miner': Suppliers.memoize(com.palantir.docker.compose.connection.Container$$Lambda$46/1692550316@5ac2447d)

那么可以使用这种方法吗?

更新:如果有意义'ethereum / client-go'Docker图像在golang:1.10-alpine as builder图片上based

2 个答案:

答案 0 :(得分:1)

我宁愿使用基于文件的方法。正如您所见,即使容器中没有侦听器,docker守护程序也会打开此端口。

搬运工-compose.yml:

version: "3"

services:
  eth_miner:
    image: ethereum/client-go:v1.7.3
    ports:
      - "8545:8545"

    volumes:
      - /var/run/geth:/var/run/geth
      - ${DATA_DIR}:/root/.ethereum
      - ${HASH_DIR}:/root/.ethash
      - ${GENESIS_FILE}:/opt/genesis.json
      - ${INIT_FILE}:/opt/init-script.sh
    entrypoint: sh /opt/init-script.sh
    command: --rpc --rpcaddr=0.0.0.0 --rpcapi=db,eth,net,web3,personal --rpccorsdomain "*" --nodiscover --cache=512 --verbosity=4 --mine --minerthreads=3 --networkid 15 --etherbase="${ETHERBASE}" --gasprice=${GASPRICE}

init-script.sh:

#!/bin/sh

# check for genesis file
if [ ! -f /root/.ethereum/.init ]; then
    echo "running 'geth init /opt/genesis.json' ..."
    geth init /opt/genesis.json
    touch /root/.ethereum/.init
    echo "... done"
fi

# check for DAG generated
if [ ! -f /root/.ethash/full-R23-0000000000000000 ]; then
    echo "running 'geth makedag 10000 /root/.ethash' ..."
    geth makedag 10000 /root/.ethash
    echo "... done"
fi

echo "Writing file to show DAG is ready "
touch /var/run/geth/ready
echo "... done"

# Trap signals to remove /var/run/geth/ready on exit
trap "rm /var/run/geth/ready TERM; exit $?" TERM
trap "rm /var/run/geth/ready EXIT; exit $?" EXIT

echo "running 'geth $@'"
geth "$@"

答案 1 :(得分:0)

我建议通过扩展父级图像来创建自己的docker图像 创建自己的Dockerfile

FROM ethereum/client-go:v1.7.3
EXPOSE 5555

构建它并生成一个新的docker镜像

#>docker build -t custom-client-go -t xyz/custom-client-go .

使用docker push将其推送到docker注册表(docker-hub下的xyz配置文件)。

#>docker push xyz/custom-client-go

然后更新docker-compose.yml(image-name - > xyz / custom-client-go