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 ,请知道的大神指点一二,谢谢!
重新编辑了一下。
TL;DR:用
args
先说能不能传 environment 进去,不能。
官方文档说得很清楚:
environment 在 build 里是不可见的,如果需要的话,用
args
。再说怎么用
args
传,文档例子如下。dockerfile
docker-compose.yml
如果说什么都不要
args
的话,还有一个选择:envfile
来共享运行和构建时的环境变量。.env 里这么写
dockerfile 的写法类似下面这样