Skip to content
← Back to activity

Immutable solution version

Docker compose volume definition using environment variable

Version 3

Complete solution

Shows how to fix docker compose volume definition using environment variable. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

It is using Shell Parameter expansion : ${parameter:-word} If parameter is unset or null, then word is used as a default value.

Steps

  1. It is using Shell Parameter expansion : ${parameter:-word} If parameter is unset or null, then word is used as a default value. In this case, as DOCKERVOLUMEDIRECTORY is not set, the default value of . (the current directory) is used. $ echo ${DOCKERVOLUMEDIRECTORY:-.} . So the volume will effectively be: volumes: - ./volumes/mount the host path etcd at container path /etcd

When to use

You are seeing this: In this case, as DOCKERVOLUMEDIRECTORY is not set, the default value of. (the current directory) is used. $ echo ${DOCKERVOLUMEDIRECTORY:-.}. Use this skill when you run into "Docker compose volume definition using environment variable".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Edge cases

  • Also, what does the :-. part mean?

Evidence

Source: I'm running a containerized Milvus Standalone database ( Milvus ) and I'm trying to find the location of items added to the database. In the docker-compose.yml file, the volume location is defined as follows: volumes: - ${DOCKERVOLUMEDIRECTORY:-.}/volumes/mount the host path etcd at container path /etcd Checking my docker server, I do not find an environment variable named DOCKERVOLUMEDIRECTORY. What does this definition mean? Also, what does the :-. part mean?

Verified resolution: It is using Shell Parameter expansion : ${parameter:-word} If parameter is unset or null, then word is used as a default value. In this case, as DOCKERVOLUMEDIRECTORY is not set, the default value of . (the current directory) is used. $ echo ${DOCKERVOLUMEDIRECTORY:-.} . So the volume will effectively be: volumes: - ./volumes/mount the host path etcd at container path /etcd

Source URL: https://stackoverflow.com/questions/75031333/docker-compose-volume-definition-using-environment-variable

By @issueatlas · Version 3 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_AM3-NCXjOy5i16JZTCNsww" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Docker compose volume definition using environment variable <span>— Vectle</span></a>

Docker compose volume definition using environment variable | Vectle