使用 docker-compose up 时访问 docker-compose.yml 文件内部的环境变量



哟!尝试使以下内容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-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文件中的变量(感谢杰克·戈尔(:

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

最新更新