gpt4 book ai didi

linux - 在 docker 容器中挂载 linux 镜像

转载 作者:IT老高 更新时间:2023-10-28 21:25:17 27 4
gpt4 key购买 nike

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

我通过挂载带有卷标志的文件夹来访问图像:

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/:挂载失败:未知错误-1。有人可以解释一下我是否可以在正在运行的 docker 容器中挂载一个 linux 镜像,如果可以的话怎么办?

编辑

在 vagrant 中执行相同的挂载操作非常有效。 docker 挂载文件系统有一些限制吗?

最佳答案

Are there some limitations to docker mounting filesystems?

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

一种解决方案是简单地在主机上执行挂载操作,然后使用 docker run-v 参数将挂载的目录公开到容器中。比如:

# losetup -fP --show raspbian.img
/dev/loop0
# mount /dev/loop0p2 /mnt
# docker run -v /mnt:/raspbian ubuntu bash

但是如果你真的想在容器内执行挂载,你可以运行一个特权容器,使用 docker run--privileged 选项。这消除了通常放置在 Docker 容器上的大部分限制:

  • 您将可以完全访问主机的 /dev
  • 您将能够挂载文件系统。
  • 您将能够修改容器内的网络配置。

例如:

# docker run -it --rm --privileged -v /images:/images ubuntu bash

现在我可以检查图像了:

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

关于linux - 在 docker 容器中挂载 linux 镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40356259/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com