Docker-compose extend 找不到环境变量

Docker-compose extend not finding environment variables

我有 docker-compose 文件的一部分

docker-compose.yml

  pitchjob-fpm01:
    container_name: pitchjob-fpm01     
    env_file: 
      - env/base.env 
    build:
      context: ./pitch
      dockerfile: PitchjobDockerfile
    volumes:
      - "/Sites/pitch/pitchjob/:/Sites"
    restart: always 
    depends_on:
      - memcached01
      - memcached02      
    links: 
      - memcached01
      - memcached02
    extends:
      file: "shared/common.yml"
      service: pitch-common-env  

我的扩展 yml 文件是

compose.yml

version: '2.0'

services:
  pitch-common-env:
    environment:
      APP_VOL_DIR: Sites
      WEB_ROOT_FOLDER: web
      CONFIG_FOLDER: app/config
      APP_NAME: sony_pitch

在 pitchjob-fpm01 的 docker 文件中,我有一个这样的命令

PitchjobDockerfile

# Set folder groups
RUN chown -Rf www-data:www-data /$APP_VOL_DIR

但是当我运行命令调出堆栈时

docker-compose -f docker-compose-base.yml up --build --force-recreate --remove-orphans

我收到以下错误

failed to build: The command '/bin/sh -c chown -Rf www-data:www-data /$APP_VOL_DIR' returned a non-zero code: 1

我猜这是因为它没有 $APP_VOL_DIR,但如果 docker compose 正在扩展另一个定义的 compose 文件,为什么会这样 环境:变量

我认为您的问题不在于覆盖,而在于您尝试进行环境变量替换的方式。来自文档:

Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, RUN [ "echo", "$HOME" ]will not do variable substitution on $HOME. If you want shell processing then either use theshell form or execute a shell directly, for example:RUN [ "sh", "-c", "echo $HOME" ].

你可以使用 build-time arguments

Dockerfile中定义:

ARG APP_VOL_DIR=app_vol_dir
# Set folder groups
RUN chown -Rf www-data:www-data /$APP_VOL_DIR

然后在 docker-compose.yml 中设置 app_vol_dir 作为构建参数:

pitchjob-fpm01:
  container_name: pitchjob-fpm01     
  env_file: 
    - env/base.env 
  build:
    context: ./pitch
    dockerfile: PitchjobDockerfile
    args:
      - app_vol_dir=Sites