In my docker compose file, I have three docker containers having volume path pointing to the same folder.
I would like to put all of these paths into one place in a single docker compose file, so when I change my folder location, I can just change one place instead of three.
I was hoping that I could store this path as a variable and use it at different places.
I have tried volume mount (or named volume) method, but it isn’t really ideal since the folder has to exist before mounting.
I know I can make a .env file or a Dockerfile to create the folder and point to them later, but I’m looking for a way to just use docker compose file if possible.
I have recently reading the extension method, but not sure if that would work.
Can anyone help?
How about you post your current compose file, which might make it easier to understand.
As bluepuma said, your Compose file will help us understand what you want to achieve
But if I understand correctly, you want three mounts placed within the same directory, so that changing it will affect all three of them
That can be achieved in this way, for example:
services:
someservice:
...
volumes:
- ${MOUNTS:-./mounts}/mount1:/container/mount1/path
- ${MOUNTS:-./mounts}/mount2:/container/mount2/path
- ${MOUNTS:-./mounts}/mount3:/container/mount3/path
This would interpolate the $MOUNTS
variable from the .env
file placed in the same directory as the compose file
If $MOUNTS
is empty or undefined, the default value ./mounts
will be used
Thank you for the reply. @bluepuma77 Here is a portion of my compose file right now.
Just as the file shows, I used Vol_nextcloud_html
multiple times and if I move the folder, I can just simply change device
to make it work. However, I don’t like the fact that I have to make sure the folder has to exist before bring up the docker compose file.
I wish to find a way to enjoy the same privilege as if I replace every single Vol_nextcloud_html
with the path /media/.../html
into each container volume.
At the same time I do not want to use .env file and Dockerfile.
The method you mention needs to use the .env file, which is one of the file I’m trying to learn if there is a way to avoid.
can I define ${MOUNTS:-./mounts} in extension?
What do you mean by an extension?
And why do you wish to avoid .env
?
I link the related post containing the link to the docs
Just trying to make it easier for me to transfer code when everything is only one file (docker compose file).
Thanks @rimelek.
I did try to use extensions but failed. Here is what I did:
x-envVar:
&test-common
StoredPath = /media/onetouch5t/pi4/data/twitcast/logs
services:
app:
environment:
- <<=*test-common
...
volumes:
- ${StoredPath}:/app/logs
...
I guess I was too greedy.
Thanks guys