gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:35:01 25 4
gpt4 key购买 nike

我创建了以下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 image作为容器运行
 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交互式输出的输出。
$ 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的容器。尽管最好将应用程序配置为在前台作为服务器运行,而不需要stdin。

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

关于docker - 如何在Dockerfile中使用自定义入口点在后台运行Docker容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46459659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com