docker-compose 命令中的变量替换

Variable substitution in docker-compose command

我需要将 env 变量从文件传递到 docker-compose 中的命令,但这似乎是不可能的。 所以我首先尝试使用环境变量,但它不再起作用。我该怎么做?

version: '3'
services:
  nginx-service-xxx:
    image: service-xxx-nginx:latest
    ports:
      - 80:80
    restart: always

  service-xxx:
    image: service-xxx:latest
#    env_file:
#        - db-settings.env
    environment:
      - PORT=80
      - PG-HOST=192.168.0.101
      - PG-DATABASE=xxx
      - PG-USER=postgres
      - PG-PASSWORD=postgres
    restart: always
    command: python xxx/main.py --port=$$PORT --pg-host=$$PG-HOST --pg-database=$$PG-DATABASE --pg-user=$$PG-USER --pg-password=$$PG-PASSWORD

尝试将命令的复杂性移至 docker 入口点。 首先,您需要将以下入口点添加到您的项目中(使其可执行)

#!/bin/bash -e

case  in

  web)
    python xxx/main.py --port=$PORT --pg-host=$PG-HOST --pg-database=$PG-DATABASE --pg-user=$PG-USER --pg-password=$PG-PASSWORD
  ;;

  *)
    exec "$@"
  ;;

esac

exit 0

为您的服务更新 docker 文件

From base-image

COPY ./docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]

更新撰写

version: '3'
services:
  nginx-service-xxx:
    image: service-xxx-nginx:latest
    ports:
      - 80:80
    restart: always

  service-xxx:
    image: service-xxx:latest
    environment:
      - PORT=80
      - PG-HOST=192.168.0.101
      - PG-DATABASE=xxx
      - PG-USER=postgres
      - PG-PASSWORD=postgres
    restart: always
    command: web