Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Just another Geek

Just another geek

Just another Geek

Just another geek

  • Home
  • Shop
    • Shop
    • Cart
    • Checkout
  • About
    • About Me
    • Coding Projects
    • Software Reqs
  • My Collections
    • Other Games
    • NES Games
    • Comics
      • Anime Insider
      • Simpsons
      • Animerica
  • My Extras …
    • My Flight Tracker
    • My Search
    • My Stream
    • My Videos
  • Hitt Hosting
  • Home
  • Shop
    • Shop
    • Cart
    • Checkout
  • About
    • About Me
    • Coding Projects
    • Software Reqs
  • My Collections
    • Other Games
    • NES Games
    • Comics
      • Anime Insider
      • Simpsons
      • Animerica
  • My Extras …
    • My Flight Tracker
    • My Search
    • My Stream
    • My Videos
  • Hitt Hosting
Close

Search

Subscribe
DockerTech

Convert QCow2 Image to a Docker Volume

contact@paulhitt.com
By contact@paulhitt.com
February 23, 2026 2 Min Read
0

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.

contact@paulhitt.com
Author

contact@paulhitt.com

Follow Me
Other Articles
Previous

Convert QCow2 image to AWS AMI

Next

Calculate Radial Kepler Equation using Julia

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Contact

Please enable JavaScript in your browser to complete this form.
Name *
Loading

Cart

Latest Posts

  • Calculate Radial Kepler Equation using Julia
  • Convert QCow2 Image to a Docker Volume
  • Convert QCow2 image to AWS AMI
  • How to Isolate QEMU Hosts
  • Windows 2000 Hardening Script
Copyright 2026 — Just another Geek. All rights reserved. Blogsy WordPress Theme