簡體   English   中英

如何在 docker ENTRYPOINT 中使用 compose 環境變量

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM