Below is a practical, step‑by‑step workflow for turning a qcow2 disk image into a Docker volume that you can attach to containers. The process assumes you have a Linux host with root (or sudo) privileges and the necessary tools installed.


1. Install the required utilities

sudo apt-get update
sudo apt-get install -y qemu-utils qemu-system-x86   # provides qemu‑nbd

qemu-nbd lets you expose the qcow2 image as a network block device, which can then be mounted like a regular filesystem.


2. Load the NBD kernel module

sudo modprobe nbd max_part=8   # allow up to 8 partitions (adjust if needed)

3. Connect the qcow2 image to an NBD device

# Replace /path/to/image.qcow2 with the actual path
sudo qemu-nbd --connect=/dev/nbd0 /path/to/image.qcow2

At this point /dev/nbd0 represents the whole disk. If the image contains partitions, they will appear as /dev/nbd0p1, /dev/nbd0p2, …

Tip: Run lsblk to see the layout and identify the partition that holds the data you need.

4. Mount the desired partition

Assuming the data lives in the first partition (/dev/nbd0p1):

sudo mkdir -p /mnt/qcow2
sudo mount /dev/nbd0p1 /mnt/qcow2

Now the filesystem inside the qcow2 image is accessible under /mnt/qcow2.


5. Create a Docker volume

docker volume create my_qcow2_volume

Docker will store the volume’s data under /var/lib/docker/volumes/my_qcow2_volume/_data (the exact path may vary with your Docker storage driver).


6. Copy the contents into the Docker volume

There are two common ways to do this:

Option A – Using a temporary container with a bind‑mount

docker run --rm \
  -v my_qcow2_volume:/dest \
  -v /mnt/qcow2:/src:ro \
  alpine \
  sh -c "cp -a /src/. /dest/"

Explanation:

  • my_qcow2_volume:/dest mounts the newly created Docker volume inside the container.
  • /mnt/qcow2:/src:ro bind‑mounts the mounted qcow2 directory read‑only.
  • cp -a preserves attributes while copying everything.

Option B – Using docker cp from the host

# Start a helper container that mounts the volume
docker run -d --name helper -v my_qcow2_volume:/dest alpine sleep infinity

# Copy the files from the host into the container’s volume
sudo docker cp /mnt/qcow2/. helper:/dest

# Stop and remove the helper container
docker stop helper && docker rm helper

Both options leave the data inside the Docker volume ready for any subsequent container.


7. Clean up the temporary mounts

sudo umount /mnt/qcow2
sudo qemu-nbd --disconnect /dev/nbd0
sudo rmdir /mnt/qcow2

If you loaded the NBD module solely for this operation, you can unload it:

sudo rmmod nbd

8. Verify the volume contents (optional)

docker run --rm -v my_qcow2_volume:/data alpine ls -l /data

You should see the same directory tree that was present inside the qcow2 image.


Summary checklist

StepCommand (short)
Install toolsapt-get install qemu-utils qemu-system-x86
Load NBD modulemodprobe nbd max_part=8
Attach imageqemu-nbd --connect=/dev/nbd0 image.qcow2
Mount partitionmount /dev/nbd0p1 /mnt/qcow2
Create volumedocker volume create my_qcow2_volume
Copy datadocker run -v my_qcow2_volume:/dest -v /mnt/qcow2:/src:ro alpine cp -a /src/. /dest/
Unmount & disconnectumount /mnt/qcow2 && qemu-nbd --disconnect /dev/nbd0

Following these steps will reliably migrate the filesystem from a qcow2 virtual‑disk image into a Docker-managed volume, allowing you to reuse the data inside any container that mounts my_qcow2_volume.