简体   繁体   English

如何在 docker ENTRYPOINT 中使用 compose 环境变量

[英]How to use compose environment variables in docker ENTRYPOINT

Environment variables don't appear to work in ENTRYPOINT .环境变量似乎在ENTRYPOINT中不起作用。 It is my understanding that the shell form of ENTRYPOINT will expand ENV variables at run time, but this doesn't to appear to work for ENV_CONFIG_INT in the example below.据我了解,ENTRYPOINT 的ENTRYPOINT形式将在运行时扩展ENV变量,但这似乎不适用于以下示例中的ENV_CONFIG_INT What have I done wrong in the following example?在以下示例中我做错了什么?

Dockerfile Dockerfile

ENTRYPOINT [ "yarn", "run", "app-${ENV_CONFIG_INT}" ]

Compose yaml组成 yaml

test:
    image: testimage/test:v1.0
    build:
        context: .
        dockerfile: Dockerfile
    env_file:
        - ./docker.env
    environment:
        - ENV_CONFIG_INT=1

Error:错误:

error Command "app-${ENV_CONFIG_INT}" not found.

Replacing the value with a static int of say 1 fixes the issue, however I want the value to be dynamic at runtime.将值替换为 static int of say 1 可以解决问题,但是我希望该值在运行时是动态的。

Thanks in advance.提前致谢。

I wouldn't try to use an environment variable to specify the command to run.我不会尝试使用环境变量来指定要运行的命令。 Remove the line you show from the Dockerfile, and instead specify the command: in your docker-compose.yml file:从 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. 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 . If you remove the JSON-array layout and just specify a flat command, the environment variable will be expanded the way you expect.如果您删除 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 setup and then runs the CMD .) (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 。)

The usual way to ensure that environment variable expansion works in the entrypoint or command of an image is to utilize bash - or sh - as the entrypoint.确保环境变量扩展在映像的入口点或命令中起作用的常用方法是使用 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

The other thing you need to do is properly escape the environment variable, so its not expanded on the host system.您需要做的另一件事是正确转义环境变量,因此它不会在主机系统上扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM