服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - 服务器知识 - 在docker容器中使用非root用户执行脚本操作

在docker容器中使用非root用户执行脚本操作

2021-05-20 18:44何鹏飞竟然被用了 服务器知识

这篇文章主要介绍了在docker容器中使用非root用户执行脚本操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

应用容器化之后,在docker容器启动时,默认使用的是root用户执行命令,因此容器中的应用默认都是使用root用户来运行的,存在很高的安全风险,那么如何能够使用非root的业务用户来运行应用呢,

下面我将举一个简单的例子来说明。

该例子是在容器中使用自建的用户来运行一个简单的shell脚本,并将脚本输出日志持久到容器外部。接下来让我们来看从制作镜像到容器运行的全过程吧。

1、构建镜像:

我将会使用dockerfile的方式来构建镜像,基础镜像使用ubuntu 14.04(需要先拉取该镜像,docker pullubuntu:14.04)。dockerfile内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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
2
3
4
5
6
7
[root@host09 test]# cattest.sh
while [ 1 = 1 ]
do
echo `id`>>/data/logs/hpf.log --将日志输出到文件,启动容器的时候做持久化
sleep 1
done
[root@host09 test]#

 

接下来让我们来构建镜像:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[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]#

 

查看所构建的镜像:

?
1
2
3
4
5
[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目录的权限,否则容器启动时,脚本中的日志将没有权限写该目录,我直接将该目录权限修改成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目录中的日志文件:

?
1
2
3
4
5
6
7
8
9
[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]#

可以看到,该文件的属主跟容器中创建的hpf用户是一致的:

?
1
2
3
hpf@ba688af3f598:~$ id
uid=1000(hpf) gid=1000(hpf) groups=1000(hpf)
hpf@ba688af3f598:~$

如果宿主机上已有其他用户跟容器中创建用户的id一样的话,宿主机上的日志文件属主就会变成该用户,但是暂时没有发现什么问题。

?
1
2
3
4
5
[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]#

简单的例子到这里就结束了。

补充知识:docker默认存放以及docker 非root用户

方法1

sudo docker info | grep “Docker Root Dir”

首先停掉Docker服务:

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 的配置文件可以设置大部分的后台进程参数,在各个操作系统中的存放位置不一致,在 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、 首先创建docker用户组,如果docker用户组存在可以忽略

sudo groupadd docker

2、把用户添加进docker组中

sudo gpasswd -a ${USER} docker

3、重启docker

sudo service docker restart

4、如果普通用户执行docker命令,如果提示get …… dial unix /var/run/docker.sock权限不够,则修改/var/run/docker.sock权限

使用root用户执行如下命令,即可

sudo chmod a+rw /var/run/docker.sock

以上这篇在docker容器中使用非root用户执行脚本操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_39776561/article/details/77886938

延伸 · 阅读

精彩推荐