docker容器以非root用户启动应用

摘要:
Docker容器的默认启动应用程序是root,777权限修改为755。在Dockfile中创建指定的用户xxx和用户组。临时文件或解压缩目录为/opt/manager/tmp/…/Abc.zip,因此默认情况下容器中的应用程序由根用户运行。此示例使用一个自建用户在容器中运行一个简单的shell脚本。

  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]#
  简单的例子到这里就结束了。

      

免责声明:文章转载自《docker容器以非root用户启动应用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ORA-01843 无效的月份python 读取共享内存下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mysql主从复制、redis基础、持久化和主从复制

一、mysql(mariadb)基础 1、基础命令(centos7操作系统下) 1.启动mysql systemctl start mariadb 2.linux客户端连接自己 mysql -uroot -p -h 127.0.0.1 -u 用户 -p 密码验证 -h 连接的主机地址 3.远程链接mysql服务端 mysql...

quartz结合多线程处理后台业务

  最近项目中有播放视频的需求,技术选型采用UMS播放器,免费版只能播放FLV格式的视频文件,因此需要对用户上传的视频进行格式转换,转换工具为FormatFactory,功能还是比较强大的。但是面临的一个问题,视频转换是非常耗时的,上传完直接转换是没法接受的,于是决定采用quartz,以任务调度的方式,在后台进行转换,具体步骤如下:   1.定义一个任务...

深度之眼PyTorch训练营第二期 ---6、nn.Module与网络模型构建

一、网络模型创建步骤 数据->模型->损失函数->优化器 1、模型创建 (1)构建子模块->卷积层,池化层,激活函数层 (2)拼接子模块->LeNet,AlexNet,ResNet等 2、权值初始化->Xavier,Kaiming,均匀分布,正态分布 二、nn.Module属性 torch.nn  nn.Parame...

vue 点击展开显示更多 点击收起部分隐藏

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <style ty...

python pip升级失败

接上篇,使用命令:python -m pip install --upgrade pip 发现pip升级时报错,无法升级 解决方法: 1、使用如下命令,查看具体失败原因: pip install --upgrade pip –vvv 2、果然在最后有提示,原来对我的用户文件夹没有访问权限: 3、查看该文件夹的所有者为system,更改所有者为当前用户 4...

Android使用GoogleMap v2(一)

使用之前的一些准备: https://developers.google.com/maps/documentation/android/start#get_an_android_certificate_and_the_google_maps_api_key (官网的详细教程) Creating a new Android application that...