濮阳杆衣贸易有限公司

主頁 > 知識庫 > 在docker容器中使用非root用戶執(zhí)行腳本操作

在docker容器中使用非root用戶執(zhí)行腳本操作

熱門標簽:企業(yè)辦理400電話收費標準 中國地圖標注上各個省 智能電銷機器人真的有用么 撫州市城區(qū)地圖標注 激光標記地圖標注 高德地圖標注足跡怎么打標 百度地圖底圖標注 電銷智能機器人試用 新鄉(xiāng)牧野400電話申請

應用容器化之后,在docker容器啟動時,默認使用的是root用戶執(zhí)行命令,因此容器中的應用默認都是使用root用戶來運行的,存在很高的安全風險,那么如何能夠使用非root的業(yè)務(wù)用戶來運行應用呢,

下面我將舉一個簡單的例子來說明。

該例子是在容器中使用自建的用戶來運行一個簡單的shell腳本,并將腳本輸出日志持久到容器外部。接下來讓我們來看從制作鏡像到容器運行的全過程吧。

1、構(gòu)建鏡像:

我將會使用dockerfile的方式來構(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)建的用戶來運行腳本
[root@host09 test]#

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

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

接下來讓我們來構(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、啟動容器:

注意,在啟動容器之前,需要將宿主機上/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 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:~$

如果宿主機上已有其他用戶跟容器中創(chuàng)建用戶的id一樣的話,宿主機上的日志文件屬主就會變成該用戶,但是暫時沒有發(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:50hpf.log
[root@host09 log]#

簡單的例子到這里就結(jié)束了。

補充知識:docker默認存放以及docker 非root用戶

方法1

sudo docker info | grep “Docker Root Dir”

首先停掉Docker服務(wù):

systemctl restart docker

或者

service docker stop

然后移動整個/var/lib/docker目錄到目的路徑:

mv /var/lib/docker /root/data/docker

ln -s /root/data/docker /var/lib/docker

方法2

Docker 的配置文件可以設(shè)置大部分的后臺進程參數(shù),在各個操作系統(tǒng)中的存放位置不一致,在 Ubuntu 中的位置是:/etc/default/docker,在 CentOS 中的位置是:/etc/sysconfig/docker。

如果是 CentOS 則添加下面這行:

OPTIONS=–graph=”/root/data/docker” –selinux-enabled -H fd://

如果是 Ubuntu 則添加下面這行(因為 Ubuntu 默認沒開啟 selinux):

OPTIONS=–graph=”/root/data/docker” -H fd://

或者

DOCKER_OPTS=”-g /root/data/docker”

1、 首先創(chuàng)建docker用戶組,如果docker用戶組存在可以忽略

sudo groupadd docker

2、把用戶添加進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í)行腳本操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

標簽:西安 忻州 南通 海西 臨汾 邯鄲 延安 辛集

巨人網(wǎng)絡(luò)通訊聲明:本文標題《在docker容器中使用非root用戶執(zhí)行腳本操作》,本文關(guān)鍵詞  在,docker,容器,中,使用,非,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在docker容器中使用非root用戶執(zhí)行腳本操作》相關(guān)的同類信息!
  • 本頁收集關(guān)于在docker容器中使用非root用戶執(zhí)行腳本操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    宁城县| 郓城县| 瑞昌市| 乐昌市| 大关县| 镇远县| 增城市| 三明市| 石楼县| 裕民县| 博爱县| 苏尼特左旗| 宽甸| 壶关县| 连城县| 花垣县| 叙永县| 丹寨县| 青龙| 靖江市| 新密市| 称多县| 图片| 米泉市| 游戏| 林芝县| 张家界市| 上栗县| 昌江| 大英县| 秭归县| 沈阳市| 涟水县| 滦平县| 普宁市| 崇仁县| 丹棱县| 华池县| 酉阳| 太原市| 通辽市|