[英]How to use compose environment variables in docker ENTRYPOINT
环境变量似乎在ENTRYPOINT
中不起作用。 据我了解,ENTRYPOINT 的ENTRYPOINT
形式将在运行时扩展ENV
变量,但这似乎不适用于以下示例中的ENV_CONFIG_INT
。 在以下示例中我做错了什么?
Dockerfile
ENTRYPOINT [ "yarn", "run", "app-${ENV_CONFIG_INT}" ]
组成 yaml
test:
image: testimage/test:v1.0
build:
context: .
dockerfile: Dockerfile
env_file:
- ./docker.env
environment:
- ENV_CONFIG_INT=1
错误:
error Command "app-${ENV_CONFIG_INT}" not found.
将值替换为 static int of say 1 可以解决问题,但是我希望该值在运行时是动态的。
提前致谢。
我不会尝试使用环境变量来指定要运行的命令。 从 Dockerfile 中删除您显示的行,而是指定command:
在您的docker-compose.yml
文件中:
test:
image: testimage/test:v1.0
build: .
env_file:
- ./docker.env
command: yarn run app-1 # <--
As you note, the shell form of ENTRYPOINT
(and CMD
and RUN
) will expand environment variables, but you're not using the shell form: you're using the exec form , which doesn't expand variables or handle any other shell constructs . 如果您删除 JSON 数组布局并仅指定一个平面命令,则环境变量将以您期望的方式扩展。
# Without JSON layout
CMD yarn run "app-${ENV_CONFIG_INT:-0}"
(I tend to prefer specifying CMD
to ENTRYPOINT
for the main application command. There are two reasons for this: it's easier to override CMD
in a plain docker run
invocation, and there's a useful pattern of using ENTRYPOINT
as a wrapper script that does some initial设置然后运行CMD
。)
确保环境变量扩展在映像的入口点或命令中起作用的常用方法是使用 bash - 或 sh - 作为入口点。
version: "3.8"
services:
test:
image: alpine
environment:
FOO: "bar"
entrypoint: ["/bin/sh", "-c", "echo $${FOO}"]
$ docker-compose run test
Creating so_test_run ... done
bar
您需要做的另一件事是正确转义环境变量,因此它不会在主机系统上扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.