溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

docker容器中怎么利用非root用戶執(zhí)行腳本

發(fā)布時間:2021-06-18 16:01:11 來源:億速云 閱讀:268 作者:Leah 欄目:大數(shù)據(jù)

今天就跟大家聊聊有關(guān)docker容器中怎么利用非root用戶執(zhí)行腳本,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。


1、構(gòu)建鏡像:
我將會使用dockerfile的方式來構(gòu)建鏡像,基礎(chǔ)鏡像使用ubuntu 14.04(需要先拉取該鏡像,docker pull ubuntu:14.04)。dockerfile內(nèi)容如下
[root@host09 test]# cat Dockerfile
FROM docker.io/ubuntu:14.04  
MAINTAINER hepengfei

RUN groupadd hpf  --創(chuàng)建用戶組
RUN useradd -d /data -g hpf -m hpf   --創(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)建的用戶來運(yùn)行腳本
[root@host09 test]#

腳本內(nèi)容如下:

[root@host09 test]# cat test.sh
while [ 1 = 1 ]
do
echo `id` >>/data/logs/hpf.log  --將日志輸出到文件,啟動容器的時候做持久化
sleep 1
done
[root@host09 test]#

接下來讓我們來構(gòu)建鏡像:

[root@host09 test]# docker build -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                IMAGE ID           CREATED            SIZE
hpf                v2                 26e7d603dbac       42 minutes ago     188.3 MB
docker.io/ubuntu   14.04              c69811d4e993       3 weeks ago        188 MB
[root@host09 test]#

2、啟動容器:

注意,在啟動容器之前,需要將宿主機(jī)上/data/hepf/log目錄的權(quán)限,否則容器啟動時,腳本中的日志將沒有權(quán)限寫該目錄,我直接將該目錄權(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)在來查看/data/hepf/log目錄中的日志文件:

[root@host09 log]# pwd
/data/hepf/log
[root@host09 log]# ll
total 12
-rw-rw-r-- 1 1000 1000 10800 Sep  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ī)上的日志文件屬主就會變成該用戶,但是暫時沒有發(fā)現(xià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:50 hpf.log
[root@host09 log]#

看完上述內(nèi)容,你們對docker容器中怎么利用非root用戶執(zhí)行腳本有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI