For a project I need to mount a linux image inside a docker container running ubuntu. The image I want to mount is Raspbian. I need to access the linux filesystem of the image and add a file.
I access the image by mounting the folder with the volume flag:
docker run -it -v /path/to/image/folder:/default ubuntu /bin/bash
With fdisk -l raspbian.img
I found the offset:
Disk raspbian.img: 1.3 GiB, 1389363200 bytes, 2713600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x5a7089a1
Device Boot Start End Sectors Size Id Type
raspbian.img1 8192 137215 129024 63M c W95 FAT32 (LBA)
raspbian.img2 137216 2713599 2576384 1.2G 83 Linux
Now when I try to mount the image with mount -o loop,offset=$((137216*512)) raspbian.img /mnt/
I get mount: /mnt/: mount failed: Unknown error -1
. Can someone explain if I can mount a linux image in a running docker container and if so how?
Edit
Doing the same mount operations in vagrant works perfectly. Are there some limitations to docker mounting filesystems?
Running a GUI program in Docker can be a useful technique when you're evaluating a new piece of software. You can install the software in a clean container, instead of having to pollute your host with new packages. This approach also helps you avoid any incompatibilities with other packages in your environment.
If you're running Docker on Linux, you have a third option: tmpfs mounts. When you create a container with a tmpfs mount, the container can create files outside the container's writable layer. As opposed to volumes and bind mounts, a tmpfs mount is temporary, and only persisted in the host memory.
This template allows you to deploy an Ubuntu VM with Docker (using the Docker Extension). You can later SSH into the VM and run Docker containers.
Are there some limitations to docker mounting filesystems?
Yes. A standard Docker container has a number of security restrictions in place. As you have discovered, you can't mount new filesystems. You are also unable to modify the network environment of the container.
One solution is simply to perform the mount operation on the host, and then expose the mounted directory into the container using the -v
argument to docker run
. Something like:
# losetup -fP --show raspbian.img
/dev/loop0
# mount /dev/loop0p2 /mnt
# docker run -v /mnt:/raspbian ubuntu bash
But if you really want to perform the mount inside the container, you can run a privileged container, using the --privileged
option to docker run
. This removes most of the restrictions normally placed on a Docker container:
/dev
.For example:
# docker run -it --rm --privileged -v /images:/images ubuntu bash
Now I can inspect the image:
root@30f80d4598dc:/# fdisk -l /images/2016-09-23-raspbian-jessie-lite.img
Disk /images/2016-09-23-raspbian-jessie-lite.img: 1.3 GiB, 1389363200 bytes, 2713600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x5a7089a1
Device Boot Start End Sectors Size Id Type
/images/2016-09-23-raspbian-jessie-lite.img1 8192 137215 129024 63M c W95 FAT
/images/2016-09-23-raspbian-jessie-lite.img2 137216 2713599 2576384 1.2G 83 Linux
And mount it:
root@952a75f105ee:/# mount -o loop,offset=$((137216*512)) /images/2016-09-23-raspbian-jessie-lite.img /mnt
root@952a75f105ee:/# ls /mnt
bin dev home lib64 media opt root sbin sys usr
boot etc lib lost+found mnt proc run srv tmp var
root@952a75f105ee:/#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With