简体   繁体   中英

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

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

在此处输入图片说明

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

在此处输入图片说明

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

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

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