zoukankan      html  css  js  c++  java
  • docker容器以非root用户启动应用

      docker容器启动应用默认的是root用户,可以使用ps命令来查看。很多的目录及文件权限是777,这些都是不安全的。

           最近的一项工作就是要以非root用户启动docker,并且修改777权限为755.

           在Dockefile中创建指定的用户xxx和用户组,然后用su-exec xxx  java ... 来启动应用,使用ps查看,进程的启动用户就是xxx了。

           以非root用户启动docker应用的一个问题是,需要提前创建好一些文件夹,并把文件夹的owner和group改为xxx。否则,日志,临时文件,解压操作等都会提示没有权限。

          我的做法是,假如当前目录是/opt/manager,临时文件或解压目录是/opt/manager/tmp/.../...abc.zip,就在dockfile中创建/opt/manager/tmp,并把文件夹的owner和group改为xxx。

          su-exec可以在一个进程中完成切换用户和执行命令,不用su之后再执行命令。

          https://www.jianshu.com/p/6cde4396da97  这也是用非root的业务用户来运行应用。记录一下。

      应用容器化之后,在docker容器启动时,默认使用的是root用户执行命令,因此容器中的应用默认都是使用root用户来运行的,存在很高的安全风险,那么如何能够使用非root的业务用户来运行应用呢,下面我将举一个简单的例子来说明。该例子是在容器中使用自建的用户来运行一个简单的shell脚本,并将脚本输出日志持久到容器外部。接下来让我们来看从制作镜像到容器运行的全过程吧。
      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]#
      简单的例子到这里就结束了。

          

  • 相关阅读:
    BZOJ3124 直径
    BZOJ1491 洛谷2047 NOI2007 社交网络
    TYVJ1423 GF和猫咪的玩具
    poj 3463 Sightseeing
    TYVJ2032 升降梯上
    NOIP2009 codevs1173 洛谷P1073 最优贸易
    [BZOJ1066] [SCOI2007] 蜥蜴 (网络流)
    [BZOJ3293] [Cqoi2011] 分金币 (贪心)
    [BZOJ1045] [HAOI2008] 糖果传递 (贪心)
    [BZOJ1005] [HNOI2008] 明明的烦恼 (prufer编码)
  • 原文地址:https://www.cnblogs.com/lnlvinso/p/13081187.html
Copyright © 2011-2022 走看看