I have a webapp
service defined in a docker compose file. Trying to add some flexibility for user to customize the volume mounts. I tried many variations, all unsuccessfully, the one with the most promise is:
volumes: ['log-volume:/path/to/container/logs',
'data-volume:/path/to/container/data' ${WEBAPP_VOLUMES:-''}]
I think this should default the environment variable to an empty string and all should be good (i.e. WEBAPP_VOLUMES is not defined in the .env file). Then to use, one would need start any definition with a comma, not ideal, but seems acceptable. However, docker-compose
hates this!
I get errors such as:
ERROR: yaml.scanner.ScannerError: while scanning a plain scalar
in "./docker-compose.yml", line 20, column 110
found unexpected ':'
in "./docker-compose.yml", line 20, column 124
Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details.
Does anyone know to accomplish what I am trying to do here?
Sure, Docker can use environment variables in volume definitions, but not the way you're trying to do it. Variable substitution happens after YAML parsing. The syntax you're trying to use results in a YAML syntax error (hence the yaml.scanner.ScannerError
exception), so docker-compose isn't even able to read in the configuration. You can use a tool like this yaml parser to validate YAML syntax.
Something like this would be valid syntax:
volumes:
- log-volume:/path/to/container/logs
- data-volume:/path/to/container/data
- ${WEBAPP_VOLUME}:/path/to/container/app
...but would not do what you want (the above would let you specify the source of the /path/to/container/app
mount with an environment variable, but would not let you make that mount optional, nor would it let you magically expand it into multiple distinct mounts).
You may find you can use the extends
keyword (described in Extending services) to get the behavior your want, although support for that keyword has been dropped in version 3 and later of the compose file format.