docker 以進程爲核心, 對系統資源進行隔離使用的管理工具. 隔離是經過 cgroups (control groups 進程控制組) 這個操做系統內核特性來實現的. 包括用戶的參數限制、 賬戶管理、 資源(CPU,內存,磁盤I/O,網絡)使用的隔離等. docker 在運行時能夠爲容器內進程指定用戶和組. 沒有指定時默認是 root .但由於隔離的緣由, 並不會所以喪失安全性. 傳統上, 特定的應用都以特定的用戶來運行, 在容器內進程指定運行程序的所屬用戶或組並不須要在 host 中事先建立.html
進程控制組cgroups主要可能作如下幾件事:mysql
與 cgroups(控制進程組) 相關聯的概念是 namespaces (命令空間).redis
命名空間主要有六種名稱隔離類型:sql
雖然新命名空間與其餘同級對象隔離, 但其 "父 " 命名空間中的進程仍會看到子命名空間中的全部進程 (儘管具備不一樣的 PID 編號)。docker
普通用戶 docker run 容器內 root緩存
如 busybox, 能夠在 docker 容器中以 root 身份運行軟件. 但 docker 容器自己仍以普通用戶執行.安全
考慮這樣的狀況bash
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 也能夠 ...
以上就是本文的所有內容,但願對你們的學習有所幫助,也但願你們多多支持腳本之家。