Last active
December 6, 2025 15:45
-
-
Save metinsenturk/59eb63aad772390f095d035470d93abb to your computer and use it in GitHub Desktop.
Create docker volume with subfolders inside the volume.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This script has functions to create a Docker volume and manages folders | |
| # within the volume. | |
| # Author: Metin Senturk | |
| # Url: https://gist.github.com/metinsenturk/59eb63aad772390f095d035470d93abb | |
| __create_volume_with_folders() { | |
| local volume_name="$1" | |
| local command="$2" | |
| shift | |
| shift | |
| local folders=("$@") | |
| # Create the Docker volume | |
| vol=$(docker volume create $volume_name) | |
| echo "Created volume: '$vol'" | |
| # Build the mkdir command for all folders | |
| local full_command=$command | |
| for folder in "${folders[@]}"; do | |
| full_command+=" //data/$folder" | |
| done | |
| echo "Full command: $full_command" | |
| # Run a temporary container to create the subfolders | |
| # --rm to remove tmp container after folder creation | |
| # --mount to mount volume to this tmp container | |
| # using a tmp image (alpine) to create folder(s) in the volume | |
| docker run --rm --mount type=volume,source=$volume_name,target=//data alpine sh -c "$full_command" | |
| } | |
| # Function: create_volume_add_folders | |
| # Description: Creates a docker volume and adds subfolders within it. | |
| # Arguments: | |
| # $1: The name of the Docker volume to create. | |
| # $2: A list of folder names to create inside the volume. | |
| # Returns: None | |
| # Usage: create_volume_add_folders <volume_name> <folder1> <folder2> ... | |
| # Example: create_volume_add_folders my_volume folder1 folder2/subfolder | |
| create_volume_add_folders() { | |
| local volume_name="$1" | |
| shift | |
| local folders=("$@") | |
| local cmd="mkdir -p" | |
| __create_volume_with_folders "$volume_name" "$cmd" "${folders[@]}" | |
| echo "Created folders: ${folders[*]} in volume '$volume_name'" | |
| } | |
| # Function: create_volume_remove_folders | |
| # Description: Creates a docker volume and removes subfolders within it. | |
| # Arguments: | |
| # $1: The name of the Docker volume to create. | |
| # $2: A list of folder names to create inside the volume. | |
| # Returns: None | |
| # Usage: create_volume_remove_folders <volume_name> <folder1> <folder2> ... | |
| # Example: create_volume_remove_folders my_volume folder1 folder2/subfolder | |
| create_volume_remove_folders() { | |
| local volume_name="$1" | |
| shift | |
| local folders=("$@") | |
| local cmd="rm -rf" | |
| __create_volume_with_folders "$volume_name" "$cmd" "${folders[@]}" | |
| echo "Removed folders: ${folders[*]} in volume '$volume_name'" | |
| } | |
| # Example usage | |
| # add folders to a volume | |
| # create_volume_add_folders home_data app1 app2 app3 | |
| # remove folders to a volume | |
| # create_volume_remove_folders home_data app1 app2 |
Author
Author
Quickly I realized that having subfolders comes with maintenance ๐. Therefore I made 2 functions, at least to make it easier to add or remove folders.
# add two folders: a1 and a2
create_volume_add_folders home_data a1 a2
# remove one: a2
create_volume_remove_folders home_data a2Hope this helps.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ideally, every container or service in a docker compose file has their own volume and it makes it very easy to to use volumes this way. However, having a lot of volumes can also get messy, or you just have a use case, such as wanting to collect all of your logs into as single volume, which makes sense.
Docker allows multiple containers to use the same volume as of March 2024. However, there is a catch. Within the volume, subfolders must exist. With docker compose file, you need adjustments if you want services to use same volume but different subfolders. Here is how to do it:
That's it, easy peasy.
For example, I did this for postgres and pgadmin. For the full docker compose file, see below.
That's it๐