您好,登录后才能下订单哦!
今天就跟大家聊聊有关docker容器中怎么利用非root用户执行脚本,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
 1、构建镜像:
 我将会使用dockerfile的方式来构建镜像,基础镜像使用ubuntu 14.04(需要先拉取该镜像,docker pull ubuntu:14.04)。dockerfile内容如下
 [root@host09 test]# cat Dockerfile
 FROM docker.io/ubuntu:14.04  
 MAINTAINER hepengfei
 
 RUN groupadd hpf  --创建用户组
 RUN useradd -d /data -g hpf -m hpf   --创建用户
 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" --使用所创建的用户来运行脚本
 [root@host09 test]#
脚本内容如下:
[root@host09 test]# cat test.sh
 while [ 1 = 1 ]
 do
 echo `id` >>/data/logs/hpf.log  --将日志输出到文件,启动容器的时候做持久化
 sleep 1
 done
 [root@host09 test]#
接下来让我们来构建镜像:
[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]#
查看所构建的镜像:
[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、启动容器:
注意,在启动容器之前,需要将宿主机上/data/hepf/log目录的权限,否则容器启动时,脚本中的日志将没有权限写该目录,我直接将该目录权限修改成777了。
[root@host09 test]# chmod 777 /data/hepf/log
[root@host09 test]# docker run -it -v /data/hepf/log:/data/logs hpf:v2
现在来查看/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]#
可以看到,该文件的属主跟容器中创建的hpf用户是一致的:
hpf@ba688af3f598:~$ id
 uid=1000(hpf) gid=1000(hpf) groups=1000(hpf)
 hpf@ba688af3f598:~$
如果宿主机上已有其他用户跟容器中创建用户的id一样的话,宿主机上的日志文件属主就会变成该用户,但是暂时没有发现什么问题。
[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]#
看完上述内容,你们对docker容器中怎么利用非root用户执行脚本有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。