容器中使用主机卷部署httpd

目录

拉取Centos镜像

配置yum源

安装httpd

配置文件

写service文件

删除多余文件及卸载不用的包

镜像制作

web站点部署

访问测试


拉取Centos镜像

 [root@master ~]# docker images
 REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
 mysql        5.7       3147495b3a5c   13 days ago   431MB
 [root@master ~]# docker pull centos:7  #拉取镜像
 7: Pulling from library/centos
 2d473b07cdd5: Pull complete 
 Digest: sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407
 Status: Downloaded newer image for centos:7
 docker.io/library/centos:7  
 [root@master ~]# docker run -itd --name httpd centos:7   #启动
 198be6cd882f76b6517acb7e206c0a9af6113466e78147c0663cfbe69b336b1e
 [root@master ~]# 
 [root@master ~]# docker ps #这里看到容器已经在运行了
 CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS          PORTS     NAMES
 198be6cd882f   centos:7   "/bin/bash"   58 seconds ago   Up 57 seconds             httpd
 [root@master ~]# docker exec -it httpd /bin/bash
 #进入容器 这个命令的意思就是在httpd容器中开启一个交互模式的终端
 [root@198be6cd882f /]# ls
 anaconda-post.log  dev  home  lib64  mnt  proc  run   srv  tmp  var
 bin                etc  lib   media  opt  root  sbin  sys  usr
 [root@198be6cd882f /]# 

