在Docker容器中挂载Linux映像

在Docker容器中挂载Linux映像

本文介绍了在Docker容器中挂载Linux映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我需要在运行的Docker容器中安装Linux映像。我要挂载的映像是Raspbian。我需要访问映像的linux文件系统并添加文件。

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

使用 fdisk -l raspbian.img 我发现了偏移量:

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

现在,当我尝试使用 mount -o loop,offset = $(((137216 * 512))raspbian.img / mnt / 我得到 mount:/ mnt /:mou nt失败:未知错误-1 。有人可以解释我是否可以在正在运行的docker容器中挂载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?

编辑

在无业游民中执行相同的装入操作非常有效。

Doing the same mount operations in vagrant works perfectly. Are there some limitations to docker mounting filesystems?

推荐答案

是。标准Docker容器具有许多安全限制。如您所知,您无法挂载新文件系统。您也无法修改容器的网络环境。

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.

一种解决方案是简单地在主机上执行安装操作,然后将已安装的目录公开到容器中对 docker run 使用 -v 参数。像这样的东西:

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

但是如果您真的想在容器内执行挂载,则可以运行特权容器,使用-privileged 选项对 docker run 。这消除了通常对Docker容器施加的大多数限制:

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

  • 您将能够挂载文件系统。

  • 您将能够修改内部的网络配置。

例如:

# 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

并安装它:

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

这篇关于在Docker容器中挂载Linux映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:49