使用上存储卷,部署一个网站

使用上存储卷,部署一个网站

拉取一个镜像

[root@docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

创建容器

[root@docker ~]# docker run  --name  centos  -dit centos /bin/bash
37b7df70cd8917644e772ee33dc3593b00bf4c3ccf2a51637e119e6c7a7599a1
[root@docker ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
37b7df70cd89   centos    "/bin/bash"   5 seconds ago   Up 5 seconds             centos

再另开一个终端上传httpd、apr、apr-util资源包

[root@docker ~]# cd /usr/src/
[root@docker src]# ls
apr-1.7.0.tar.gz       debug                kernels
apr-util-1.6.1.tar.gz  httpd-2.4.48.tar.gz
[root@docker src]# docker cp httpd-2.4.48.tar.gz  centos:/usr/src/
[root@docker src]# docker cp apr-util-1.6.1.tar.gz  centos:/usr/src/
[root@docker src]# docker cp apr-1.7.0.tar.gz  centos:/usr/src/

运行容器

[root@docker ~]# docker exec  -it centos  /bin/bash
[root@37b7df70cd89 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@37b7df70cd89 /]# cd /usr/src/
[root@37b7df70cd89 src]# ls
apr-1.7.0.tar.gz       debug                kernels
apr-util-1.6.1.tar.gz  httpd-2.4.48.tar.gz

下载依赖包

[root@37b7df70cd89 ~]# yum groups mark install 'Development Tools'
[root@37b7df70cd89 ~]# yum -y install openssl-devel pcre-devel pcre  expat-devel libtool gcc gcc-c++  make

创建角色

[root@37b7df70cd89 ~]# groupadd -r apache
[root@37b7df70cd89 ~]# useradd -r -M -s /sbin/nologin -g apache apache 
[root@37b7df70cd89 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)

解压&编译安装apr、apr-util、httpd

[root@37b7df70cd89 ~]# cd /usr/src/   
[root@37b7df70cd89 src]# ls
apr-1.7.0.tar.gz       debug                kernels
apr-util-1.6.1.tar.gz  httpd-2.4.48.tar.gz
[root@37b7df70cd89 src]# tar -xf httpd-2.4.48.tar.gz 
[root@37b7df70cd89 src]# tar -xf apr-1.7.0.tar.gz    
[root@37b7df70cd89 src]# tar -xf apr-util-1.6.1.tar.gz 
[root@37b7df70cd89 src]# ls
apr-1.7.0         apr-util-1.6.1.tar.gz  httpd-2.4.48.tar.gz
apr-1.7.0.tar.gz  debug                  kernels
apr-util-1.6.1    httpd-2.4.48
[root@37b7df70cd89 src]# cd apr-1.7.0
[root@37b7df70cd89 apr-1.7.0]# ls
apr-config.in     CMakeLists.txt  libapr.mak     poll
apr.dep           config.layout   libapr.rc      random
apr.dsp           configure       LICENSE        README
apr.dsw           configure.in    locks          README.cmake
apr.mak           docs            Makefile.in    shmem
apr.pc.in         dso             Makefile.win   strings
apr.spec          emacs-mode      memory         support
atomic            encoding        misc           tables
build             file_io         mmap           test
build.conf        helpers         network_io     threadproc
buildconf         include         NOTICE         time
build-outputs.mk  libapr.dep      NWGNUmakefile  tools
CHANGES           libapr.dsp      passwd         user
[root@37b7df70cd89 apr-1.7.0]# sed -i '/$RM "$cfgfile"/d' configure
[root@37b7df70cd89 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@37b7df70cd89 apr-1.7.0]# make && make install

[root@37b7df70cd89 apr-1.7.0]# cd ../apr-util-1.6.1
[root@37b7df70cd89 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@37b7df70cd89 apr-util-1.6.1]# make && make install

[root@37b7df70cd89 apr-util-1.6.1]# cd ../httpd-2.4.48
[root@37b7df70cd89 httpd-2.4.48]# ./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@37b7df70cd89 httpd-2.4.48]# make && make install

配置环境变量&软连接

[root@37b7df70cd89 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@37b7df70cd89 ~]# source /etc/profile.d/httpd.sh

[root@37b7df70cd89 ~]# ln -s /usr/local/apache/include/ /usr/include/httpd

修改配置文件

[root@37b7df70cd89 ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[root@37b7df70cd89 ~]# sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf

编写启动脚本

[root@37b7df70cd89 ~]# mkdir /scripts/
[root@37b7df70cd89 ~]# cd /scripts/
[root@37b7df70cd89 scripts]# ls
[root@37b7df70cd89 scripts]# touch start.sh
[root@37b7df70cd89 scripts]# chmod +x start.sh
[root@37b7df70cd89 scripts]# vi start.sh
[root@37b7df70cd89 scripts]# cat start.sh 
#!/bin/bash
/usr/local/apache/bin/apachectl start
/bin/bash

制作镜像

[root@docker ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
37b7df70cd89   centos    "/bin/bash"   23 minutes ago   Up 23 minutes             centos
[root@docker ~]# docker commit -a 'abc <12345678@qq.com>' -c 'CMD ["/scripts/start.sh"]' -p centos httpd:v1.0
sha256:b6feea4ad39e4ff8502e79bde478e1d0987941ef224f5dc56ac1824163e63709

再另启动一台主机配置nfs

安装nfs

[root@nfs ~]# yum -y install nfs-utils
[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.47.161(rw)
[root@nfs ~]# systemctl restart nfs-server

在docker主机上配置nfs

安装nfs

[root@nfs ~]# yum -y install nfs-utils
[root@nfs ~]# systemctl enable --now nfs-server.service 

查看共享

[root@localhost ~]# showmount -e 192.168.47.160
Export list for 192.168.47.160:
/nfs 192.168.47.161

临时挂载

[root@localhost ~]# mount -t nfs 192.168.47.160:/nfs /var/www/html/
[root@localhost ~]# df -h 
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   50G  3.1G   47G    7% /
devtmpfs                 899M     0  899M    0% /dev
tmpfs                    911M     0  911M    0% /dev/shm
tmpfs                    911M  9.8M  902M    2% /run
tmpfs                    911M     0  911M    0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M   14% /boot
/dev/mapper/centos-home   47G   33M   47G    1% /home
tmpfs                    183M     0  183M    0% /run/user/0
overlay                   50G  3.1G   47G    7% /var/lib/docker/overlay2/564ad7aebbbebfc2fb8a7c3e1cef860c16dc183d57dca6705e97d08c7c7514c7/merged
192.168.47.160:/nfs       66G  2.1G   63G    4% /var/www/html

永久挂载

[root@localhost ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Oct 21 14:29:38 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=5e14e536-8ace-429b-a723-c7fcb6520fbb /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
192.168.47.160:/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]# touch 123
[root@localhost html]# ls
123

nfs主机查看

[root@nfs ~]# ls /nfs/
123

用新镜像启动一个容器


上一篇:docker源码部署httpd—存储卷部署网站


下一篇:7. JavaBean + 【在JSP中使用JavaBean】:<jsp:useBean>、<jsp:setProperty>、<jsp:getProperty>