如何在Dockerfile中的自定义入口点运行Docker容器

我已经创build了下面的docker 文件 。

FROM ubuntu:latest as builder ENV DEBIAN_FRONTEND=noninteractive \ INITRD=No \ LANG=en_US.UTF-8 \ GOVERSION=1.9 \ GOROOT=/opt/go \ GOPATH=/root/.go RUN apt-get update \ && apt-get install -y software-properties-common \ && apt-get install -y wget \ && apt-get install -y make \ && rm -rf /var/lib/apt/lists/* \ && add-apt-repository ppa:jonathonf/gcc-7.2 \ && apt-get update \ && apt-get install -y gcc g++ \ && apt-get install -y linux-headers-4.11.0-14-generic \ && apt-get install -y musl-dev \ && cd /opt && wget https://storage.googleapis.com/golang/go${GOVERSION}.linux-amd64.tar.gz && \ tar zxf go${GOVERSION}.linux-amd64.tar.gz && rm go${GOVERSION}.linux-amd64.tar.gz && \ ln -s /opt/go/bin/go /usr/bin/ && \ mkdir $GOPATH ADD . /go-ethereum RUN mkdir -p /usr/local/config \ && mkdir -p /home/ubuntu/eth-dev \ && mkdir -p /usr/local/scripts \ && cd /go-ethereum && make geth COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/ COPY --from=builder /go-ethereum/scripts/entry-point.sh /usr/local/bin/ COPY --from=builder /go-ethereum/genesis/genesis.json /usr/local/config/ EXPOSE 8545 8546 30303 30303/udp ENTRYPOINT ["entry-point.sh"] 

entry-point.sh的内容是

  #!/bin/bash geth --datadir /home/ubuntu/eth-dev init /usr/local/config/genesis.json geth --networkid 45634 --verbosity 4 --ipcdisable --rpc --port 30301 --rpcport 8545 --rpcaddr 0.0.0.0 console 2>> /home/ubuntu/eth-dev/eth.log 

当我从docker文件构builddocker镜像时,它是成功创build的。 然后,我使用下面的命令将docker映像作为容器运行

  docker run -d --name ethereum-ubuntu-geth-node2 ethereum-ubuntu-geth-node2 

它创build容器并立即退出容器。 我不期待这个,因为我在守护进程模式下运行容器(它应该在后台运行)。

以下是容器状态

 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0d25a5e71449 ethereum-ubuntu-geth-node2 "entry-point.sh" 4 seconds ago Exited (0) 1 second ago ethereum-ubuntu-geth-node2 

以下是容器日志的输出。

 $ docker logs ethereum-ubuntu-geth-node2 WARN [09-28|00:32:28] No etherbase set and no accounts found as default INFO [09-28|00:32:28] Allocated cache and file handles database=/home/ubuntu/eth-dev/geth/chaindata cache=16 handles=16 INFO [09-28|00:32:28] Writing custom genesis block INFO [09-28|00:32:28] Successfully wrote genesis state database=chaindata hash=a3c5c1…d6926b INFO [09-28|00:32:28] Allocated cache and file handles database=/home/ubuntu/eth-dev/geth/lightchaindata cache=16 handles=16 INFO [09-28|00:32:28] Writing custom genesis block INFO [09-28|00:32:28] Successfully wrote genesis state database=lightchaindata hash=a3c5c1…d6926b Welcome to the Geth JavaScript console! instance: Geth/v1.7.1-unstable/linux-amd64/go1.9 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 

以下是docker run交互式输出的输出。

 $ docker run -it --name ethereum-ubuntu-geth-node2 ethereum-ubuntu-geth-node2 WARN [09-28|00:45:28] No etherbase set and no accounts found as default INFO [09-28|00:45:28] Allocated cache and file handles database=/home/ubuntu/eth-dev/geth/chaindata cache=16 handles=16 INFO [09-28|00:45:28] Writing custom genesis block INFO [09-28|00:45:28] Successfully wrote genesis state database=chaindata hash=a3c5c1…d6926b INFO [09-28|00:45:28] Allocated cache and file handles database=/home/ubuntu/eth-dev/geth/lightchaindata cache=16 handles=16 INFO [09-28|00:45:28] Writing custom genesis block INFO [09-28|00:45:28] Successfully wrote genesis state database=lightchaindata hash=a3c5c1…d6926b Welcome to the Geth JavaScript console! instance: Geth/v1.7.1-unstable/linux-amd64/go1.9 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 > 

现在当我检查容器状态时,它已经启动了。

 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e27de43da867 ethereum-ubuntu-geth-node2 "entry-point.sh" 12 seconds ago Up 11 seconds 8545-8546/tcp, 30303/tcp, 30303/udp ethereum-ubuntu-geth-node2 

以下是我的docker细节。

 $ docker version Client: Version: 17.09.0-ce-rc2 API version: 1.32 Go version: go1.8.3 Git commit: 363a3e7 Built: Thu Sep 14 02:01:59 2017 OS/Arch: linux/amd64 Server: Version: 17.09.0-ce-rc2 API version: 1.32 (minimum version 1.12) Go version: go1.8.3 Git commit: 363a3e7 Built: Thu Sep 14 02:03:24 2017 OS/Arch: linux/amd64 Experimental: false 

一旦容器启动,我想在shell中执行一些geth工具命令,一旦我退出shell仍然期待容器运行。 我能做到吗?

你可以运行一个容器,同时定义和分离stdin。 尽pipe将应用程序configuration为在前台作为服务器运行,而不需要stdin,会好很多。

 $ docker run -itd \ --name ethereum-ubuntu-geth-node2 \ ethereum-ubuntu-geth-node2