I want to add a volume to my service, but only if the final user gave a folder for it. Otherwise, no volume should be mounted, for the already-prepared image has valid data in a default folder.
That is, I want to do something like (pseudocode):
services: my_awesome_service: volumes: if ${VARIABLE} => ${VARIABLE}:/app/folder
Are such conditional statements definable in a docker-compose file?
The only way I see to make this possible is to first define a base docker-compose file, which does not have the volume mount, and the call on a second docker-compose file only if the $VARIABLE
is defined. This is fine for a single or few conditions, but gets nasty if there are many.
Any solution?
Multiple containers can mount the same volume. Running docker-compose up will create a volume named <project_name>_html_files if it doesn't already exist .
We can also create a volume with Docker compose service or also specify existing volumes. For example, the following screenshot shows a 'docker-compose' file that creates a docker-compose service with a volume. As a result of the above command, a volume with the name 'myvolume' gets created.
Configure Compose using environment variablesSeveral environment variables are available for you to configure the Docker Compose command-line behavior.
Following the deprecation of Compose on Kubernetes, support for Kubernetes in the stack and context commands in the docker CLI is now marked as deprecated as well.
Poor man's solution:
volumes: ${VARIABLE:-/dev/null}:/app/folder
Or:
volumes: ${VARIABLE:-/dev/null}:${VARIABLE:-/tmp}/app/folder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With