用一个存储卷部署一个网站
创建容器
[root@localhost ~]# docker run --name centos -dit centos /bin/bash
ea929a61e38b1547de2ab5929b93b36b1ef5a1275a37897b7539badf709cd548
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea929a61e38b centos "/bin/bash" 6 seconds ago Up 5 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.51.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@localhost ~]# docker exec -it centos /bin/bash
[root@ea929a61e38b /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@ea929a61e38b /]# cd /usr/src/
[root@ea929a61e38b src]# ls
apr-1.7.0.tar.gz debug kernels
apr-util-1.6.1.tar.gz httpd-2.4.51.tar.gz
[root@ea929a61e38b src]# yum groups mark install 'Development Tools'
[root@ea929a61e38b src]# yum -y install openssl-devel pcre-devel pcre expat-devel libtool gcc gcc-c++ make
创建id
[root@ea929a61e38b src]# groupadd -r apache
[root@ea929a61e38b src]# useradd -r -M -s /sbin/nologin -g apache apache
[root@ea929a61e38b src]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
安装依赖包apr
[root@ea929a61e38b src]# tar xf apr-1.7.0.tar.gz
[root@ea929a61e38b src]# tar xf apr-util-1.6.1.tar.gz
[root@ea929a61e38b src]# tar xf httpd-2.4.51.tar.gz
[root@ea929a61e38b src]# ls
apr-1.7.0 apr-util-1.6.1 debug httpd-2.4.51.tar.gz
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.51 kernels
[root@ea929a61e38b src]# cd apr-1.7.0
[root@ea929a61e38b apr-1.7.0]# sed -i '/$RM "$cfgfile"/d' configure
[root@ea929a61e38b apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@ea929a61e38b apr-1.7.0]# make && make install
安装依赖包apr-util
[root@ea929a61e38b apr-1.7.0]# cd ../apr-util-1.6.1
[root@ea929a61e38b apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@ea929a61e38b apr-util-1.6.1]# make && make install
安装软件包httpd
[root@ea929a61e38b apr-util-1.6.1]# cd ../httpd-2.4.51
[root@ea929a61e38b 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@ea929a61e38b httpd-2.4.51]# make && make install
[root@ea929a61e38b httpd-2.4.51]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@ea929a61e38b httpd-2.4.51]# source /etc/profile.d/httpd.sh
[root@ea929a61e38b httpd-2.4.51]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@ea929a61e38b httpd-2.4.51]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[root@ea929a61e38b httpd-2.4.51]# sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
编写启动脚本
[root@ea929a61e38b httpd-2.4.51]# mkdir /scripts/
[root@ea929a61e38b httpd-2.4.51]# cd /scripts/
[root@ea929a61e38b scripts]# touch start.sh
[root@ea929a61e38b scripts]# chmod +x start.sh
[root@ea929a61e38b scripts]# vi start.sh
[root@ea929a61e38b scripts]# cat start.sh
#!/bin/bash
/usr/local/apache/bin/apachectl start
/bin/bash
制作镜像
[root@localhost src]# docker commit -a 'csl <1163582076>' -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.230.143(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.230.132:
/nfs 192.168.230.143
永久挂载
[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.153.161:/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@c3527c62ca05 /]# cd /usr/local/apache/htdocs/
[root@c3527c62ca05 htdocs]# ls
上传网页文件
[root@localhost html]# ls
2021人气网页游戏大全.mhtml
[root@c3527c62ca05 htdocs]# ls /usr/local/apache/htdocs/
2021人气网页游戏大全.mhtml
效果图