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