Last week we brought the reverse article, how to migrate from
Docker volumes to Bind Mounts - Read it here:
Migrate docker volumes to bind mounts
Docker containers can store persistent data using either ordinary directories on the host or Docker-managed named volumes.
A bind mount might look like this:
volumes:
- ./postgres-data:/var/lib/postgresql/data
A Docker named volume looks like this:
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data:
Both approaches work well, but they have different advantages.
If you currently use bind mounts and decide that you would rather let Docker manage your persistent storage, you can migrate the existing data into a named volume.
This guide shows how.
Bind Mounts vs Docker Volumes
The main difference is who manages the storage.
With a bind mount:
./postgres-data:/var/lib/postgresql/data
the left-hand side is an ordinary directory on your host.
With a named volume:
postgres-data:/var/lib/postgresql/data
the left-hand side is managed by Docker.
The application inside the container can use either one without knowing the difference.
Bind Mounts - Pros and Cons
| Pros |
Cons |
| Data location is immediately visible |
Host filesystem permissions must be managed correctly |
| Easy to inspect with normal Linux tools |
Host users can directly modify application data |
| Easy to include in filesystem backups |
Compose projects can be less portable between hosts |
| Easy to move to another disk |
You are responsible for managing the directories |
| Works naturally with existing filesystem layouts |
Accidental host-side changes can damage application data |
Docker Volumes - Pros and Cons
| Pros |
Cons |
| Docker manages the storage |
Data is less visible from the host |
| Good separation between application and host filesystem |
Inspecting data requires Docker commands or access to Docker's storage |
| Simple Compose configuration |
Backups need to explicitly account for Docker volumes |
| Easy to recreate on another Docker host |
Moving data outside Docker requires an additional migration step |
| Less opportunity for accidental host-side modification |
Physical storage location is abstracted away |
Why Move From Bind Mounts to Docker Volumes?
There are several reasons you might decide that Docker-managed volumes are a better fit.
You may want Docker to manage the lifecycle of persistent storage rather than having application data sitting directly inside your project directory.
You may also want to reduce the amount of host-specific configuration in your Compose project.
For example, a project currently using:
services:
postgres:
image: postgres:16-alpine
volumes:
- ./postgres-data:/var/lib/postgresql/data
could instead use:
services:
postgres:
image: postgres:16-alpine
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data:
Docker now owns the persistent storage.
Important: Changing the Compose File Does Not Migrate the Data
Just like moving in the other direction, simply changing the volume declaration is not enough.
If you change:
./postgres-data:/var/lib/postgresql/data
to:
postgres-data:/var/lib/postgresql/data
Docker will create an empty named volume if it does not already exist.
Your existing database is still sitting in
./postgres-data.
You therefore need to copy the existing data into the new Docker volume before starting the application with the new configuration.
1. Stop the Project
First, stop the Compose project:
docker compose down
This stops and removes the containers while leaving your bind-mounted data untouched.
2. Create the Docker Volume
You can let Compose create the volume, or create it explicitly.
For example:
docker volume create dockerProject_postgres-data
Check that it exists:
docker volume ls | grep dockerProject
You should see something similar to:
local dockerProject_postgres-data
3. Copy the Existing Bind-Mounted Data Into the Volume
The easiest way to copy the data is to temporarily mount both locations into an Alpine container.
For PostgreSQL:
docker run --rm \
-v "$PWD/postgres-data:/from:ro" \
-v dockerProject_postgres-data:/to \
alpine \
sh -c 'cp -a /from/. /to/'
The source directory is mounted read-only using
:ro.
The Docker volume is mounted as
/to and receives the copied data.
For Redis:
docker run --rm \
-v "$PWD/redis-data:/from:ro" \
-v dockerProject_redis-data:/to \
alpine \
sh -c 'cp -a /from/. /to/'
And for MinIO:
docker run --rm \
-v "$PWD/minio-data:/from:ro" \
-v dockerProject_minio-data:/to \
alpine \
sh -c 'cp -a /from/. /to/'
4. Verify the Volume Contents
You can inspect the contents of the new Docker volume without changing your Compose configuration yet.
For example:
docker run --rm \
-v dockerProject_postgres-data:/data:ro \
alpine \
ls -la /data
For PostgreSQL, you should see files and directories such as:
PG_VERSION
base/
global/
pg_wal/
postgresql.auto.conf
You can also check the size:
docker run --rm \
-v dockerProject_postgres-data:/data:ro \
alpine \
du -sh /data
This is useful for confirming that the volume contains the data you expected before switching the application over.
5. Change Docker Compose
Now change your Compose file.
The original bind mount might look like:
services:
postgres:
image: postgres:16-alpine
volumes:
- ./postgres-data:/var/lib/postgresql/data
Change it to this (essentially removing the
./):
services:
postgres:
image: postgres:16-alpine
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data:
For a project using several persistent services:
services:
postgres:
image: postgres:16-alpine
volumes:
- postgres-data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
volumes:
- redis-data:/data
minio:
image: minio/minio:latest
volumes:
- minio-data:/data
volumes:
postgres-data:
redis-data:
minio-data:
The directories on the host are no longer referenced by Compose.
6. Start the Project
Start the application:
docker compose up -d
Check the containers:
docker compose ps
If you're migrating a database, check its logs:
docker compose logs postgres --tail=50
You want to see the database successfully starting and accepting connections.
7. Verify the Application
Don't consider the migration complete simply because the containers are running.
Open the application and check thoroughly that everything is as it should be.
The important thing is to verify that the application is now reading and writing the migrated Docker volumes.
8. Keep the Original Directories
Do not immediately delete the old bind-mounted directories.
Your original data is still sitting on the host:
./postgres-data
./redis-data
./minio-data
Keep these directories until you are satisfied that the new Docker volumes are working correctly.
They give you a fallback if you discover that something was missed during the migration.
9. Remove the Old Bind-Mounted Data
Once you have verified the migration and have an appropriate backup, you can remove the old directories.
For example:
rm -rf postgres-data
rm -rf redis-data
rm -rf minio-data
Be extremely careful with this step.
These directories contain the original application data. Make absolutely certain that the Docker volumes contain the data you need before deleting them.
If you are unsure - keep the directories.
Disk space is generally cheaper than recovering a deleted database (though 2026 might feel different).
Where Does Docker Store the Volume?
One of the main differences with this migration is that you no longer have to decide where the persistent data lives.
Docker manages the location.
You can find the volume's details with:
docker volume inspect dockerProject_postgres-data
You will see information including its mount point.
On a typical Linux Docker installation, named volumes are commonly stored somewhere below:
/var/lib/docker/volumes/
However, you should use
docker volume inspect rather than assuming a particular storage location.
What About Backups?
Moving to Docker volumes changes how you should think about filesystem backups.
With a bind mount, a backup system can simply include:
/var/lib/docker/dockerProject/postgres-data
With a Docker volume, the data is managed by Docker instead.
Your backup strategy therefore needs to explicitly account for the volume.
One option is to use a temporary container to create an archive of the volume.
For example:
docker run --rm \
-v dockerProject_postgres-data:/data:ro \
-v "$PWD:/backup" \
alpine \
tar czf /backup/postgres-data-backup.tar.gz -C /data .
For important databases, however, a database-aware backup such as
pg_dump is generally preferable to treating the raw PostgreSQL data directory as a conventional file backup.
When Does a Docker Volume Make More Sense?
A named volume can be attractive when you want Docker to manage persistent storage rather than making your Compose project dependent on a particular host directory.
It can also make sense when:
- You want a clean separation between application configuration and application data
- You don't need to inspect the underlying files regularly
- You want Docker to manage the storage lifecycle
- You want fewer host filesystem assumptions in your Compose configuration
- You are comfortable managing backups through Docker-aware tooling
When Does a Bind Mount Make More Sense?
A bind mount can still be preferable when you want direct access to the files.
It can make sense when:
- You want the data next to the Compose project
- Your existing backup system operates on ordinary filesystem paths
- You frequently inspect or manipulate application data
- You want explicit control over which disk stores the data
- You want to move the entire project and its data as one filesystem tree
Conclusion
Moving from bind mounts to Docker named volumes is straightforward as long as you remember one important rule:
Changing the Compose configuration does not move the data.
The migration process is:
- Stop the application.
- Create the Docker volume.
- Copy the existing bind-mounted data into the volume.
- Change the Compose file.
- Start the application.
- Verify the data and application.
- Keep the original directories until you're satisfied.
- Remove the old directories only after verification and backup.
The end result is a project where Docker manages the persistent storage instead of ordinary directories on the host.
Neither approach is universally better. Bind mounts give you direct control over the filesystem, while Docker volumes provide more abstraction and let Docker manage the persistent storage.
The right choice depends on how you want to manage your Docker environment.
Comments (0)
No comments yet. Be the first to comment!
Leave a Comment