I created a service for docker-compose 3 that uses many environment variables:
version: "3"
services:
myservice:
build:
context: ./myservice
command: ./something
environment:
VAR1: "val1"
VAR2: "val2"
VAR3: "val3"
Now I want to add a service that uses the same environment variable values, except for VAL1, and that has a different command:
myotherservice:
build:
context: ./myservice
command: ./somethingelse
environment:
VAR1: "val1-bis"
VAR2: "val2"
VAR3: "val3"
Is there any way to avoid the duplication of environment variables in the docker-compose.yml file? In docker-compose 2, it was possible to use the extends keyword but this is no longer the case in docker-compose 3.
EDIT: In October 2017, extension fields were added to the docker-compose 3.4 syntax: https://docs.docker.com/compose/compose-file/#extension-fields This is the right way to go:
version: "3"
x-env:
&default-env
VAR1: "val1"
VAR2: "val2"
VAR3: "val3"
services:
myservice:
build:
context: ./myservice
command: ./something
environment: *default-env
myotherservice:
build:
context: ./myservice
command: ./somethingelse
environment:
<< : *default-env
VAR1: "val1-bis"