Mount a volume in docker-compose conditionally

Since extend has been removed in version 3 there's a new way of overriding settings by providing multiple -f params where the next file extends the previous config

The docs are mixed between versions https://docs.docker.com/compose/extends/#multiple-compose-files

i.e. to run in development mode

docker-compose -f docker-compose.yml -f docker-compose.dev.yml

Where docker-compose.yml contains the full config and docker-compose.dev.yml only adds a volume

services:
  serviceA:
    volumes:
      -.:/usr/local/abc/service

Instead of (not) mounting a volume commenting out that line, I would use https://docs.docker.com/compose/extends/#example-use-case to run a service extension that specifies a volume.

I do this to tackle two different problems:

  • I don't specify a volume when the docker image is meant to run in production and has all the necessary files bundled in it.
  • I specify a volume during development, to check live changes.