vogash
vogash

Reputation: 1018

Re-using variables in docker-compose yml

There are two articles describing using environment variable but my use case is different.

I have docker-compose file where I have 3-7 containers. Depends on situation.

version: '2'

services:
  db:
    image: example/db
  backend:
    image: example/server
  frontend:
    image: example/gui

Now, in above example all my images will use latest version, but I would like to control which version to deploy, therefore I want to define some variable version and use it in all my images, something like:

version: '2'

variable version=1.0.1

services:
  db:
    image: example/db:$version
  backend:
    image: example/server:$version
  frontend:
    image: example/gui:$version

Second example is wrong, but it shows my need what I want to achieve

Upvotes: 8

Views: 7573

Answers (2)

Adiii
Adiii

Reputation: 59906

Docker-compose used Interpolation Syntax ${variable} for variables and you missed that in your file.

version: '2'

services:
  db:
    image: example/db:${version}
  backend:
    image: example/server:${version}
  frontend:
    image: example/gui:${version}

So just pass the version to your docker-compose command

version=1.13-alpine docker-compose up

Upvotes: 5

Lam Nguyen
Lam Nguyen

Reputation: 196

In the same directory as docker-compose.yml add an environment file named .env, then specify your environment variable.

enter image description here

After that, add variable into your docker-compose.yml

enter image description here

The ${..} represents a variable in .env

Upvotes: 6

Related Questions