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

时间:2018-08-08 14:43:43

标签: docker

Y!尝试使以下各项工作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变量?甚至有可能吗?

2 个答案:

答案 0 :(得分:2)

可能您没有导出变量。这样做虽然应该可以:

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

答案 1 :(得分:0)

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