在docker compose文件中使用参数

在docker compose文件中使用参数,docker,docker-compose,Docker,Docker Compose,我的docker compose.yml文件中有以下内容 geth-testnet: build: context: . dockerfile: Dockerfile args: GETH_REPO: 'https://github.com/ethereum/go-ethereum' GETH_VERSION: 'v1.8.12' RPC_LISTEN_PORT: 8546 command:

我的
docker compose.yml
文件中有以下内容

 geth-testnet:
    build:
      context: .
      dockerfile: Dockerfile
      args:  
        GETH_REPO: 'https://github.com/ethereum/go-ethereum'
        GETH_VERSION: 'v1.8.12'
        RPC_LISTEN_PORT: 8546
    command: "--rpcport ${RPC_LISTEN_PORT}"
    entrypoint: 
        - "geth"
    tty: true
    image: geth-node-testnet:v1.8.12
    container_name: geth-node-testnet
    ports:
      - '8546:8546'
    volumes:
      - /root/.ethereum
运行时,
docker compose up--build
,希望它运行以下命令:

geth -rpcport 8546
但是,我得到了以下错误

flag needs an argument: -rpcport
因此,
RPC\u LISTEN\u PORT
的值未正确替换


我在dockerfile中有
ARG RPC\u LISTEN\u PORT
仔细检查了你的问题,似乎
命令:“--rpcport${RPC\u LISTEN\u PORT}”
无法使用你在
docker compose.yml中输入的值

因此,我们可以提供两种解决方案:

  • 在执行
    compose
    命令之前,先在bash中导出RPC\u LISTEN\u PORT=8546
  • 在同一文件夹中新建一个
    .env
    文件,将
    RPC\u LISTEN\u PORT=8546
    放入其中

  • 实际上,我尝试在docker compose`environment:RPC_LISTEN_PORT:8546`中添加此项。我遇到了相同的错误,我在docker文件`ENV RPC_LISTEN_PORT=“${RPC_LISTEN_PORT}”中也尝试了此项。我再次遇到了相同的错误。请尝试将
    命令
    的值更改为字符串数组,而不是空格分隔的字符串。