Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
Does docker-compose support merging extension field environment variables with service declarations?

Using the extension fields feature of Docker Compose 3.4, is it possible to combine a common set of environment variables in array form (or any array) with service-specific declarations? The documentation is unclear as to whether this is supported.

Calling docker-compose up on the the following YAML results in restart, entrypoint, and volumes being set in my-service, but it seems to replace the environment declaration coming from the extension field with the declaration which exists in my-service. I would like them merged.

version: '3.4'
x-service-config: &service-config
  restart: "on-failure"
  entrypoint: ""
  volumes:
    - "~/.aws:/home/serviceuser/.aws"
  environment:
    - "CommonVar1=foo"
    - "CommonVar2=bar"
services:
  my-service:
    <<: *service-config
    image: my-service:latest
    environment:
      - "ServiceVar1=baz"
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The merge only happens at the top level. If you want to merge a key at a lower level, use a separate template.


I've recreated the issue using the following compose file. It uses a public image and runs the env command so one can easily follow along without any external dependencies.

Here's a docker-compose.yml that works standalone, showing the 'environment' getting merge in:

version: '3.4'
x-service-config: &ref
    restart: 'on-failure'
    environment: &env
        'foo': 'bar'
        'bing': 'baz'
services:
    bop:
        image: 'alpine'
        command: 'env'
        <<: *ref

Here's the output, showing that the 'environment' key from the anchor is getting copied over:

$ docker-compose up
Creating network "merge_default" with the default driver
Creating merge_bop_1 ... done
Attaching to merge_bop_1
bop_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
bop_1  | HOSTNAME=d4122a655e1d
bop_1  | foo=bar
bop_1  | bing=baz
bop_1  | HOME=/root
merge_bop_1 exited with code 0

Here's the same compose file, but with 'environment' overridden in the 'bop' service definition:

version: '3.4'
x-service-config: &ref
    restart: 'on-failure'
    environment: &env
        'foo': 'bar'
        'bing': 'baz'
services:
    bop:
        image: 'alpine'
        command: 'env'
        environment:
            'bat': 'far'
        <<: *ref

and the corresponding output, showing only bat=far and not the other variables:

$ docker-compose up
Creating network "merge_default" with the default driver
Creating merge_bop_1 ... done
Attaching to merge_bop_1
bop_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
bop_1  | HOSTNAME=efe76cb7e09d
bop_1  | bat=far
bop_1  | HOME=/root
merge_bop_1 exited with code 0

The fix is to break this out into two template/anchors as follows:

version: '3.4'
x-service-config: &ref
    restart: 'on-failure'
x-environment-config: &env
    'foo': 'bar'
    'bing': 'baz'
services:
    bop:
        image: 'alpine'
        command: 'env'
        environment:
          <<: *env
          'true': 'false'
        <<: *ref

and here's the output showing the merged values:

$ docker-compose up
Creating network "merge_default" with the default driver
Creating merge_bop_1 ... done
Attaching to merge_bop_1
bop_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
bop_1  | HOSTNAME=617f90a94a7d
bop_1  | foo=bar
bop_1  | bing=baz
bop_1  | true=false
bop_1  | HOME=/root
merge_bop_1 exited with code 0

This works because you're doing the merge at both keys that you want to merge.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda