Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose, conditional statements? (e.g. add volume only if condition)

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?

like image 867
juanmirocks Avatar asked May 17 '18 08:05

juanmirocks


People also ask

Will Docker compose create volume if not exists?

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 .

Can Docker compose create volumes?

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.

Can you use environment variables in Docker compose?

Configure Compose using environment variablesSeveral environment variables are available for you to configure the Docker Compose command-line behavior.

Is Docker compose deprecated?

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.


1 Answers

Poor man's solution:

    volumes:       ${VARIABLE:-/dev/null}:/app/folder 

Or:

    volumes:       ${VARIABLE:-/dev/null}:${VARIABLE:-/tmp}/app/folder 
like image 75
gatopeich Avatar answered Oct 08 '22 05:10

gatopeich