title: Yaml asterisk syntax with docker compose volumes
body: <h2>My goal is to recuse duplicated volume definition within each service</h2>
  <p>I read the following Stackoverflow questions/answers but they did not help:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/27425486/yaml-store-array-as-variable">YAML: Store array as variable</a></li>
  <li><a href="https://stackoverflow.com/questions/6651275/what-do-the-mean-in-this-database-yml-file">What do the &amp;,&lt;&lt;, * mean in this database.yml file?</a></li>
  </ul>
  <h4>Current attempts:</h4>
  <p><strong>Variant 1:</strong>
  Using <code>default-volumes</code> with a dash prefix like every array entry has to (relates to the first SO question)</p>
  <pre class="lang-yaml prettyprint-override"><code>version: '2.4'
  
  x-default-volumes: &amp;default-volumes
    - /etc/localtime:/etc/localtime:ro
    
  x-default-service: &amp;default-service
    image: my-image-name:my-tag
    restart: unless-stopped
    volumes:
      *default-volumes
    networks:
      my-network:
      
  services:
    service1:
      &lt;&lt;: *default-service
      image: service1-image:tag
    
    service2:
      &lt;&lt;: *default-service
      image: service2-image:tag
      volumes:
        - *default-volumes
        - service2:/path/on/container
  
      
  networks:
    my-network:
    
  volumes:
    service2: {}
  </code></pre>
  <p><code>docker-compose config</code></p>
  <blockquote>
  <p>ERROR: The Compose file './docker-compose.yml' is invalid because:
  services.service2.volumes contains an invalid type, it should be a string, or an object</p>
  </blockquote>
  <p><strong>Variant 2:</strong>
  An alternative attempt...</p>
  <pre class="lang-yaml prettyprint-override"><code>version: '2.4'
  
  x-default-volumes: &amp;default-volumes
    - /etc/localtime:/etc/localtime:ro
    
  x-default-service: &amp;default-service
    image: my-image-name:my-tag
    restart: unless-stopped
    volumes:
      *default-volumes
    networks:
      my-network:
      
  services:
    service1:
      &lt;&lt;: *default-service
      image: service1-image:tag
    
    service2:
      &lt;&lt;: *default-service
      image: service2-image:tag
      volumes:
        *default-volumes
        - service2:/path/on/container
  
      
  networks:
    my-network:
    
  volumes:
    service2: {}
  </code></pre>
  <p><code>docker-compose config</code></p>
  <blockquote>
  <p>ERROR: yaml.parser.ParserError: while parsing a block mapping<br />
  in &quot;./docker-compose.yml&quot;, line 20, column 5
  expected , but found ''<br />
  in &quot;./docker-compose.yml&quot;, line 24, column 7</p>
  </blockquote>
  <p>Without the usage of the asterisk character in the <code>service2</code> volumes definition, a <code>docker-compose config</code> command yields a valid result.</p>
  
  <pre class="lang-yaml prettyprint-override"><code>version: '2.4'
  
  x-default-volumes: &amp;default-volumes
    - /etc/localtime:/etc/localtime:ro
    
  x-default-service: &amp;default-service
    image: my-image-name:my-tag
    restart: unless-stopped
    volumes:
      *default-volumes
    networks:
      my-network:
      
  services:
    service1:
      &lt;&lt;: *default-service
      image: service1-image:tag
    
    service2:
      &lt;&lt;: *default-service
      image: service2-image:tag
      volumes:
        *default-volumes
        - service2:/path/on/container
  
      
  networks:
    my-network:
    
  volumes:
    service2: {}
  </code></pre>
  
  <p><strong>Does anyone have an idea how to achieve this goal? Thanks in advance.</strong></p>
  <h2>Edit 1:</h2>
  <p>As David Maze pointed out: There is no valid yaml solution to this problem. He also found a similar question (<a href="https://stackoverflow.com/questions/4948933/is-there-a-way-to-alias-anchor-an-array-in-yaml">Is there a way to alias/anchor an array in YAML?</a>) in which some kind of reuse is described.</p>
  <p>I ended up using the following result:</p>
  <pre class="lang-yaml prettyprint-override"><code>version: '2.4'
  
  x-volumes:
    - &amp;volume-localtime
      /etc/localtime:/etc/localtime:ro
  
  x-default-service: &amp;default-service
    image: my-image-name:my-tag
    restart: unless-stopped
    volumes:
      - *volume-localtime
    networks:
      my-network:
  
  services:
    service1:
      &lt;&lt;: *default-service
      image: service1-image:tag
  
    service2:
      &lt;&lt;: *default-service
      image: service2-image:tag
      volumes:
        - *volume-localtime
        - service2:/path/on/container
  
  
  networks:
    my-network:
  
  volumes:
    service2: {}
  </code></pre>