Docker 如何实现非root用户构建httpd镜像以及遇到的问题有哪些

  • Post author:
  • Post category:其他




为什么要用非root来构建httpd呢?

由于公司云平台,一般的web组件。不允许用root用户来部署相关应用程序。所以只能编写dockerfile先构建镜像,然后再部署到云平台(kubenetes集群),主要从安全问题来考虑的。还有本来我这个职位职责是管理租户应用问题,但是测试机器没部署。只能自己找镜像,写file部署。中间遇到了很多问题,可能由于我的技术比较菜导致的吧。掉坑里一天出不来,最后还是慢慢排错出来的。不聊别的了,聊聊技术吧。

  • 首先我们需要准备一个干净的镜像,因为我们云平台有docker仓库Harbor。所以我只需要使用命令拉取镜像:
docker push 10.0.0.1/public/bclinux:1.1.1
  • 拉取镜像之后,需要使用docker run 命令去运行,我拉取的镜像:
docker run --name tudo:1.1.1 -tid 10.0.0.1/public/bclinux:1.1.1
  • 进入运行的docker的容器中(未运行的叫镜像、运行了就叫容器了)
docker exec -ti tudo:1.1.1 /bin/bash
  • 修改基础镜像的yum源文件,因为内网涉及安全问题一般yum源配置是:
cat >/etc/yum.repos.d/os.repo<<EOF
[centos7]
name=Server
baseurl=http://10.0.0.1/fpz #源路径
gpgcheck=0
gpgcheck=1             #是否检查GPG(GNU Private Guard),一种密钥方式签名。 
EOF
  • 使用yum install -y httpd 部署安装完成后使用systemctl启动报错
# systemctl start httpd 
System has not been booed with systemd as init system (PID 1).Can't operate.
Failed to connect to bus: Host is down 
  • 怎么解决报错呢?答案是跳过这种方式去启动httpd直接用路径启动
#type httpd
httpd is /usr/sbin/httpd
  • 执行完命令发现还有问题
#/usr/sbin/httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.0.0.9. Set the 'ServerName' directive globally to suppress this message 
httpd (pid 18)  already running
报错的意思是我们没有修改/etc/httpd/conf/http.conf配置文件的参数导致 
ServerName localhost:80
再启动
#/usr/sbin/httpd
启动成功了
  • 启动后我们要编写docker file文件
vi /date/dockerfile
FROM 10.0.0.1/public/bclinux:1.1.1
RUN groupadd www
RUN useradd www -g www -u 1911
RUN chown -R www:www  /etc/httpd/conf/httpd.conf
RUN chmod -R 755 /etc/httpd/conf/httpd.conf
RUN chown -R www:www /usr/sbin/httpd
RUN chown -R www:www /etc/httpd/logs/
RUN chmod -R 755 /etc/httpd/logs/
RUN chown -R www:www /run/httpd/
RUN chmod -R 755 /run/httpd
RUN chmod -R 755 /usr/sbin/httpd
RUN mkdir -p  /var/tmp/logs/
RUN chown -R www:www /var/tmp/
RUN mkdir -p /date
ADD start.sh /date
RUN chmod a+x /date/start.sh
RUN chown -R www:www /date/start.sh
RUN chmod -R 755 /var/tmp/
RUN sed -i 's/User apache/User www/g' /etc/httpd/conf/httpd.conf
RUN sed -i 's/Group apache/Group www/g' /etc/httpd/conf/httpd.conf
RUN sed -i 's/#ServerName www.example.com:80/ServerName localhost:8080/g' /etc/httpd/conf/httpd.conf
RUN sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
USER 1911
ENTRYPOINT ["sh","/date/start.sh"]
  • 启动脚本start.sh编写
vi /date/start.sh
#!/bin/bash
/usr/sbin/httpd -D FOREGROUND
不加 -D FOREGROUND会报错别问我为什么知道的。因为这里我卡了很久。。。。
重启提示AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
  • 构建镜像使用docker命令
1.构建命令
docker build -f dockerfile -t httpjingxing:1.1.1 .
(还有个问题docker构建镜像镜像名不能大写会报错)
2.上传命令
docker push httpjingxing:1.1.1 

到此完结。。。。。。。。。。。。



版权声明:本文为m0_47293933原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。