gpt4 book ai didi

Docker userns-remap 无法写入挂载目录

转载 作者:行者123 更新时间:2023-12-04 15:17:55 25 4
gpt4 key购买 nike

我正在试用 userns-remap Docker 在容器内创建文件的功能 root用户并拥有此文件的所有者为 test主机上的用户。

我已将以下内容添加到 /etc/docker/daemon.json

{
"userns-remap": "test:test"
}

重新映射似乎是根据守护进程日志进行的
User namespaces: ID ranges will be mapped to subuid/subgid ranges of: test:test

和条目 test:100000:65536test:100000:65536已添加到 /etc/subuid/etc/subgid/文件,分别。

但是当我启动一个容器并尝试在工作目录中创建一个文件时,它失败了
test@box:~$ docker run -v /home/test/tmp:/somedir -w /somedir -it ubuntu:16.04 /bin/bash
root@11ff6c42ffe1:/somedir# touch file.txt
touch: cannot touch 'file.txt': Permission denied

root@11ff6c42ffe1:/somedir# ls -l
total 0
-rw-rw-r-- 1 nobody nogroup 0 Mar 23 21:39 already_existing_file.txt

root@11ff6c42ffe1:/somedir# id root
uid=0(root) gid=0(root) groups=0(root)

root@11ff6c42ffe1:/somedir# touch /file.txt

在未从主机挂载的其他目录中创建文件按预期工作。

此外,如果 777 权限授予挂载目录,在这种情况下 /home/test/tmp 在主机上,可以从容器内部成功创建文件。但是,新创建的文件在主机上具有以下权限:
ls -l /home/test/tmp
total 0
-rw-r--r-- 1 165536 165536 0 march 29 01:36 file.txt

id 为 165536 的用户不在/etc/passwd 中,这让我们回到了开始。我希望 容器内的用户具有与 相同的权限测试 主机上的用户以及由 创建的文件根容器中的用户在使用 userns-remap 映射的主机上拥有所有者,即 测试 .

docs 中有说明那

...if volumes are mounted from the host, file ownership must be pre-arranged need read or write access to the volume contents.

... One notable restriction is the inability to use the mknod command. Permission is denied for device creation within the container when run by the root user.



这是否意味着 容器内的用户无法在挂载目录中创建文件/目录,即使挂载目录的所有者是 的用户。根映射到使用 userns-remap,在这种情况下 测试 ?

任何想法如何使工作目录也可由容器内的用户写入?

Docker 版本 : 18.03.0
Ubuntu 版本 : 16.04.1
内核版本 : 4.13.0-36-通用

重现步骤
sudo adduser test
sudo usermod -aG docker test
sudo echo '{ "userns-remap": "test"}' >> /etc/docker/daemon.json
service docker restart
su - test
mkdir tmp
docker run -v /home/test/tmp:/somedir ubuntu:16.04 touch /somedir/file.txt

这里有一些类似的问题,但并不完全相同,因为我想在不修改 Dockerfile 的情况下完成这项工作:
  • Docker and --userns-remap, how to manage volume permissions to share data between host and container? - 也正是这个问题中提出的问题,但是 2 年没有答案,所以保持这个问题开放
  • docker non-root bind-mount permissions, WITH --userns-remap
  • Can I control the owner of a bind-mounted volume in a docker image?
  • Docker can't write to directory mounted using -v unless it has 777 permissions
  • 最佳答案

    要回答您的问题,最好的方法是使用 docker 引擎的“用户命名空间”功能。

    这是一个如何使用的示例。
    假设您的主机用户是 myuser与 ID 3000
    添加 myuser:3000:65536到您的/etc/subuid 和/etc/subgid 文件

    用这个更新你的/etc/docker/daemon.json :

    {
    "userns-remap": "myuser"
    }

    不要忘记重新启动您的 docker 引擎 :)

    就是这样,属于您 myuser 的所有文件本地帐户将属于 ID 0是您的容器,反之亦然。

    这应该可以帮助您解决问题。

    让我知道

    关于Docker userns-remap 无法写入挂载目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49538616/

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