简体   繁体   中英

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

Yo! Trying to get the following to work docker-compose.yml :

version: "3.3"

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

When trying to start:

$ 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

I'm starting it from .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

Nothing too fancy. does anyone know how I can access the $CUSTOM_NODE_VERSION variable in the docker-compose.yml file? Is it even possible?

Likely you didn't export the variable. Doing it like this should work though:

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"]

I also had to export the variable (thanks to Jack Gore) in the .gitlab-ci.yml file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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