docker 以進程為核心, 對系統(tǒng)資源進行隔離使用的管理工具. 隔離是通過 cgroups (control groups 進程控制組) 這個操作系統(tǒng)內核特性來實現(xiàn)的. 包括用戶的參數(shù)限制、 帳戶管理、 資源(CPU,內存,磁盤I/O,網(wǎng)絡)使用的隔離等. docker 在運行時可以為容器內進程指定用戶和組. 沒有指定時默認是 root .但因為隔離的原因, 并不會因此喪失安全性. 傳統(tǒng)上, 特定的應用都以特定的用戶來運行, 在容器內進程指定運行程序的所屬用戶或組并不需要在 host 中事先創(chuàng)建.
進程控制組cgroups主要可能做以下幾件事:
- 資源限制 組可以設置為不超過配置的內存限制, 其中還包括文件系統(tǒng)緩存
- 優(yōu)先級 某些組可能會獲得更大的 CPU 利用率份額或磁盤 i/o 吞吐量
- 帳號會計 度量組的資源使用情況, 例如, 用于計費的目的
- 控制 凍結組進程, 設置進程的檢查點和重新啟動
與 cgroups(控制進程組) 相關聯(lián)的概念是 namespaces (命令空間).
命名空間主要有六種名稱隔離類型:
- PID 命名空間為進程標識符 (PIDs) 的分配、進程列表及其詳細信息提供了隔離。
雖然新命名空間與其他同級對象隔離, 但其 "父 " 命名空間中的進程仍會看到子命名空間中的所有進程 (盡管具有不同的 PID 編號)。
- 網(wǎng)絡命名空間隔離網(wǎng)絡接口控制器 (物理或虛擬)、iptables 防火墻規(guī)則、路由表等。網(wǎng)絡命名空間可以使用 "veth " 虛擬以太網(wǎng)設備彼此連接。
- UTS 命名空間允許更改主機名。
- mount(裝載)命名空間允許創(chuàng)建不同的文件系統(tǒng)布局, 或使某些裝入點為只讀。
- IPC 命名空間將 System V 的進程間通信通過命名空間隔離開來。
- 用戶命名空間將用戶 id 通過命名空間隔離開來。
普通用戶 docker run 容器內 root
如 busybox, 可以在 docker 容器中以 root 身份運行軟件. 但 docker 容器本身仍以普通用戶執(zhí)行.
考慮這樣的情況
echo test | docker run -i busybox cat
前面的是當前用戶當前系統(tǒng)進程,后面的轉入容器內用戶和容器內進程運行.
當在容器內 PID 以1運行時, Linux 會忽略信號系統(tǒng)的默認行為, 進程收到 SIGINT 或 SIGTERM 信號時不會退出, 除非你的進程為此編碼. 可以通過 Dockerfile STOPSIGNAL signal指定停止信號.
如:
創(chuàng)建一個 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 進程運行于同一個命名空間
![](http://img.jbzj.com/file_images/article/201810/2018102409591844.jpg?201892410028)
普通用戶 docker run 容器內指定不同用戶 demo_user
docker run --user=demo_user:group1 --group-add group2 <image_name> <command>
這里的 demo_user 和 group1(主組), group2(副組) 不是主機的用戶和組, 而是創(chuàng)建容器鏡像時創(chuàng)建的.
當Dockerfile里沒有通過USER指令指定運行用戶時, 容器會以 root 用戶運行進程.
docker 指定用戶的方式
Dockerfile 中指定用戶運行特定的命令
USER <user>[:<group>] #或
USER <UID>[:<GID>]
docker run -u(--user)[user:group] 或 --group-add 參數(shù)方式
$ 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 中普通用戶創(chuàng)建的文件, 到 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
而在容器內卷目錄中創(chuàng)建的文件, 則對應 host 當前執(zhí)行 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 文件訪問權限
創(chuàng)建和使用卷, docker 不支持相對路徑的掛載點, 多個容器可以同時使用同一個卷.
$ docker volume create hello #創(chuàng)建卷
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 ..
外部創(chuàng)建文件, 容器內指定用戶去刪除
$ 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 也可以
...
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。