TechBuzzPro

Move Docker Volume to Another Partition

· 6 min read · Apps & Browsers

In this article
  1. Background: How Docker Volumes Work
  2. Why Move a Volume to a Different Partition
  3. Option 1: Move the Entire Docker Data Directory
  4. Option 2: Move Only a Specific Volume
  5. How to Choose the Right Method
  6. Common Problems and Pitfalls
  7. Comparison: Symlink vs. data-root vs. Bind Mount
  8. Costs and Safety Considerations
  9. Frequently Asked Questions

Yes, you can move a Docker volume to a different partition, and the standard method is to copy the volume's data to the new location and then update the Docker daemon's configuration so it uses the new path. This is a common task when a disk fills up or when you want to place containers on faster storage, and it can be done without losing data if you follow the steps carefully.

Background: How Docker Volumes Work

Docker volumes are directories that live on the host filesystem, typically under /var/lib/docker/volumes/. Each volume has a subdirectory named after the volume's ID (a long hash), and inside that subdirectory there is a _data folder that holds the actual files. When you mount a volume into a container, Docker binds that _data directory to a path inside the container.

The default location /var/lib/docker is on the same partition as the root filesystem. If that partition runs out of space, you can either move the entire Docker data directory (which includes images, containers, and volumes) to a larger partition, or you can move just the volume data to a different mount point and configure Docker to use it.

Why Move a Volume to a Different Partition

  • Disk space: The root partition is full, but another partition (e.g., a data drive) has plenty of free space.
  • Performance: You want volumes on an SSD or a faster RAID array, while the rest of Docker stays on a slower HDD.
  • Backup or migration: You are moving containers to a new host and want to place volume data on a dedicated storage partition.
  • Separation of concerns: You want to keep volatile container data separate from system files for easier backups or snapshots.

Option 1: Move the Entire Docker Data Directory

The simplest and most common approach is to move the whole /var/lib/docker directory to a new partition. This moves all volumes, images, and containers at once. Here are the steps:

  1. Stop the Docker daemon: sudo systemctl stop docker (or sudo service docker stop on older systems).
  2. Copy the existing data to the new location. For example, if the new partition is mounted at /mnt/docker:
    sudo rsync -avxP /var/lib/docker/ /mnt/docker/
  3. Rename the old directory as a backup: sudo mv /var/lib/docker /var/lib/docker.bak
  4. Create a symlink so Docker still finds the data at the default path: sudo ln -s /mnt/docker /var/lib/docker
  5. Start Docker again: sudo systemctl start docker
  6. Verify that containers and volumes are intact: docker volume ls and docker ps -a.
  7. Once you are sure everything works, remove the backup: sudo rm -rf /var/lib/docker.bak

Using a symlink is a common pattern, but you can also edit the Docker daemon configuration file (/etc/docker/daemon.json) and set the data-root option to the new path. That is a more permanent solution and is recommended if you want to avoid symlinks.

Option 2: Move Only a Specific Volume

If you only need to move one volume (or a few) to a different partition, you can do that without moving the entire Docker data directory. The idea is to copy the volume's data to the new location and then mount that location into the container instead of using the Docker-managed volume.

  1. Stop the container that uses the volume: docker stop <container_name>
  2. Find the volume's mount point on the host: docker volume inspect <volume_name> – look for the Mountpoint field, e.g., /var/lib/docker/volumes/myvolume/_data.
  3. Copy the data to the new partition, e.g., sudo cp -a /var/lib/docker/volumes/myvolume/_data /mnt/data/myvolume
  4. Remove the old volume: docker volume rm myvolume (only after you have a copy).
  5. Recreate the container, but this time mount the new host directory as a bind mount instead of using the volume. For example, with docker run -v /mnt/data/myvolume:/app/data ... or in a docker-compose.yml file: volumes: - /mnt/data/myvolume:/app/data.

This approach changes the container definition, so it is not a true “move” of the volume itself, but it achieves the goal of relocating the data to another partition. Docker volumes are tied to the daemon's data root, so you cannot have a single volume stored in a different partition without either moving the whole data root or using bind mounts.

How to Choose the Right Method

  • If you want to move all Docker data (images, containers, volumes) to a new partition, use the data-root method.
  • If you only have one or two volumes that need to be on a different partition, use the bind-mount approach.
  • If you are moving to a new host, consider using docker run --mount with the new location directly, or use a tool like docker cp to copy data out and back in.

Common Problems and Pitfalls

  • Permission errors: After copying, the files may have different ownership. Use sudo chown -R root:root /mnt/docker (or the appropriate user) to match the original.
  • Symlink issues: If you use a symlink, make sure Docker is not running when you create it. Also, some tools (like backup scripts) may follow the symlink unexpectedly.
  • Data loss: Always test after the move. Start a container and verify that the data is readable. Do not delete the original until you are certain.
  • Docker version differences: The data-root option was introduced in Docker 17.06. If you have an older version, you may need to use the symlink method or upgrade.
MethodScopePersistenceEase
SymlinkAll Docker dataBreaks if symlink is removedEasy, but less clean
data-root in daemon.jsonAll Docker dataPermanent, survives restartsClean, recommended
Bind mount for a volumeSingle volumeDepends on container definitionRequires recreating container

Costs and Safety Considerations

Moving Docker data is free in terms of software, but it does require downtime. You must stop the Docker daemon, which means any running containers will be stopped. Plan for a maintenance window. The main risk is data loss if you accidentally delete the original before verifying the copy. Always keep a backup of the original directory until you are confident the new location works.

Also consider that moving to a different partition might change the filesystem type (e.g., from ext4 to XFS). Docker works on any standard Linux filesystem, but if you are using special features like overlay2 storage driver, ensure the new partition supports them (most do).

Frequently Asked Questions

Can I move a Docker volume without stopping the container?

No, you should stop the container (or the Docker daemon) before moving the data. If you copy files while the container is writing to them, you risk corruption. The safest way is to stop the container, copy, then start it again.

Will moving the volume affect container performance?

It can, depending on the new partition's speed. If you move to an SSD, performance may improve; if you move to a slower HDD, it may degrade. The filesystem type and mount options also play a role.

How do I know which partition my volumes are on?

Run df -h /var/lib/docker to see the partition and usage. You can also inspect a specific volume with docker volume inspect <name> to see its mount point, then df -h on that path.

Can I move a volume to another machine?

Yes, you can copy the volume's _data directory to another host and use it as a bind mount or import it as a new volume. However, the container itself must be recreated on the new host, and any container-specific files (like environment variables) are not part of the volume.

What happens if I change the data-root and the new path is empty?

Docker will create a fresh data directory and you will lose access to your existing volumes and images. That is why you must copy the old data to the new location before changing the configuration.

Is it safe to use a symlink for /var/lib/docker?

Yes, it is safe as long as the symlink points to a valid directory and Docker has permission to read and write through it. However, some system tools or backup scripts may not follow symlinks by default, so the data-root method is generally preferred for production.