0

I'm dockerizing a bunch of windows apps in Windows Containers.

All my apps require the same mappings, here's a short snippet of my config:

version: '3.9'
services:


  shell0:
    build:
      target: myimage
      context: .
    image: 'salimfadhley/myimage:latest'
    entrypoint: c:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe
    working_dir: "c:\\"
    volumes:
      - type: "bind"
        source: "x:"
        target: "x:"


volumes: # THIS BIT DOESN'T WORK!
  xdrive:
    source: "x:"

"xdrive" is a network drive share used by all of my applications. Every single process needs access do "xdrve", that's why I'm bind-mounting this into each service.

I'm doing this by repeating the configuration for every single service in this Docker Compose file. There's going to be quite a few of them. It's going to make my docker-compose file very repetitive.

Is there a way to define the "xdrive" just once, for example in the global "volumes" section? I'd like to be able to do something like this per-service:

...service
  volumes:
   - xdrive: "x:"

Can it be done? What is the syntax to define a bind-mount globally?

Salim Fadhley
  • 6,975
  • 14
  • 46
  • 83

1 Answers1

1

You can solve this with YAML syntax:

version: "3.5"
services:
  one:
    image: busybox
    command: ls /foo
    volumes:
    - &volume-foo
      type: bind
      source: .
      target: /foo

  two:
    image: busybox
    command: ls /foo
    volumes:
    - *volume-foo

&volume-foo is an anchor, *volume-foo is an alias. An alias repeats what's been declared after the corresponding anchor, in this case a single object of the array. After parsing it will look like this:

version: "3.5"
services: 
  one: 
    image: busybox
    command: "ls /foo"
    volumes: 
      - 
        source: "."
        target: /foo
        type: bind

  two: 
    image: busybox
    command: "ls /foo"
    volumes: 
      - 
        source: "."
        target: /foo
        type: bind
anemyte
  • 17,618
  • 1
  • 24
  • 45
  • That would certainly eliminate most of the repetition! Is there no way to define a bind-mapping globally? – Salim Fadhley Jan 28 '21 at 12:54
  • 1
    @SalimFadhley the `volumes` section is for _creating_ volumes. AFAIK when you bind-mount something you do not create a volume. The solution I posted is what you need, but you probably need more examples. With YAML syntax you can not only reference an object, you can merge several and mix their properties (to override `target` for example. It is also possible to declare common keys under `X-something` top-level key, again using anchors and aliases. – anemyte Jan 28 '21 at 13:11
  • 1
    @SalimFadhley see example 1 in this answer https://stackoverflow.com/questions/36283908/re-using-environment-variables-in-docker-compose-yml – anemyte Jan 28 '21 at 13:12
  • Thank you @anemyte for your help. Yes, I think I misunderstood the meaning of the global volumes option. We aren't creating a new volume, we are just reference what already exists on the system so this approach works just fine. – Salim Fadhley Jan 28 '21 at 17:00