在docker-compose.yml中无法看到环境变量

时间:2019-06-06 14:39:56

标签: docker docker-compose

我正在使用以下命令以docker-compose.sh构建环境:

curl -L --fail https://github.com/docker/compose/releases/download/1.23.0/run.sh -o docker-compose.sh
chmod +x docker-compose.sh
export DOCKER_CONTAINER_POSTFIX="test_name"

./docker-compose.sh  -p $PROJECT_NAME  build --compress
./docker-compose.sh -p $PROJECT_NAME  up --exit-code-from runner runner
./docker-compose.sh -p $PROJECT_NAME down

使用以下docker-compose.yml:

version: '2.3'

services:
  base:
    image: base
    build:
      context: .
      dockerfile: Dockerfile

  pytest_worker_01:
    extends:
      service: base
    container_name: "worker_${DOCKER_CONTAINER_POSTFIX}"

  runner:
    extends:
      service: base
    container_name: "runner_${DOCKER_CONTAINER_POSTFIX}"
    depends_on:
      - worker
    networks:
      - default
    command: ["cmd"]

在构建和启动新服务期间DOCKER_CONTAINER_POSTFIX不可用的问题。另外,我尝试使用以下命令:

DOCKER_CONTAINER_POSTFIX="test_name" ./docker-compose.sh  -p $PROJECT_NAME  build --compress

1 个答案:

答案 0 :(得分:0)

下载的脚本docker-compose.sh执行自己的例程,如果仔细查看它,则会发现它在下面运行exec docker run ...命令:

exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@"

这里的关键是传递COMPOSE_OPTIONS变量,这是注入env变量之类的正确点。将DOCKER_CONTAINER_POSTFIX的导出替换为另一种将解决您的问题:

COMPOSE_OPTIONS = "-e DOCKER_CONTAINER_POSTFIX=test_name"