我試圖安裝一個目錄作爲泊塢量以這樣的方式, 一個用戶,這是一個容器內創建可以寫 到該卷的文件中。同時,文件 至少對容器外部的用戶lape
可讀。
本質上,我需要將用戶UID從容器用戶名空間重新映射到主機用戶名空間上的特定UID。
我該怎麼做?
我寧願答案是:
- 不涉及改變碼頭工人守護進程運行方式的方式;
- 並允許分別爲每個容器配置容器用戶名空間;
- 不需要重建圖像;
- 我會接受顯示使用Access Control Lists以及一個很好的解決方案的答案;
設置:
情況是這樣的怎麼可以被複制。
我有我的Linux用戶lape
,分配到docker
組,所以我 可以運行Docker容器而不用root。
[email protected] ~ $ 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"]
entrypoint.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
創建。
從我的主機的權限是這樣的:
[email protected] ~ $ 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
用戶不連泊塢命名空間之外存在)。
觀光我嘗試已經:
我試圖指定
--user
參數docker run
,但似乎它只能哪個用戶在主機上被映射地圖爲uid = 0(root)的搬運工內部命名空間,在我的情況下,insider
不是root。所以在這種情況下它並不真正起作用。我如何實現
insider
從容器內(UID = 800)的唯一途徑,被視爲LAPE從主機(UID = 1000),是通過將--userns-remap="default"
到dockerd
啓動腳本,並加入dockremap:200:100000
到文件/etc/subuid
如documentation for --userns-remap中建議的/etc/subgid
。巧合的是,這對我來說很有效,但它並不足以解決問題,因爲:- 它需要重新配置Docker守護程序如何運行的方式;
- 需要對用戶ID進行一些算術運算:'200 = 1000 - 800',其中1000是主機上我用戶的UID,800是UID是
insider
用戶; - 如果內部用戶需要比我的主機用戶擁有更高的UID,那麼這個功能甚至不會工作;
- 它只能配置全局映射用戶名稱空間的方式,而不能爲每個容器配置唯一的配置;
- 這種解決方案的作品,但實際使用它有點太醜了。
你看了一下docker中的用戶命名空間:https://docs.docker.com/engine/security/userns-remap/? – yamenk
是的,也檢查了。我在原來的帖子結尾寫了一些結論。我有一種感覺,'userns-remap'更能防止主機和容器命名空間中的UID衝突,但是,我想要的是用戶在主機和容器中的重疊。另一件事是當docker守護進程啓動時創建'userns-remap',並且無法爲單個容器配置它。 –