【问题标题】:Run each Docker container in a specific user namespace configuration在特定的用户命名空间配置中运行每个 Docker 容器
【发布时间】:2018-02-04 07:35:14
【问题描述】:

问题:

我正在尝试以这种方式将目录挂载为 Docker 卷, 在容器内创建的用户可以编写 到该卷中的文件中。同时,文件应该 至少对容器外我的用户lape 可读。

基本上,我需要将用户 UID 从容器用户命名空间重新映射到主机用户命名空间上的特定 UID。

我该怎么做?

我希望得到以下答案:

  • 不涉及更改 Docker 守护程序的运行方式;
  • 并允许为每个容器分别配置容器用户命名空间;
  • 不需要重建映像;
  • 我也会接受使用 Access Control Lists 的解决方案的答案;

设置:

这就是可以复制这种情况的方式。

我有我的 Linux 用户 lape,分配到 docker 组,所以我 无需 root 即可运行 Docker 容器。

lape@localhost ~ $ id
uid=1000(lape) gid=1000(lape) groups=1000(lape),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),121(lpadmin),131(sambashare),999(docker)

Dockerfile:

FROM alpine
RUN apk add --update su-exec && rm -rf /var/cache/apk/*

# I create a user inside the image which i want to be mapped to my `lape`
RUN adduser -D -u 800 -g 801 insider
VOLUME /data

COPY ./entrypoint.sh /entrypoint.sh
ENTRYPOINT ["sh", "/entrypoint.sh"]

入口点.sh:

#!/bin/sh
chmod 755 /data
chown insider:insider /data

# This will run as `insider`, and will touch a file to the shared volume
# (the name of the file will be current timestamp)
su-exec insider:insider sh -c 'touch /data/$(date +%s)'

# Show permissions of created files
ls -las /data

一旦构建:

docker build -t nstest

我运行容器:

docker run --rm -v $(pwd)/data:/data nstest

输出如下:

total 8
 4 drwxr-xr-x    2 insider  insider       4096 Aug 26 08:44 .
 4 drwxr-xr-x   31 root     root          4096 Aug 26 08:44 ..
 0 -rw-r--r--    1 insider  insider          0 Aug 26 08:44 1503737079

所以文件似乎是作为用户insider创建的。

在我的主机上,权限如下所示:

lape@localhost ~ $ ls -las ./data
total 8
4 drwxr-xr-x 2  800  800 4096 Aug 26 09:44 .
4 drwxrwxr-x 3 lape lape 4096 Aug 26 09:43 ..
0 -rw-r--r-- 1  800  800    0 Aug 26 09:44 1503737079

这表明该文件属于 uid=800(即insider 用户,它甚至不存在于 Docker 命名空间之外)。

我已经尝试过的事情:

  1. 我尝试将 --user 参数指定为 docker run,但它似乎只能将主机上的哪个用户映射到 docker 命名空间内的 uid=0(根),在我的情况下为 @987654338 @ 不是根。所以在这种情况下它并没有真正起作用。

  2. 我从容器中实现insider(uid=800) 的唯一方法是从主机中将--userns-remap="default" 添加到dockerd 启动脚本中,然后添加dockremap:200:100000 到文件 /etc/subuid/etc/subgid,如 documentation for --userns-remap 中所建议的那样。巧合的是,这对我有用,但这还不够,因为:

    • 需要重新配置 Docker 守护进程的运行方式;
    • 需要对用户 ID 进行一些运算:'200 = 1000 - 800',其中 1000 是我在主机上的用户的 UID,而 800 是 insider 用户的 UID;
    • 如果内部用户需要比我的主机用户更高的 UID,这甚至都行不通;
    • 它只能配置用户命名空间的全局映射方式,无法为每个容器进行唯一配置;
    • 这种解决方案很有效,但对于实际使用来说有点太丑了。

【问题讨论】:

  • 你看过 docker 中的用户命名空间:docs.docker.com/engine/security/userns-remap 吗?
  • 是的,也检查过。实际上,我在原始帖子的末尾写了一些结论。我有一种感觉,'userns-remap' 更像是一个防止主机和容器命名空间中的 UID 冲突的功能,但是,我想要的是主机和容器中的用户重叠。另一件事是 'userns-remap' 是在 docker daemon 启动时创建的,无法为单个容器配置它。

标签: linux docker file-permissions linux-namespaces


【解决方案1】:

如果您只需要为您的用户提供读取权限,最简单的方法是使用 docker 外部的 acls 为 /data 中的所有文件和子目录添加读取权限。

添加默认acl:setfacl -d -m u:lape:-rx /data

您还需要授予对目录本身的访问权限:setfacl -m u:lape:-rx /data

这样的解决方案有什么障碍吗?

【讨论】:

    猜你喜欢
    • 2021-01-28
    • 1970-01-01
    • 2019-12-25
    • 2020-08-03
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2011-11-07
    相关资源
    最近更新 更多