Docker: Convert Volumes to Local Folders

06-12-2024 - 2 minutes, 5 seconds -
docker volumes data-management documentation

Introduction

Docker named volumes are convenient for managing persistent data. However, there are cases where converting these volumes into local folders becomes necessary for better visibility or easier access. This guide details the steps to achieve this and highlights common issues with shared volumes.


Problem Scenario

In a Docker setup using dbdata for both /data/db and /data/configdb, the same volume is referenced in two locations. This results in identical data being accessible at both paths, as Docker volumes represent a shared storage space.

If distinct data for db and configdb is required, follow these steps to separate the data and transition to local folder-based storage.


Steps to Convert and Separate Volumes

1. Stop Running Containers

Stop the containers using the shared volume to avoid data corruption:

docker stop <container_name>

2. Export the Volume to Local Folders

Use a temporary container to copy the volume data into local folders.

For /data/db:

docker run --rm -v dbdata:/volume -v C:\DockerVolumes\data\db:/target alpine cp -r /volume /target

For /data/configdb:

docker run --rm -v dbdata:/volume -v C:\DockerVolumes\data\configdb:/target alpine cp -r /volume /target

3. Verify the Exported Data

Check the local folders (C:\DockerVolumes\data\db and C:\DockerVolumes\data\configdb) to ensure the data is copied correctly. Since the same volume was used for both paths, the content will initially be identical.

4. Separate the Data

Manually organize the data into distinct folders if necessary:

  • Move database files to C:\DockerVolumes\data\db.
  • Move configuration files to C:\DockerVolumes\data\configdb.

5. Update docker-compose.yml

Modify the volume configuration in your docker-compose.yml to use bind mounts instead of the named volume:

volumes:
  - C:\DockerVolumes\data\db:/data/db
  - C:\DockerVolumes\data\configdb:/data/configdb

6. Restart the Containers

Start the containers with the updated configuration:

docker-compose up -d

Troubleshooting Common Issues

Shared Volume Behavior

If the same volume is referenced in multiple paths, the data will always be identical. Ensure separate volumes or bind mounts are used if distinct data is required.

Volume Permissions

Ensure the local folders have the correct permissions for Docker to access them. On Windows, use the following command to check permissions:

icacls C:\DockerVolumes\data\db

Data Validation

After starting the containers, verify that the applications can access the data correctly:

  • Check logs for errors.
  • Access the application UI to confirm functionality.

Resources and References


Conclusion

Converting Docker volumes to local folders allows for better data visibility and control. By addressing shared volume issues, you can ensure that your applications access distinct and properly managed data. This guide simplifies the transition process, providing a solid foundation for managing Docker data effectively.