如何通过docker-compose.yml向dockerfile传递environment变量的值?

docker-compose config 内容如下:

yhm@:~/myserver/test<master>$ docker-compose config
services:
  ubuntu-001:
    build:
      context: /home/yhm/myserver/test/ubuntu
      dockerfile: Dockerfile
      args:
        GROUP_ID: "1000"
        USERNAME_ID: "1000"
    container_name: mytest-001
    environment:
      SYNC_GROUP: yhm
      SYNC_USERNAME: yhm
    image: mytest:001
    networks:
      default: null
    privileged: true
    stdin_open: true
    tty: true
networks:
  default:
    name: test_default

dockerfile 内容如下:

FROM ubuntu:latest
RUN groupadd -g 1000 ${SYNC_GROUP}

执行 docker-compose build 报错如下:

yhm@:~/myserver/test<master>$ docker-compose build
Sending build context to Docker daemon     166B
Step 1/2 : FROM ubuntu:latest
 ---> 54c9d81cbb44
Step 2/2 : RUN groupadd -g 1000 ${SYNC_GROUP}
 ---> Running in 74d9b0f12614
Usage: groupadd [options] GROUP

Options:                                                                                                                     
  -f, --force                   exit successfully if the group already exists,                                               
                                and cancel -g if the GID is already used                                                     
  -g, --gid GID                 use GID for the new group                                                                    
  -h, --help                    display this help message and exit                                                           
  -K, --key KEY=VALUE           override /etc/login.defs defaults                                                            
  -o, --non-unique              allow to create groups with duplicate                                                        
                                (non-unique) GID                                                                             
  -p, --password PASSWORD       use this encrypted password for the new group                                                
  -r, --system                  create a system account                                                                      
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       directory prefix
      --extrausers              Use the extra users database

1 error occurred:
        * Status: The command '/bin/sh -c groupadd -g 1000 ${SYNC_GROUP}' returned a non-zero code: 2, Code: 2*

原因就是 dockerfile 没有获取到 SYNC_GROUP 这个变量的值,请不要告诉我用 args 这种方式传参,这种的我传过没问题,现在因为需求需要通过 environment ,请知道的大神指点一二,谢谢!

阅读 5.5k
2 个回答

重新编辑了一下。

TL;DR:用 args


先说能不能传 environment 进去,不能。

官方文档说得很清楚:

If your service specifies a build option, variables defined in environment are not automatically visible during the build. Use the args sub-option of build to define build-time environment variables.

environment 在 build 里是不可见的,如果需要的话,用 args

再说怎么用 args 传,文档例子如下。

dockerfile

# syntax=docker/dockerfile:1

ARG buildno
ARG gitcommithash

RUN echo "Build number: $buildno"
RUN echo "Based on commit: $gitcommithash"

docker-compose.yml

build:
  context: .
  args:
    buildno: 1
    gitcommithash: cdc3b19

如果说什么都不要 args 的话,还有一个选择:envfile来共享运行和构建时的环境变量。

env_file: .env

.env 里这么写

GID=100

dockerfile 的写法类似下面这样

COPY .env .
RUN source .env && \
    groupadd -g ${GID}
FROM ubuntu:latest
RUN groupadd -g 1000 $SYNC_GROUP
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题