docker部署apache服务(使用存储卷)

用一个存储卷部署一个网站

创建容器
[root@localhost ~]#  docker run --name centos -dit centos /bin/bash
5797953ad4494972432350c3ddfc41e3d8cfe0dcb771d3c868550e1f9057b992
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
5797953ad449   centos    "/bin/bash"   7 seconds ago   Up 6 seconds             centos


传输httpd编译所需的资源包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
apr-1.7.0.tar.gz       debug                kernels
apr-util-1.6.1.tar.gz  httpd-2.4.51.tar.gz
[root@localhost src]# docker cp httpd-2.4.49.tar.gz  centos:/usr/src/
[root@localhost src]# docker cp apr-1.7.0.tar.gz centos:/usr/src/
[root@localhost src]# docker cp apr-util-1.6.1.tar.gz centos:/usr/src/

连接容器并下载依赖包
[root@5797953ad449 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@5797953ad449 /]# cd /usr/src/
[root@5797953ad449 src]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  debug  httpd-2.4.49.tar.gz  kernels
[root@5797953ad449 src]# yum groups mark install 'Development Tools'
[root@5797953ad449 src]# yum -y install openssl-devel pcre-devel pcre  expat-devel libtool gcc gcc-c++  make

创建id
[root@5797953ad449 src]# groupadd -r apache
[root@5797953ad449 src]# useradd -r -M -s /sbin/nologin -g apache apache
[root@5797953ad449 src]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)

安装依赖包apr
[root@5797953ad449 src]# tar xf apr-1.7.0.tar.gz 
[root@5797953ad449 src]# tar xf apr-util-1.6.1.tar.gz 
[root@5797953ad449 src]# tar xf httpd-2.4.49.tar.gz   
[root@5797953ad449 src]# ls
apr-1.7.0         apr-util-1.6.1         debug         httpd-2.4.49.tar.gz
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.51  kernels
[root@5797953ad449 src]# cd apr-1.7.0
[root@5797953ad449 apr-1.7.0]# sed -i '/$RM "$cfgfile"/d' configure
[root@5797953ad449 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@5797953ad449 apr-1.7.0]# make && make install

安装依赖包apr-util
[root@5797953ad449 apr-1.7.0]# cd ../apr-util-1.6.1
[root@5797953ad449 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@5797953ad449 apr-util-1.6.1]# make && make install

安装软件包httpd
[root@5797953ad449 apr-util-1.6.1]# cd ../httpd-2.4.49
[root@5797953ad449 apr-util-1.6.1]# ./configure --prefix=/usr/local/apache \
--enable-so \
--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
[root@5797953ad449 httpd-2.4.49]# make && make install

[root@5797953ad449 httpd-2.4.49]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@5797953ad449 httpd-2.4.49]# source /etc/profile.d/httpd.sh
[root@5797953ad449 httpd-2.4.49]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@5797953ad449 httpd-2.4.49]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[root@5797953ad449 httpd-2.4.49]# sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf

编写启动脚本
[root@5797953ad449 httpd-2.4.49]# mkdir /scripts/
[root@5797953ad449 httpd-2.4.49]# cd /scripts/
[root@5797953ad449 scripts]# touch start.sh
[root@5797953ad449 scripts]# chmod +x start.sh 
[root@5797953ad449 scripts]# vi start.sh
[root@5797953ad449 scripts]# cat start.sh 
#!/bin/bash

/usr/local/apache/bin/apachectl start
/bin/bash

制作镜像
[root@localhost src]# docker commit -a 'csl <yibie>' -c 'CMD ["/scripts/start.sh"]'   -p  centos   httpd:v1.0
sha256:3d403d117443ab5d8d7fdd6ff84befe8e2391c61bf143eb817d345f4df24635f
在另一台主机配置nfs存储
安装nfs
[root@nfs ~]# yum -y install nfs-server
[root@nfs ~]#  systemctl enable --now nfs-server.service
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
创建共享目录
[root@nfs ~]# mkdir /nfs
[root@nfs ~]# chmod 777 /nfs/
[root@nfs ~]# vim /etc/exports
/nfs   192.168.57.139(rw)
[root@nfs ~]# systemctl restart nfs-server.service

在docker主机上配置nfs

安装nfs
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# systemctl enable --now nfs-server.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.

查看共享
[root@localhost ~]# showmount -e 192.168.230.132
Export list for 192.168.57.139:
/nfs 192.168.57.140

永久挂载
[root@localhost ~]# vim /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Oct 11 04:00:01 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=c8ac0ee8-e337-4e19-bad8-2fda61bb24b2 /boot                   xfs     defaults        0 0
/dev/mapper/rhel-home   /home                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
192.168.57.140:/nfs   /var/www/html            nfs     defaults,_netdev 0 0
[root@localhost ~]# mount -a

测试nfs
在docker主机共享目录上创建文件
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# 
[root@localhost html]# touch 123
[root@localhost html]# ls
123

nfs主机查看
[root@nfs ~]# ls /nfs/
123

根据新镜像启动一个容器
[root@localhost src]# docker run --name httpd -dit -p 80:80 -v /var/www/html:/usr/local/apache/htdocs  httpd:v1.0
c3527c62ca05f21af4ed289263feaef7e3922cb7abfc5e8b11cf57ddc779d2de
[root@localhost src]# docker ps
CONTAINER ID   IMAGE        COMMAND               CREATED          STATUS          PORTS                               NAMES
c3527c62ca05   httpd:v1.0   "/scripts/start.sh"   24 seconds ago   Up 22 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   httpd
[root@localhost src]# ss -anlt
State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:80           0.0.0.0:*              
LISTEN  0       128            0.0.0.0:22           0.0.0.0:*              
LISTEN  0       128               [::]:80              [::]:*              
LISTEN  0       128               [::]:22              [::]:*              
[root@localhost src]# docker exec -it httpd /bin/bash
[root@3037202498fe /]# cd /usr/local/apache/htdocs/
[root@3037202498fe htdocs]# ls

上传网页文件
[root@localhost html]# ls
'(15条消息) 部署个网站_早川语的博客-CSDN博客.html'
[root@c3527c62ca05 htdocs]# ls /usr/local/apache/htdocs/
'(15条消息) 部署个网站_早川语的博客-CSDN博客.html'

docker部署apache服务(使用存储卷)

docker部署apache服务(使用存储卷)

 

上一篇:Dockerfile


下一篇:docker存储卷