在docker容器中使用非root用戶執(zhí)行腳本操作
應(yīng)用容器化之后,在docker容器啟動(dòng)時(shí),默認(rèn)使用的是root用戶執(zhí)行命令,因此容器中的應(yīng)用默認(rèn)都是使用root用戶來(lái)運(yùn)行的,存在很高的安全風(fēng)險(xiǎn),那么如何能夠使用非root的業(yè)務(wù)用戶來(lái)運(yùn)行應(yīng)用呢,
下面我將舉一個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明。
該例子是在容器中使用自建的用戶來(lái)運(yùn)行一個(gè)簡(jiǎn)單的shell腳本,并將腳本輸出日志持久到容器外部。接下來(lái)讓我們來(lái)看從制作鏡像到容器運(yùn)行的全過(guò)程吧。
1、構(gòu)建鏡像:
我將會(huì)使用dockerfile的方式來(lái)構(gòu)建鏡像,基礎(chǔ)鏡像使用ubuntu 14.04(需要先拉取該鏡像,docker pullubuntu:14.04)。dockerfile內(nèi)容如下
[root@host09 test]# cat Dockerfile FROMdocker.io/ubuntu:14.04 MAINTAINER hepengfei RUN groupadd hpf --創(chuàng)建用戶組 RUN useradd -d /data -g hpf -mhpf --創(chuàng)建用戶 RUN su - hpf -c "mkdir -p /data/scripts" RUN su - hpf -c "mkdir -p /data/logs" WORKDIR /data/scripts COPY test.sh /data/scripts/ RUN chown hpf:hpf test.sh RUN chmod 755 test.sh ENTRYPOINT su - hpf -c "/data/scripts/test.sh" --使用所創(chuàng)建的用戶來(lái)運(yùn)行腳本 [root@host09 test]#
腳本內(nèi)容如下:
[root@host09 test]# cattest.sh while [ 1 = 1 ] do echo `id`>>/data/logs/hpf.log --將日志輸出到文件,啟動(dòng)容器的時(shí)候做持久化 sleep 1 done [root@host09 test]#
接下來(lái)讓我們來(lái)構(gòu)建鏡像:
[root@host09 test]# dockerbuild -t hpf:v2 . Sending build context to Docker daemon 3.072 kB Step 1 : FROM docker.io/ubuntu:14.04 ---> c69811d4e993 Step 2 : MAINTAINER hepengfei ---> Using cache ---> b8401d2eb439 Step 3 : RUN groupadd hpf ---> Using cache ---> 2e0d20802c41 Step 4 : RUN useradd -d /data -g hpf -m hpf ---> Using cache ---> bac36ee97aba Step 5 : RUN su - hpf -c "mkdir -p /data/scripts" ---> Using cache ---> a92c3f5f8e34 Step 6 : RUN su - hpf -c "mkdir -p /data/logs" ---> Using cache ---> 2e8665da7092 Step 7 : WORKDIR /data/scripts ---> Using cache ---> 7cf84a5a8aca Step 8 : COPY test.sh /data/scripts/ ---> 7e4c24de2096 Removing intermediate container f96358d91c35 Step 9 : RUN chown hpf:hpf test.sh ---> Running in fc9ab290c56c ---> f38afd1ea62c Removing intermediate container fc9ab290c56c Step 10 : RUN chmod 755 test.sh ---> Running in a35b507a1527 ---> 5b5223249f4c Removing intermediate container a35b507a1527 Step 11 : ENTRYPOINT su - hpf -c "/data/scripts/test.sh" ---> Running in 1ee7cc7fbec7 ---> 26e7d603dbac Removing intermediate container 1ee7cc7fbec7 Successfully built 26e7d603dbac [root@host09 test]#
查看所構(gòu)建的鏡像:
[root@host09 test]# docker images REPOSITORY TAG IMAGEID CREATED SIZE hpf v2 26e7d603dbac 42 minutesago 188.3 MB docker.io/ubuntu 14.04 c69811d4e993 3 weeksago 188 MB [root@host09 test]#
2、啟動(dòng)容器:
注意,在啟動(dòng)容器之前,需要將宿主機(jī)上/data/hepf/log目錄的權(quán)限,否則容器啟動(dòng)時(shí),腳本中的日志將沒(méi)有權(quán)限寫(xiě)該目錄,我直接將該目錄權(quán)限修改成777了。
[root@host09 test]#chmod 777/data/hepf/log
[root@host09 test]# docker run -it -v/data/hepf/log:/data/logs hpf:v2
現(xiàn)在來(lái)查看/data/hepf/log目錄中的日志文件:
[root@host09 log]# pwd /data/hepf/log [root@host09 log]# ll total 12 -rw-rw-r-- 1 1000 1000 10800Sep 7 08:02 hpf.log [root@host09 log]# tail -2 hpf.log uid=1000(hpf) gid=1000(hpf) groups=1000(hpf) uid=1000(hpf) gid=1000(hpf) groups=1000(hpf) [root@host09 log]#
可以看到,該文件的屬主跟容器中創(chuàng)建的hpf用戶是一致的:
hpf@ba688af3f598:~$ id uid=1000(hpf) gid=1000(hpf) groups=1000(hpf) hpf@ba688af3f598:~$
如果宿主機(jī)上已有其他用戶跟容器中創(chuàng)建用戶的id一樣的話,宿主機(jī)上的日志文件屬主就會(huì)變成該用戶,但是暫時(shí)沒(méi)有發(fā)現(xiàn)什么問(wèn)題。
[root@host09 log]# cat /etc/passwd |grep hpf1 hpf1:x:1000:1000::/data1:/bin/bash[root@host09 log]# ll total 12 -rw-rw-r-- 1 hpf1 hpf1 11250 Sep 7 08:50hpf.log [root@host09 log]#
簡(jiǎn)單的例子到這里就結(jié)束了。
補(bǔ)充知識(shí):docker默認(rèn)存放以及docker 非root用戶
方法1
sudo docker info | grep “Docker Root Dir”
首先停掉Docker服務(wù):
systemctl restart docker
或者
service docker stop
然后移動(dòng)整個(gè)/var/lib/docker目錄到目的路徑:
mv /var/lib/docker /root/data/docker
ln -s /root/data/docker /var/lib/docker
方法2
Docker 的配置文件可以設(shè)置大部分的后臺(tái)進(jìn)程參數(shù),在各個(gè)操作系統(tǒng)中的存放位置不一致,在 Ubuntu 中的位置是:/etc/default/docker,在 CentOS 中的位置是:/etc/sysconfig/docker。
如果是 CentOS 則添加下面這行:
OPTIONS=–graph=”/root/data/docker” –selinux-enabled -H fd://
如果是 Ubuntu 則添加下面這行(因?yàn)?Ubuntu 默認(rèn)沒(méi)開(kāi)啟 selinux):
OPTIONS=–graph=”/root/data/docker” -H fd://
或者
DOCKER_OPTS=”-g /root/data/docker”
1、 首先創(chuàng)建docker用戶組,如果docker用戶組存在可以忽略
sudo groupadd docker
2、把用戶添加進(jìn)docker組中
sudo gpasswd -a ${USER} docker
3、重啟docker
sudo service docker restart
4、如果普通用戶執(zhí)行docker命令,如果提示get …… dial unix /var/run/docker.sock權(quán)限不夠,則修改/var/run/docker.sock權(quán)限
使用root用戶執(zhí)行如下命令,即可
sudo chmod a+rw /var/run/docker.sock
以上這篇在docker容器中使用非root用戶執(zhí)行腳本操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。