最近在开始学习Docker,看到很多教程里使用httpd的容器做讲义,但是不知道为啥我下载的httpd容器里没有yum/vim等工具,使用起来很不方便,所以就想自己在一个标准的Centos容器里通过dockerfile重建一个带有httpd功能的系统。本以为很简单通过yum安装httpd并进行配置即可,没想到yum install以后发现容器里没有systemctl命令,当然可以通过privileged参数来打开systemctl的功能,但是这种方法个人感觉毕竟权限太高了,而且也有违容器的初衷,所以也是不可取的。废话不多说,直接开干。
宿主机系统:CentOS7.5
容器系统:CentOS8(通过docker pull默认拉取的最新的系统版本)
操作目的:通过dockerfile文件重新build一个带有httpd功能的镜像
操作也很简单,只需要2个脚本即可:
DockerFile文件:/home/df_centos_httpd
FROM centos
MAINTAINER lizy
RUN yum update -y
RUN yum install -y vim
RUN yum install -y httpd
RUN touch /var/www/html/index.html
RUN echo "I have finished, pls give me five!!!! ---lizy " >> /var/www/html/index.html
EXPOSE 80
ADD run-httpd.sh /run-httpd.sh
RUN chmod -v +x /run-httpd.sh
CMD ["/run-httpd.sh"]
httpd启动脚本,此脚本的目的在于在每次启动容器时可以清理残留环境,建议和dockerfile放到同一目录下:/home/run-httpd.sh
#!/bin/bash
# Make sure we're not confused by old, incompletely-shutdown httpd
# context after restarting the container. httpd won't start correctly
# if it thinks it is already running.
rm -rf /run/httpd/*
exec /usr/sbin/httpd -D FOREGROUND
根据dockerfile生成新的image
docker build -f /home/df_centos_httpd -t lizy/httpd:lizy .
通过docker images命令查看新生成的镜像
启动新生成的镜像,并把镜像的80端口映射到宿主机的8080
docker run -itd -p 8080:80 --name=myhttpd lizy/httpd:lizy
使用命令验证是否成功
curl 127.0.0.1:8080
参考文章链接:
https://blog.csdn.net/weixin_30904593/article/details/98101680