簡體   English   中英

使用docker-compose up時訪問docker-compose.yml文件內的環境變量

[英]Accessing environmental variable inside of docker-compose.yml file when using docker-compose up

嘗試使以下內容工作docker-compose.yml

version: "3.3"

services:
  node:
    image: node:$CUSTOM_NODE_VERSION
    environment:
      - NODE_ENV=test
    entrypoint: ["npm", "run", "lint"]

嘗試開始時:

$ docker-compose -f docker-compose.yml up --remove-orphans --force-recreate --abort-on-container-exit
The CUSTOM_NODE_VERSION variable is not set. Defaulting to a blank string.
no such image: node:: invalid reference format

我從.gitlab-ci.yml

Node:
  stage: Node
  script:
    - echo $CUSTOM_NODE_VERSION
    - docker-compose -f docker-compose.lint.yml up --remove-orphans --force-recreate --abort-on-container-exit

沒什么好看的。 有誰知道我如何訪問docker docker-compose.yml文件中的$ CUSTOM_NODE_VERSION變量? 可能嗎

您可能沒有導出變量。 這樣做雖然應該可以:

CUSTOM_NODE_VERSION=10 docker-compose -f docker-compose.lint.yml up --remove-orphans --force-recreate --abort-on-container-exit

https://docs.docker.com/compose/environment-variables/

version: "3.3"

services:
  node:
    image: "node:${CUSTOM_NODE_VERSION}"
    environment:
      - NODE_ENV=test
    entrypoint: ["npm", "run", "lint"]

我還必須在.gitlab-ci.yml文件中導出變量(感謝Jack Gore):

Node:
  stage: Node
  script:
    - export CUSTOM_NODE_VERSION=$CUSTOM_NODE_VERSION
    - docker-compose -f docker-compose.lint.yml up --remove-orphans --force-recreate --abort-on-container-exit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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