准备工作
//下载apache源码包
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz
//拉取镜像
[root@localhost ~]# docker pull centos
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest beae173ccac6 3 months ago 1.24MB
httpd latest dabbfbe0c57b 4 months ago 144MB
centos latest 5d0da3dc9764 7 months ago 231MB
//创建容器基于centos镜像,在终端1上操作
[root@localhost ~]# docker run -it --name c1 centos /bin/bash
[root@aac9feaa6e20 /]# ls
bin home lost+found opt run sys var
dev lib media proc sbin tmp
etc lib64 mnt root srv usr
[root@aac9feaa6e20 /]# alias ls='ls --color' //显示颜色
[root@aac9feaa6e20 /]# ls
bin home lost+found opt run sys var
dev lib media proc sbin tmp
etc lib64 mnt root srv usr
[root@aac9feaa6e20 /]# cd /usr/src
[root@aac9feaa6e20 src]# ls
debug kernels
//另起一个终端2,在终端2上执行操作
[root@localhost ~]# ls
anaconda-ks.cfg apr-util-1.6.1.tar.gz
apr-1.7.0.tar.gz httpd-2.4.53.tar.gz
[root@localhost ~]# mkdir software //创建目录
[root@localhost ~]# mv *.gz software/ //将apache的源码包移动复制到目录里
[root@localhost ~]# ls
anaconda-ks.cfg software
[root@localhost ~]# docker cp software c1:/usr/src/ //将此目录复制到容器c1的/usr/src
//在终端1上操作
[root@aac9feaa6e20 src]# ls
debug kernels software
[root@aac9feaa6e20 src]# ls software/
apr-1.7.0.tar.gz httpd-2.4.53.tar.gz
apr-util-1.6.1.tar.gz
//配置yum源
[root@aac9feaa6e20 src]# cd /etc/yum.repos.d/
[root@aac9feaa6e20 yum.repos.d]# ls
CentOS-Linux-AppStream.repo
CentOS-Linux-BaseOS.repo
CentOS-Linux-ContinuousRelease.repo
CentOS-Linux-Debuginfo.repo
CentOS-Linux-Devel.repo
CentOS-Linux-Extras.repo
CentOS-Linux-FastTrack.repo
CentOS-Linux-HighAvailability.repo
CentOS-Linux-Media.repo
CentOS-Linux-Plus.repo
CentOS-Linux-PowerTools.repo
CentOS-Linux-Sources.repo
[root@aac9feaa6e20 yum.repos.d]# rm -rf *
[root@aac9feaa6e20 yum.repos.d]# ls
[root@aac9feaa6e20 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@aac9feaa6e20 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@aac9feaa6e20 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@aac9feaa6e20 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@aac9feaa6e20 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@aac9feaa6e20 yum.repos.d]# yum clean all
Failed to set locale, defaulting to C.UTF-8
18 files removed
[root@aac9feaa6e20 yum.repos.d]# yum makecache
基于容器编译apache
//安装开发环境
[root@aac9feaa6e20 ~]# yum -y install wget make
[root@aac9feaa6e20 ~]# yum groups mark install "Development Tools"
[root@aac9feaa6e20 ~]# rpm -qa | grep gcc
libgcc-8.4.1-1.el8.x86_64
[root@aac9feaa6e20 ~]# useradd -r -M -s /sbin/nologin apache
[root@aac9feaa6e20 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
[root@aac9feaa6e20 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
//解压
[root@aac9feaa6e20 software]# tar xf apr-1.7.0.tar.gz
[root@aac9feaa6e20 software]# tar xf apr-util-1.6.1.tar.gz
[root@aac9feaa6e20 software]# tar xf httpd-2.4.53.tar.gz
//编译apr-1.7.0
[root@aac9feaa6e20 ~]# cd /usr/src/software/apr-1.7.0
[root@aac9feaa6e20 apr-1.7.0]# ls
apr-config.in dso NOTICE
apr.dep emacs-mode NWGNUmakefile
apr.dsp encoding passwd
apr.dsw file_io poll
apr.mak helpers random
apr.pc.in include README
apr.spec libapr.dep README.cmake
atomic libapr.dsp shmem
build libapr.mak strings
build.conf libapr.rc support
buildconf LICENSE tables
build-outputs.mk locks test
CHANGES Makefile.in threadproc
CMakeLists.txt Makefile.win time
config.layout memory tools
configure misc user
configure.in mmap
docs network_io
[root@aac9feaa6e20 apr-1.7.0]# vim configure
$RM "$cfgfile" //把这个删掉
[root@aac9feaa6e20 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@aac9feaa6e20 apr-1.7.0]# make
[root@aac9feaa6e20 apr-1.7.0]# make install
//编译apr-util-1.6.1
[root@aac9feaa6e20 apr-1.7.0]# cd ../apr-util-1.6.1
[root@aac9feaa6e20 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@aac9feaa6e20 apr-util-1.6.1]# make
[root@aac9feaa6e20 apr-util-1.6.1]# make install
//编译httpd-2.4.53
[root@aac9feaa6e20 apr-util-1.6.1]# cd ../httpd-2.4.53
[root@aac9feaa6e20 httpd-2.4.53]# ls
ABOUT_APACHE docs modules
acinclude.m4 emacs-style NOTICE
Apache-apr2.dsw httpd.dep NWGNUmakefile
Apache.dsw httpd.dsp os
apache_probes.d httpd.mak README
ap.d httpd.spec README.CHANGES
build include README.cmake
BuildAll.dsp INSTALL README.platforms
BuildBin.dsp InstallBin.dsp ROADMAP
buildconf LAYOUT server
CHANGES libhttpd.dep srclib
changes-entries libhttpd.dsp support
CMakeLists.txt libhttpd.mak test
config.layout LICENSE VERSIONING
configure Makefile.in
configure.in Makefile.win
[root@aac9feaa6e20 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=prefork
[root@aac9feaa6e20 httpd-2.4.53]# make
[root@aac9feaa6e20 httpd-2.4.53]# make install
//要使apache在前台运行则需要写脚本
[root@aac9feaa6e20 ~]# cd /
[root@aac9feaa6e20 /]# ls
bin home lost+found opt run sys var
dev lib media proc sbin tmp
etc lib64 mnt root srv usr
[root@aac9feaa6e20 /]# vi entrypoint.sh
[root@aac9feaa6e20 /]# chmod +x entrypoint.sh //给执行权限
[root@aac9feaa6e20 /]# ls
bin etc lib64 mnt root srv usr
dev home lost+found opt run sys var
entrypoint.sh lib media proc sbin tmp
[root@aac9feaa6e20 /]# cat entrypoint.sh
#!/bin/bash
/usr/local/apache/bin/httpd && sleep 5d
[root@aac9feaa6e20 /]# ./entrypoint.sh
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[root@aac9feaa6e20 /]# 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:*
//在终端2试着访问
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aac9feaa6e20 centos "/bin/bash" 36 minutes ago Up 36 minutes c1
[root@localhost ~]# docker inspect c1
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
[root@localhost ~]# curl 172.17.0.2 //访问成功,表示编译成功了
It works!
基于容器生成镜像
//制作镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd v0.1 2aea1e2b975c 6 minutes ago 729MB
busybox latest beae173ccac6 3 months ago 1.24MB
httpd latest dabbfbe0c57b 4 months ago 144MB
centos latest 5d0da3dc9764 7 months ago 231MB
[root@localhost ~]# docker commit -a 'sean <[email protected]>' -c 'CMD ["/entrypoint.sh"]' -p c1 httpd:v0.2 //制作镜像
sha256:fe36a36e4c2f8b45757d88fe054d12f7fe3df9b05f6e1880107d21e0887be090
[root@localhost ~]# docker run -d --name web -p 80:80 httpd:v0.2 //基于这个镜像创建一个容器来看是否成功
7f2510c27750ecc7aedf2c0f96513e9d32e2040ae4c52f6207ed88e54fe2ba49
[root@localhost ~]# docker ps //查看有启动,表示成功
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f2510c27750 httpd:v0.2 "/entrypoint.sh" 9 seconds ago Up 8 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
aac9feaa6e20 centos "/bin/bash" 52 minutes ago Up 52 minutes c1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd v0.2 fe36a36e4c2f 2 minutes ago 729MB
httpd v0.1 2aea1e2b975c 9 minutes ago 729MB
busybox latest beae173ccac6 3 months ago 1.24MB
httpd latest dabbfbe0c57b 4 months ago 144MB
centos latest 5d0da3dc9764 7 months ago 231MB
使用IP地址访问
如果程序本身可以在前台运行,就不用写脚本,直接让在程序里启动
//制作镜像
[root@localhost ~]# docker commit -a 'sean <[email protected]>' -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' -p c1 httpd:v0.4
[root@localhost ~]# docker run -d --name web1 -p 80:80 httpd:v0.4 //如果报错显示端口号被占
则把占用端口的那个删掉就行
[root@localhost ~]# docker rm -f web
//如果前面创建容器报错了端口被占,后面删除了占端口的web,run命令的创建成功了,启动没有成功,所以需要启动
[root@localhost ~]# docker start web1