如何在Dockerfile中使用自定义入口点在后台运行docker容器

时间:2017-09-28 01:39:00

标签: docker dockerfile

我创建了以下docker file

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文件构建docker镜像时,它是否已成功创建。 然后我使用下面的命令

将docker镜像作为容器运行
 docker run -d --name ethereum-ubuntu-geth-node2 ethereum-ubuntu-geth-node2

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

以下是容器状态

$ 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 interactive output的输出。

$ 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 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仍然希望容器运行。我还能这样做吗?

1 个答案:

答案 0 :(得分:1)

您可以同时运行定义和分离stdin的容器。虽然将应用程序配置为在前台运行作为服务器而不需要stdin会好得多。

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