docker apache镜像编写
环境说明: centos8
主机名 | IP地址 | 部署 | 功能 |
---|---|---|---|
root@102 | 192.168.143.102 | docker nfs | 提供web网站 |
root@ node2 | 192.168.143.103 | nfs | 提供nfs共享目录 |
[root@102 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 months ago 231MB
[root@102 ~]# docker run -it --name httpd centos /bin/bash
//导入apache源码包
[root@102 ~]# docker cp /usr/src/apr-1.7.0.tar.gz httpd:/usr/src/
[root@102 ~]# docker cp /usr/src/apr-util-1.6.1.tar.gz httpd:/usr/src/
[root@102 ~]# docker cp /usr/src/httpd-2.4.51.tar.gz httpd:/usr/src/
//创建apache依赖包
[root@d19b25f4165b /]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
//创建apache用户
[root@d19b25f4165b ~]# useradd -r -M -s /sbin/nologin apache
//解压
[root@d19b25f4165b /]# cd /usr/src/
[root@d19b25f4165b src]# ls
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz debug httpd-2.4.48.tar.gz kernels
[root@d19b25f4165b src]# tar xf apr-1.7.0.tar.gz
[root@d19b25f4165b src]# tar xf apr-util-1.6.1.tar.gz
[root@d19b25f4165b src]# tar xf httpd-2.4.48.tar.gz
[root@d19b25f4165b src]# ls
apr-1.7.0 apr-util-1.6.1 debug httpd-2.4.48.tar.gz
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.48 kernels
//编译安装apr
[root@d19b25f4165b src]# cd apr-1.7.0
[root@d19b25f4165b apr-1.7.0]# vi configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#$RM "$cfgfile" \\删除或注释此行
[root@d19b25f4165b apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@d19b25f4165b apr-1.7.0]# make -j && make install
//编译安装apr-util
[root@d19b25f4165b src]# cd ../apr-util-1.6.1
[root@d19b25f4165b apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@d19b25f4165b apr-1.7.0]# make -j && make install
//编译安装httpd
[root@d19b25f4165b apr-1.7.0]# cd ../httpd-2.4.48
[root@d19b25f4165b 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@d19b25f4165b httpd-2.4.48]# make -j && make install
[root@d19b25f4165b ~]# ls /usr/local/
apache apr-util etc include lib64 sbin src
apr bin games lib libexec share
//给htpd配置位置创建软链接
[root@d19b25f4165b ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@d19b25f4165b ~]# vi /usr/local/apache/conf/httpd.conf
[root@d19b25f4165b ~]# /usr/local/apache/bin/
ab checkgid htcacheclean htpasswd logresolve
apachectl dbmmanage htdbm httpd rotatelogs
apxs fcgistarter htdigest httxt2dbm
[root@d19b25f4165b ~]# /usr/local/apache/bin/httpd
[root@d19b25f4165b ~]# ss -atnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
[root@d19b25f4165b ~]# vi /start.sh
#! /bin/sh
/usr/local/apache/bin/httpd
/bin/bash
[root@d19b25f4165b ~]#
//制作成镜像
[root@102 ~]# docker commit -c 'CMD ["/bin/bash","/start.sh"]' -p d19b25f4165b hyhxy0206/httpd:v0.1
sha256:5b1e4b7f179097a63f3f594cefdbfee8127d91cd6160cd95dc51420c882f684b
[root@102 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hyhxy0206/httpd v0.1 5b1e4b7f1790 36 seconds ago 715MB
#创建nfs共享
//另一台主机创建nfs共享存储
[root@node2 ~]# yum install -y nfs-utils
[root@node2 ~]# mkdir /nfs
[root@node2 ~]# vim /etc/exports
/nfs 192.168.143.102(rw)
[root@node2 ~]# systemctl enable --now nfs-server
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
[root@node2 ~]# id nobody
uid=65534(nobody) gid=65534(nobody) 组=65534(nobody)
[root@node2 ~]# setfacl -m u:nobody:rwx /nfs
[root@node2 ~]# getfacl /nfs/
getfacl: Removing leading '/' from absolute path names
# file: nfs/
# owner: root
# group: root
user::rwx
user:nobody:rwx
group::r-x
mask::rwx
other::r-x
//当前主机使用nfs共享存储
[root@102 ~]# yum install -y nfs-utils
[root@102 ~]# mkdir /storage
[root@102 ~]# mount -t nfs 192.168.143.103:/nfs /storage/
[root@102 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.0M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/cl-root 64G 7.7G 57G 13% /
/dev/mapper/cl-home 32G 255M 31G 1% /home
/dev/sr0 9.3G 9.3G 0 100% /mnt
/dev/sda1 1014M 197M 818M 20% /boot
tmpfs 371M 0 371M 0% /run/user/0
192.168.143.103:/nfs 64G 2.4G 62G 4% /storage
//后台运行
[root@102 ~]#docker run -itd --name web -p 80:80 -v /storage:/usr/local/apache/htdocs 5b1e4b7f1790
//提供在/storage html文件
[root@102 ~]# cd /storage/
[root@102 storage]# ls
images index.html scripts sound