配置yum源

 [root@198be6cd882f /]# cd /etc/yum.repos.d/
 [root@198be6cd882f yum.repos.d]# ls
 CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Sources.repo  CentOS-fasttrack.repo
 CentOS-CR.repo    CentOS-Media.repo      CentOS-Vault.repo    CentOS-x86_64-kernel.repo
 [root@198be6cd882f yum.repos.d]# rm -rf *
 [root@198be6cd882f yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
 100  2523  100  2523    0     0   9238      0 --:--:-- --:--:-- --:--:--  9275
 #这是用的阿里源
 [root@198be6cd882f yum.repos.d]# ls
 CentOS-Base.repo
 [root@198be6cd882f yum.repos.d]# 

安装httpd

 [root@198be6cd882f yum.repos.d]# useradd -r -M -s /sbin/nologin apache      #创建用户 
 [root@198be6cd882f yum.repos.d]# yum -y install openssl-devel pcre-devel make expat-devel wget vim gcc gcc-c++ libtool          #安装需要使用的wget和httpd相关依赖包
 ​
 [root@198be6cd882f ~]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
 [root@198be6cd882f ~]# wget https://downloads.apache.org/apr/apr-1.6.5.tar.gz
 [root@198be6cd882f ~]#  wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
 [root@198be6cd882f ~]# ls
 anaconda-ks.cfg  apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz
 #ok
 [root@198be6cd882f ~]# tar -xf apr-1.6.5.tar.gz 
 [root@198be6cd882f ~]# tar -xf apr-util-1.6.1.tar.gz 
 [root@198be6cd882f ~]# tar -xf httpd-2.4.54.tar.gz 
 [root@198be6cd882f ~]# cd apr-1.6.5
 [root@198be6cd882f apr-1.6.5]# ./configure --prefix=/usr/local/apr
 [root@198be6cd882f apr-1.6.5]# make && make install
 [root@198be6cd882f ~]# cd apr-util-1.6.1
 [root@198be6cd882f apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
 [root@198be6cd882f apr-util-1.6.1]# make && make install
 [root@198be6cd882f ~]# cd httpd-2.4.54
 [root@198be6cd882f httpd-2.4.54]#  ./configure --prefix=/usr/local/apache --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre  --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork && make &&make install

配置文件

 [root@198be6cd882f ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
 [root@198be6cd882f ~]# source /etc/profile.d/apache.sh 
 [root@198be6cd882f ~]# which httpd
 /usr/local/apache/bin/httpd
 [root@198be6cd882f ~]# ln -s /usr/local/apache/include/ /usr/include/apache
 [root@198be6cd882f ~]#  ll /usr/include/|grep apache
 lrwxrwxrwx.  1 root root     26 Aug  9 01:31 apache -> /usr/local/apache/include/
 ​

写service文件

 [root@198be6cd882f ~]# cd /etc/systemd/system/
 [root@198be6cd882f system]# ls
 default.target        getty.target.wants       system-update.target.wants
 default.target.wants  multi-user.target.wants
 [root@198be6cd882f system]# cd /etc/systemd/system
 [root@198be6cd882f system]# vim httpd.service
 [root@198be6cd882f system]# cat httpd.service
 [Unit]
 Description=httpd server daemon
 Documentation=man:httpd(8)
 After=network.target 
 ​
 [Service]
 Type=forking
 ExecStart=/usr/local/apache/bin/apachectl start
 ExecStop=/usr/local/apache/bin/apachectl stop
 #上两个start和stop路径不绝对,倘若安装过程中改变了文件位置可以使用命令找一下find / -name apachectl
 /root/httpd-2.4.54/support/apachectl
 /usr/local/apache/bin/apachectl
 ​
 ExecReload=/bin/kill -HUP $MAINPID
 ​
 [Install]
 WantedBy=multi-user.target
 [root@198be6cd882f system]# 
 ​
 ​
 [root@198be6cd882f ~]# cd /usr/local/apache/
 [root@198be6cd882f apache]# ls
 bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
 [root@198be6cd882f apache]# cd htdocs/
 #这里就是httpd默认显示页面存放路径.可以改index.html中的文件内容.
 [root@198be6cd882f htdocs]# ls
 index.html
 [root@198be6cd882f htdocs]# echo "hhhhhhhh" index.html 
 hhhhhhhh index.html
 [root@198be6cd882f htdocs]# echo "hhhhhhhh" >> index.html 
 [root@198be6cd882f htdocs]# cat index.html 
 <html><body><h1>It works!</h1></body></html>
 hhhhhhhh
 [root@198be6cd882f htdocs]# 
 ​
 ---------------
 #启动服务并设置开机自启
 [root@198be6cd882f ~]# apachectl start
 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
 [root@198be6cd882f ~]# systemctl start httpd
 [root@198be6cd882f ~]# ss -antl
 State      Recv-Q Send-Q   Local Address:Port                  Peer Address:Port              
 LISTEN     0      128                  *:80                               *:*                  
 [root@198be6cd882f ~]# 
 ​
 写一个启动脚本,以便后面启动用
 [root@198be6cd882f bin]# vi /httpd.sh 
 [root@198be6cd882f bin]# cat /httpd.sh 
 #!/bin/bash
 /usr/local/apache/bin/apachectl
 [root@198be6cd882f bin]# chmod +x /httpd.sh 
 [root@198be6cd882f bin]# ll / |grep httpd
 -rwxr-xr-x.   1 root root    44 Aug  9 02:06 httpd.sh
 [root@198be6cd882f bin]# 
 ​

删除多余文件及卸载不用的包

保持docker 的优点,尽量做到减少资源消耗.

 [root@198be6cd882f ~]# ls
 anaconda-ks.cfg  apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz
 apr-1.6.5        apr-util-1.6.1    httpd-2.4.54
 [root@198be6cd882f ~]# rm -rf apr*
 [root@198be6cd882f ~]# rm -rf httpd*
 [root@198be6cd882f ~]# ls
 anaconda-ks.cfg
 [root@198be6cd882f ~]# 
 [root@198be6cd882f ~]# yum  -y remove gcc make wget vim 
 ​

镜像制作

 #在创建镜像的时候,容器不能关闭,必须使其处于允许状态,所以我们必须另起一个终端.
 [root@198be6cd882f bin]# vi /httpd.sh 
 [root@198be6cd882f bin]# cat /httpd.sh 
 #!/bin/bash
 /usr/local/apache/bin/apachectl
 [root@198be6cd882f bin]# chmod +x /httpd.sh 
 [root@198be6cd882f bin]# ll / |grep httpd
 -rwxr-xr-x.   1 root root    44 Aug  9 02:06 httpd.sh
 [root@198be6cd882f bin]# 
 [root@master ~]# docker commit -a '1264218057@qq.com' -m 'Source code installation' -c 'CMD ["/httpd.sh"]' -p httpd httpd:v0.1
 #这里当时写的那个shell脚本就用上了
 sha256:fddaa12cf47117d3ccbcbb2ee63ed8c802b73985b58e85423dbc208c8c7cf269
 [root@master ~]# docker images
 REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
 httpd        v0.1      fddaa12cf471   13 seconds ago   708MB
 mysql        5.7       3147495b3a5c   13 days ago      431MB
 centos       7         eeb6ee3f44bd   10 months ago    204MB
 [root@master ~]# 
 [root@master ~]# docker ps
 CONTAINER ID   IMAGE      COMMAND       CREATED             STATUS             PORTS     NAMES
 198be6cd882f   centos:7   "/bin/bash"   About an hour ago   Up About an hour             httpd
 [root@master ~]# docker run -itd --name httpd1 -p 80:80 httpd:v0.1
 [root@master ~]# docker inspect 
 #这个命令里面找ip
 [root@master ~]# curl 172.17.0.2
 <html><body><h1>It works!</h1></body></html>
 hhhhhhhh

做到这里,一个可以正常使用的httpd就已经完成了。

下面就在基础镜像的基础上进行修改,使其通过主机存储卷里的源码生成web站点。

web站点部署

 [root@master html]# pwd
 /www/html
 [root@master html]# ls
 index.html  static
 [root@master html]# 
 #源码已经在主机上写好了
 --------------------
 开始部署
 ---
 [root@master ~]# docker run -itd --name httpd -p 81:80 -v /www/html/:/www/html tangyuxuan1/httpd:v3
 08e1fbb053e781bda26c34f8e773ad8c5eb4cd362320d9f857ef0eda96200978
 [root@master ~]# 
 # -p用的81端口,待会访问的时候使用ip+81端口访问,-v将主机中的/www/html/这个目录挂载到了容器中使用 镜像就是已经做好了的源码安装httpd。
 ------------------------
 [root@master ~]# docker exec -it httpd /bin/bash
 [root@08e1fbb053e7 /]# ls |grep www
 www
 #成功挂载
 [root@08e1fbb053e7 /]# cd /usr/local/apache/conf/
 [root@08e1fbb053e7 conf]# vi httpd.conf 
 ​
 DocumentRoot "/usr/local/apache/htdocs"
 <Directory "/usr/local/apache/htdocs">
 #这是网站默认显示页面路径,改成挂载过来的/www/html
 DocumentRoot "/www/html"
 <Directory "/www/html"
 [root@08e1fbb053e7 conf]# cd ..
 [root@08e1fbb053e7 apache]# cd bin 
 [root@08e1fbb053e7 bin]# apachectl stop
 --------
 [root@08e1fbb053e7 bin]# apachectl start
 --------
 [root@08e1fbb053e7 bin]#
 ​

访问测试

 

 #访问成功
 制作镜像,并上传
 [root@master ~]# docker commit -a '1264218057@qq.com' -m 'httpd默认文件显示路径为/www/html' httpd tangyuxuan1/httpd:v4
 sha256:f80b3f1a12694d29fd7aef81952f88c5ccb46eeb50f9025a83b67f47daf105ea
 [root@master ~]# docker push tangyuxuan1/httpd:v4 
 ​

 

 #拉下来测试
 [root@master ~]# docker images
 REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
 tangyuxuan1/httpd   v3        02ac9597a898   46 hours ago   708MB
 mysql               5.7       3147495b3a5c   2 weeks ago    431MB
 [root@master ~]# docker pull tangyuxuan1/httpd:v4
 v4: Pulling from tangyuxuan1/httpd
 2d473b07cdd5: Already exists 
 2c6b7c323172: Already exists 
 f1f1401bc681: Already exists 
 6ce997333f07: Already exists 
 4b3e15832082: Pull complete 
 Digest: sha256:112fe2aaf7bc0d83741ff01e56f694f8edfb222d0bfcb190d9ebd2b73ee5f87a
 Status: Downloaded newer image for tangyuxuan1/httpd:v4
 docker.io/tangyuxuan1/httpd:v4
 [root@master ~]# docker images
 REPOSITORY          TAG       IMAGE ID       CREATED         SIZE
 tangyuxuan1/httpd   v4        f80b3f1a1269   9 minutes ago   708MB
 tangyuxuan1/httpd   v3        02ac9597a898   46 hours ago    708MB
 mysql               5.7       3147495b3a5c   2 weeks ago     431MB
 [root@master ~]# 
 ​
 -------------------------
 [root@master ~]# docker ps -a
 CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
 [root@master ~]# docker run -d --name httpd -p 82:80 -v /www/html/:/www/html tangyuxuan1/httpd:v4
 5f40e4c21646e26d1d1c0c8acfbdb4decc1b9555ff85bc1a7b8810a55e5fdd16
 [root@master ~]# 

完成 

之后使用此镜像只需要更改/www/html这个目录中的内容即可

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值