//准备基础环境docker
[root@lch ~]# cd /etc/yum.repos.d/
[root@lch yum.repos.d]# ls
CentOS-Base.repo epel-modular.repo epel-testing-modular.repo
docker-ce.repo epel.repo epel-testing.repo
[root@lch yum.repos.d]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2022-04-26 13:05:58 CST; 1h 46min ago
Docs: https://docs.docker.com
Main PID: 16907 (dockerd)
Tasks: 9
Memory: 299.1M
CGroup: /system.slice/docker.service
└─16907 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
//拉取centos镜像
[root@lch ~]# docker pull centos:8
8: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:8
docker.io/library/centos:8
[root@lch ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 8 5d0da3dc9764 7 months ago 231MB
//利用centos8生成容器
[root@lch ~]# docker run -it --name httpd centos:8 /bin/bash
[root@d1040fce381b /]#
//配置一下阿里云的yum源
[root@d1040fce381b /]# cd /etc/yum.repos.d/
[root@d1040fce381b yum.repos.d]# ls
CentOS-Linux-AppStream.repo CentOS-Linux-FastTrack.repo
CentOS-Linux-BaseOS.repo CentOS-Linux-HighAvailability.repo
CentOS-Linux-ContinuousRelease.repo CentOS-Linux-Media.repo
CentOS-Linux-Debuginfo.repo CentOS-Linux-Plus.repo
CentOS-Linux-Devel.repo CentOS-Linux-PowerTools.repo
CentOS-Linux-Extras.repo CentOS-Linux-Sources.repo
[root@d1040fce381b yum.repos.d]# rm -rf *
[root@d1040fce381b yum.repos.d]# ls
[root@d1040fce381b yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@d1040fce381b yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@d1040fce381b yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@d1040fce381b yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@d1040fce381b yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@d1040fce381b yum.repos.d]# yum clean all
[root@d1040fce381b yum.repos.d]# yum makecache
在容器里编译安装apache
//下载apache安装包
[root@lch ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@lch ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@lch ~]# wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz
//将安装包上传到httpd容器里
[root@lch ~]# docker cp httpd-2.4.53.tar.gz httpd:/usr/src/
[root@lch ~]# docker cp apr-util-1.6.1.tar.gz httpd:/usr/src/
[root@lch ~]# docker cp apr-1.7.0.tar.gz httpd:/usr/src/
[root@d1040fce381b ~]# ls /usr/src/
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz debug httpd-2.4.53.tar.gz kernels
//准备apache的基础环境
[root@d1040fce381b ~]# yum groups mark install 'Development Tools' -y
[root@d1040fce381b ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
[root@d1040fce381b ~]# useradd -r -M -s /sbin/nologin apache
//编译安装apache
[root@d1040fce381b ~]# cd /usr/src/
[root@d1040fce381b src]# tar -xf apr-1.7.0.tar.gz
[root@d1040fce381b src]# cd apr-1.7.0
[root@d1040fce381b apr-1.7.0]# vi configure
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
$RM "$cfgfile" //将此行加上注释,或者删除此行
[root@d1040fce381b apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@d1040fce381b apr-1.7.0]# make
[root@d1040fce381b apr-1.7.0]# make install
[root@d1040fce381b apr-1.7.0]# cd ..
[root@d1040fce381b src]# tar -xf apr-util-1.6.1.tar.gz
[root@d1040fce381b src]# cd apr-util-1.6.1
[root@d1040fce381b apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@d1040fce381b apr-util-1.6.1]# make
[root@d1040fce381b apr-util-1.6.1]# make install
[root@d1040fce381b apr-util-1.6.1]# cd ..
[root@d1040fce381b src]# tar -xf httpd-2.4.53.tar.gz
[root@d1040fce381b src]# cd httpd-2.4.53
[root@d1040fce381b httpd-2.4.53]# ./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=worker
[root@d1040fce381b httpd-2.4.53]# make
[root@d1040fce381b httpd-2.4.53]# make install
//设置环境变量 做映射关系 头文件 man文件
[root@d1040fce381b httpd-2.4.53]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@d1040fce381b httpd-2.4.53]# source /etc/profile.d/apache.sh
[root@d1040fce381b httpd-2.4.53]# yum -y install which
[root@d1040fce381b httpd-2.4.53]# which httpd
/usr/local/apache/bin/httpd
[root@d1040fce381b httpd-2.4.53]# ln -s /usr/local/apache/include/ /usr/include/apache
[root@lch ~]# cd /usr/local/apache/conf/
[root@lch conf]# vi httpd.conf
#ServerName www.example.com:80 // 此行取消注释
[root@d1040fce381b ~]# apachectl start
[root@d1040fce381b ~]# ss -antl
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@d1040fce381b ~]# apachectl start
[root@d1040fce381b ~]# ss -antl
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@d1040fce381b ~]# /usr/local/apache/bin/httpd -DFOREGROUND
制作httpd镜像
[root@lch ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1040fce381b centos:8 "/bin/bash" 54 minutes ago Up 50 minutes httpd
[root@lch ~]# docker commit -a 'lch <1@2.com>' -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND",]' -p httpd wjmz2/httpd:v1.0
[root@lch ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wjmz2/httpd v1.0 95d3c3109438 39 seconds ago 749MB
centos 8 5d0da3dc9764 7 months ago 231MB
使用此镜像生成容器进行验证
[root@lch ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1040fce381b centos:8 "/bin/bash" 58 minutes ago Up 54 minutes httpd
[root@lch ~]# docker stop httpd
httpd
[root@lch ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@lch ~]# docker run -d -p 80:80 --name web wjmz2/httpd:v1.0
d72748777112a65378dba45888f2460c5d38f3718a620702bd336e88e40553cf
[root@lch ~]# docker exec -it web /bin/bash
[root@lch ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
779973e9810f wjmz2/httpd:v1.0 "/usr/local/apache/b…" 51 minutes ago Up 2 seconds minutes ago web6
eaa607c9f823 centos:8 "/bin/bash" 3 hours ago Up 45 minutes httpd