Can (default values for) variables be defined in docker-compose files?

  docker, docker-compose

Dockerfiles seem to allow variable definitions, e.g.

ARG NETWORK_NAME=mynet

And docker-compose files seem to allow use of environment variables, e.g.

networks:
  my-bridge:
    name: ${MY_NETWORK}

services:
  my_proj:
    image: artifactory.mine.com/${arch}/proj:${ver}
    entrypoint: [ "sh", ]
    tty: true

…but the latter seems to rely on the variable being defined externally by the caller, e.g. as an environment variable.

Is there a way to define a variable (e.g. define-if-not-already-defined) in a docker-compose file? Something along the lines of:

ARG MY_NETWORK=mynet
ARG arch=x86
ARG ver=2.0

networks:
  my-bridge:
    name: ${MY_NETWORK}

services:
  my_proj:
    image: artifactory.mine.com/${arch}/proj:${ver}
    entrypoint: [ "sh", ]
    tty: true

? Or must the variables used in docker-compose files be defined outside, e.g. as environment variables?

Source: Docker Questions

LEAVE A COMMENT