How to Mount an Iso Inside a Docker Container

Mount linux image in docker container

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:

  • You will have complete access to he host's /dev.
  • You will be able to mount filesystems.
  • You will be able to modify the network configuration inside the container.

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:/#

how to let docker-machine use a local boot2docker.iso

You can do a "dirty trick". You can remove the argument of specifying a url for the iso --hyperv-boot2docker-url file:///D:/docker/boot2docker.iso doing this:

Overwrite the boot2docker.iso file. Is under your %userprofile%\.docker\machine\cache folder. Then cut your internet access by unplugging network cable or however. Then launch your command.

Usually if you don't specify anything for the iso, it checks the checksum of the file. It's different from the original so it will try to download a new one and after failing because you don't have internet access it will copy your custom boot2docker.iso file from your %userprofile%\.docker\machine\cache folder to %userprofile%\.docker\machine\machines\default and it will be used to create the machine.

Hope it helps.

How to use big file only to build the container without adding it?

Use Docker's multi-stage builds. This mechanism allows you to drop intermediate artifacts and therefore achieve a lightweight image.

Example:

FROM alpine:latest as build
# copy large file
# build

FROM alpine:latest as output
# copy necessary files built in the previous stage
COPY --from=build app /app

Anything built in the build stage will not be included in the final image, unless you explicitly COPY them.

Docs: https://docs.docker.com/develop/develop-images/multistage-build/



Related Topics



Leave a reply



Submit