Docker 運行時的用戶與組管理的方法

docker 以進程爲核心, 對系統資源進行隔離使用的管理工具. 隔離是通過 cgroups (control groups 進程控制組) 這個操作系統內核特性來實現的. 包括用戶的參數限制、 帳戶管理、 資源(CPU,內存,磁盤I/O,網絡)使用的隔離等. docker 在運行時可以爲容器內進程指定用戶和組. 沒有指定時默認是 root .但因爲隔離的原因, 並不會因此喪失安全性. 傳統上, 特定的應用都以特定的用戶來運行, 在容器內進程指定運行程序的所屬用戶或組並不需要在 host 中事先創建.

進程控制組cgroups主要可能做以下幾件事:

  • 資源限制 組可以設置爲不超過配置的內存限制, 其中還包括文件系統緩存
  • 優先級 某些組可能會獲得更大的 CPU 利用率份額或磁盤 i/o 吞吐量
  • 帳號會計 度量組的資源使用情況, 例如, 用於計費的目的
  • 控制 凍結組進程, 設置進程的檢查點和重新啓動

與 cgroups(控制進程組) 相關聯的概念是 namespaces (命令空間).

命名空間主要有六種名稱隔離類型:

  • PID 命名空間爲進程標識符 (PIDs) 的分配、進程列表及其詳細信息提供了隔離。

雖然新命名空間與其他同級對象隔離, 但其 "父 " 命名空間中的進程仍會看到子命名空間中的所有進程 (儘管具有不同的 PID 編號)。

  • 網絡命名空間隔離網絡接口控制器 (物理或虛擬)、iptables 防火牆規則、路由表等。網絡命名空間可以使用 "veth " 虛擬以太網設備彼此連接。
  • UTS 命名空間允許更改主機名。
  • mount(裝載)命名空間允許創建不同的文件系統佈局, 或使某些裝入點爲只讀。
  • IPC 命名空間將 System V 的進程間通信通過命名空間隔離開來。
  • 用戶命名空間將用戶 id 通過命名空間隔離開來。

普通用戶 docker run 容器內 root

如 busybox, 可以在 docker 容器中以 root 身份運行軟件. 但 docker 容器本身仍以普通用戶執行.

考慮這樣的情況

echo test | docker run -i busybox cat

前面的是當前用戶當前系統進程,後面的轉入容器內用戶和容器內進程運行.

當在容器內 PID 以1運行時, Linux 會忽略信號系統的默認行爲, 進程收到 SIGINT 或 SIGTERM 信號時不會退出, 除非你的進程爲此編碼. 可以通過 Dockerfile STOPSIGNAL signal指定停止信號.

如:

STOPSIGNAL SIGKILL

創建一個 Dockerfile

FROM alpine:latest
RUN apk add --update htop && rm -rf /var/cache/apk/*
CMD ["htop"]
$ docker build -t myhtop . #構建鏡像
$ docker run -it --rm --pid=host myhtop #與 host 進程運行於同一個命名空間

普通用戶 docker run 容器內指定不同用戶 demo_user

docker run --user=demo_user:group1 --group-add group2 <image_name> <command>

這裏的 demo_user 和 group1(主組), group2(副組) 不是主機的用戶和組, 而是創建容器鏡像時創建的.

當Dockerfile裏沒有通過USER指令指定運行用戶時, 容器會以 root 用戶運行進程.

docker 指定用戶的方式

Dockerfile 中指定用戶運行特定的命令

USER <user>[:<group>] #或
USER <UID>[:<GID>]

docker run -u(--user)[user:group] 或 --group-add 參數方式

$ docker run busybox cat /etc/passwd
root:x:0:0:root:/root:/bin/sh
...
www-data:x:33:33:www-data:/var/www:/bin/false
nobody:x:65534:65534:nobody:/home:/bin/false

$ docker run --user www-data busybox id
uid=33(www-data) gid=33(www-data)

docker 容器內用戶的權限

對比以下情況, host 中普通用戶創建的文件, 到 docker 容器下映射成了 root 用戶屬主:

$ mkdir test && touch test/a.txt && cd test
$ docker run --rm -it -v `pwd`:/mnt -w /mnt busybox  /bin/sh -c 'ls -al /mnt/*'
-rw-r--r--  1 root   root       0 Oct 22 15:36 /mnt/a.txt

而在容器內卷目錄中創建的文件, 則對應 host 當前執行 docker 的用戶:

$ docker run --rm -it -v `pwd`:/mnt -w /mnt busybox  /bin/sh -c 'touch b.txt'
$ ls -al
-rw-r--r-- 1 xwx staff  0 10 22 23:36 a.txt
-rw-r--r-- 1 xwx staff  0 10 22 23:54 b.txt

docker volume 文件訪問權限

創建和使用卷, docker 不支持相對路徑的掛載點, 多個容器可以同時使用同一個卷.

$ docker volume create hello #創建卷

hello

$ docker run -it --rm -v hello:/world -w /world busybox /bin/sh -c 'touch /world/a.txt && ls -al'  #容器內建個文件
total 8
drwxr-xr-x  2 root   root     4096 Oct 22 16:38 .
drwxr-xr-x  1 root   root     4096 Oct 22 16:38 ..
-rw-r--r--  1 root   root       0 Oct 22 16:38 a.txt

$ docker run -it --rm -v hello:/world -w /world busybox /bin/sh -c 'rm /world/a.txt && ls -al' #從容器內刪除
total 8
drwxr-xr-x  2 root   root     4096 Oct 22 16:38 .
drwxr-xr-x  1 root   root     4096 Oct 22 16:38 ..

外部創建文件, 容器內指定用戶去刪除

$ touch c.txt && sudo chmod root:wheel c.txt
$ docker run -u 100 -it --rm -v `pwd`:/world -w /world busybox /bin/sh -c 'rm /world/c.txt && ls -al'

實際是可以刪除的

rm: remove '/world/c.txt'? y
total 4
drwxr-xr-x  4 100   root      128 Oct 23 16:09 .
drwxr-xr-x  1 root   root     4096 Oct 23 16:09 ..
-rw-r--r--  1 100   root       0 Oct 22 15:36 a.txt
-rw-r--r--  1 100   root       0 Oct 22 15:54 b.txt

docker 普通用戶的1024以下端口權限

$ docker run -u 100 -it --rm -p 70:80 busybox /bin/sh -c 'nc -l -p 80'
nc: bind: Permission denied #用戶id 100 時, 不能打開80端口
$ docker run -u 100 -it --rm -p 70:8800 busybox /bin/sh -c 'nc -l -p 8800' #容器端口大於1024時則可以
...
$ docker run -it --rm -p 70:80 busybox /bin/sh -c 'nc -l -p 80' #容器內是 root 也可以
...

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

文章同步發佈: https://www.geek-share.com/detail/2755227386.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章