在docker容器中使用非root用户执行脚本
来源:互联网 发布:html调用java方法 编辑:程序博客网 时间:2024/06/14 05:19
应用容器化之后,在docker容器启动时,默认使用的是root用户执行命令,因此容器中的应用默认都是使用root用户来运行的,存在很高的安全风险,那么如何能够使用非root的业务用户来运行应用呢,下面我将举一个简单的例子来说明。该例子是在容器中使用自建的用户来运行一个简单的shell脚本,并将脚本输出日志持久到容器外部。接下来让我们来看从制作镜像到容器运行的全过程吧。
1、构建镜像:
我将会使用dockerfile的方式来构建镜像,基础镜像使用ubuntu 14.04(需要先拉取该镜像,docker pullubuntu:14.04)。dockerfile内容如下
[root@host09 test]# cat Dockerfile
FROMdocker.io/ubuntu:14.04
MAINTAINER hepengfei
RUN groupadd hpf --创建用户组
RUN useradd -d /data -g hpf -mhpf --创建用户
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]#
1、构建镜像:
我将会使用dockerfile的方式来构建镜像,基础镜像使用ubuntu 14.04(需要先拉取该镜像,docker pullubuntu:14.04)。dockerfile内容如下
[root@host09 test]# cat Dockerfile
FROMdocker.io/ubuntu:14.04
MAINTAINER hepengfei
RUN groupadd hpf
RUN useradd -d /data -g hpf -mhpf
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]# cattest.sh
while [ 1 = 1 ]
do
echo `id`>>/data/logs/hpf.log --将日志输出到文件,启动容器的时候做持久化
sleep 1
done
[root@host09 test]#
while [ 1 = 1 ]
do
echo `id`>>/data/logs/hpf.log
sleep 1
done
[root@host09 test]#
接下来让我们来构建镜像:
[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]#
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM docker.io/ubuntu:14.04
Step 2 : MAINTAINER hepengfei
Step 3 : RUN groupadd hpf
Step 4 : RUN useradd -d /data -g hpf -m hpf
Step 5 : RUN su - hpf -c "mkdir -p /data/scripts"
Step 6 : RUN su - hpf -c "mkdir -p /data/logs"
Step 7 : WORKDIR /data/scripts
Step 8 : COPY test.sh /data/scripts/
Removing intermediate container f96358d91c35
Step 9 : RUN chown hpf:hpf test.sh
Removing intermediate container fc9ab290c56c
Step 10 : RUN chmod 755 test.sh
Removing intermediate container a35b507a1527
Step 11 : ENTRYPOINT su - hpf -c "/data/scripts/test.sh"
Removing intermediate container 1ee7cc7fbec7
Successfully built 26e7d603dbac
[root@host09 test]#
查看所构建的镜像:
[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]#
REPOSITORY
hpf
docker.io/ubuntu
[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 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]#
/data/hepf/log
[root@host09 log]# ll
total 12
-rw-rw-r-- 1 1000 1000 10800Sep
[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:~$
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:50hpf.log
[root@host09 log]#
简单的例子到这里就结束了。hpf1:x:1000:1000::/data1:/bin/bash[root@host09 log]# ll
total 12
-rw-rw-r-- 1 hpf1 hpf1 11250 Sep
[root@host09 log]#
阅读全文
0 0
- 在docker容器中使用非root用户执行脚本
- Docker安全--关于Docker使用root与非root用户的场景中的容器与host中的执行用户的研究
- Docker使用非root用户
- 非root用户使用docker
- Docker使用非root用户
- Docker配置非root用户执行客户端命令
- 非root用户登录docker
- 非root用户运行docker
- linux使用某非root用户执行开机启动项
- 非root用户执行程序---sudo的使用
- 在非Spring容器中使用注入
- docker默认存放以及docker 非root用户
- 非root 用户swith to root运行脚本
- 让Raw Socket的应用程序在非root用户下执行
- ubuntu 在非root用户下抓包设置
- 在linux shell脚本中root切换到普通用户执行脚本或命令
- Linux在脚本中实现普通用户到root用户的切换(包括脚本输入密码)
- ubuntu12+ 非root用户使用串口
- WeCenter 中 (可能所有图片上传功能)图片上传如果出现图片上传无反应
- Lua_语句_if语句_005
- tomcat6.0 发布 jdk1.7
- hdu1002 水题,大数相加
- 例题6-7 树的层次遍历(Trees on the level, Duke 1993, UVa 122)
- 在docker容器中使用非root用户执行脚本
- codeforces 846/B Math Show(思维)
- 拨开字符编码的迷雾--MySQL数据库字符编码
- 9月笔试
- selenium自动化学习一环境搭建
- POJ1837-Balance(01背包)
- Mapped Statements collection does not contain value for
- Java 基础 —— Objects and Classes
- C语言制作游戏——贪吃蛇