基于容器编译安装apache制作为镜像
镜像的概念
镜像可以理解为应用程序的集装箱,而docker用来装卸集装箱。
docker镜像含有启动容器所需要的文件系统及其内容,因此,其用于创建并启动容器。
docker镜像采用分层构建机制,最底层为bootfs,其上为rootfs
- bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以节约内存资源
- rootfs:位于bootfs之上,表现为docker容器的根文件系统
- 传统模式中,系统启动之时,内核挂载rootfs会首先将其挂载为“只读”模式,完整性自检完成后将其重新挂载为读写模式
- docker中,rootfs由内核挂载为“只读”模式,而后通过“联合挂载”技术额外挂载一个“可写”层
docker镜像的制作
多数情况下,我们做镜像是基于别人已存在的某个基础镜像来实现的,我们把它称为base image。比如一个纯净版的最小化的centos、ubuntu或debian。
那么这个最小化的centos镜像从何而来呢?其实这个基础镜像一般是由Docker Hub的相关维护人员,也就是Docker官方手动制作的。这个基础镜像的制作对于Docker官方的专业人员来说是非常容易的,但对于终端用户来说就不是那么容易制作的了。
基于容器制作镜像
//下载新的 CentOS-Base.repo 到 /etc/yum.repos.d///查看是否有centos镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
centos latest 5d0da3dc9764 10 months ago 231MB
//进入容器
[root@localhost ~]# docker run -it --rm centos
[root@799da60f020d /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@799da60f020d /]# cd /etc/yum.repos.d/
[root@799da60f020d 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@799da60f020d yum.repos.d]# rm -rf *
[root@799da60f020d yum.repos.d]# ls
//下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/
[root@799da60f020d yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --100 2495 100 2495 0 0 9105 0 --:--:-- --:--:-- --:--:-- 9105
[root@799da60f020d yum.repos.d]# ls
CentOS-Base.repo
[root@799da60f020d yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//下载新repo 到/etc/yum.repos.d/
root@799da60f020d yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm //安装 epel 配置包
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors. 282 kB/s | 4.6 MB 00:16
CentOS-8.5.2111 - Extras - mirror 57 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - mir 191 kB/s | 8.4 MB 00:45
epel-release-latest-8.noarch.rpm 107 kB/s | 24 kB 00:00
Dependencies resolved.
==================================================================
Package Arch Version Repository Size
==================================================================
Installing:
epel-release noarch 8-16.el8 @commandline 24 k
Transaction Summary
==================================================================
Install 1 Package
Total size: 24 k
Installed size: 34 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : epel-release-8-16.el8.noarch 1/1
Running scriptlet: epel-release-8-16.el8.noarch 1/1
Many EPEL packages require the CodeReady Builder (CRB) repository.
It is recommended that you run /usr/bin/crb enable to enable the CRB repository.
Verifying : epel-release-8-16.el8.noarch 1/1
Installed:
epel-release-8-16.el8.noarch
Complete!
//将 repo 配置中的地址替换为阿里云镜像站地址
[root@799da60f020d yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@799da60f020d yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
//清理缓存
[root@799da60f020d ~]# dnf clean all
Failed to set locale, defaulting to C.UTF-8
18 files removed
[root@799da60f020d ~]# dnf makecache
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors. 588 kB/s | 4.6 MB 00:07
CentOS-8.5.2111 - Extras - mirror 43 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - mir 402 kB/s | 8.4 MB 00:21
Extra Packages for Enterprise Lin 306 kB/s | 341 kB 00:01
Extra Packages for Enterprise Lin 400 kB/s | 13 MB 00:32
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Metadata cache created.
//安装开发工具包
[root@799da60f020d ~]# yum groups mark install 'Development Tools'
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:01:11 ago on Tue Aug 9 01:04:48 2022.
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Dependencies resolved.
==================================================================
Package Architecture Version Repository Size
==================================================================
Installing Groups:
Development Tools
Transaction Summary
==================================================================
Is this ok [y/N]: y
Complete!
//创建apache服务的用户和组
[root@799da60f020d ~]# useradd -r -M -s /sbin/nologin apache
//安装依赖包
[root@799da60f020d ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make vim wget
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:03:01 ago on Tue Aug 9 01:04:48 2022.
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
.....................................
vim-common-2:8.0.1763-16.el8.x86_64
vim-enhanced-2:8.0.1763-16.el8.x86_64
vim-filesystem-2:8.0.1763-16.el8.noarch
wget-1.19.5-10.el8.x86_64
which-2.21-16.el8.x86_64
zlib-devel-1.2.11-17.el8.x86_64
Complete!
//下载和安装apr以及apr-util
[root@799da60f020d ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
--2022-08-09 01:12:43-- https://downloads.apache.org/apr/apr-1.7.0.tar.gz
Resolving downloads.apache.org (downloads.apache.org)... 135.181.214.104, 88.99.95.219, 2a01:4f9:3a:2c57::2, ...
Connecting to downloads.apache.org (downloads.apache.org)|135.181.214.104|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1093896 (1.0M) [application/x-gzip]
Saving to: ‘apr-1.7.0.tar.gz’
apr-1.7.0.tar.gz 100%[========>] 1.04M 70.2KB/s in 12s
2022-08-09 01:12:56 (86.8 KB/s) - ‘apr-1.7.0.tar.gz’ saved [1093896/1093896]
--2022-08-09 01:12:56-- https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
Reusing existing connection to downloads.apache.org:443.
HTTP request sent, awaiting response... 200 OK
Length: 554301 (541K) [application/x-gzip]
Saving to: ‘apr-util-1.6.1.tar.gz’
apr-util-1.6.1.t 100%[========>] 541.31K 31.8KB/s in 17s
2022-08-09 01:13:14 (31.2 KB/s) - ‘apr-util-1.6.1.tar.gz’ saved [554301/554301]
FINISHED --2022-08-09 01:13:14--
Total wall clock time: 31s
Downloaded: 2 files, 1.6M in 30s (54.3 KB/s)
[root@799da60f020d ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.gz original-ks.cfg
anaconda-post.log apr-util-1.6.1.tar.gz
[root@799da60f020d ~]# tar xf apr-1.7.0.tar.gz
[root@799da60f020d ~]# tar xf apr-util-1.6.1.tar.gz [root@799da60f020d ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.gz original-ks.cfg
anaconda-post.log apr-util-1.6.1
apr-1.7.0 apr-util-1.6.1.tar.gz
[root@799da60f020d ~]# cd apr-1.7.0
[root@799da60f020d 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@799da60f020d apr-1.7.0]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //注释此行
[root@799da60f020d apr-1.7.0]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.7.0
checking for chosen layout... apr
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
................................................
[root@799da60f020d apr-1.7.0]# make && make install
make[1]: Entering directory '/root/apr-1.7.0'
/bin/sh /root/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/root/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/root/apr-1.7.0/include/arch/unix -I/root/apr-1.7.0/include -I/root/apr-1.7.0/include/private -I/root/apr-1.7.0/include/private -o encoding/apr_encode.lo -c encoding/apr_encode.c && touch encoding/apr_encode.lo
/root/apr-1.7.0/build/mkdir.sh tools
/bin/sh /root/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/root/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/root/apr-1.7.0/include/arch/unix -I/root/apr-1.7.0/include -I/root/apr-1.7.0/include/private -I/root/apr-1.7.0/include/private -o tools/gen_test_char.lo -c tools/gen_test_char.c && touch tools/gen_test_char.lo
............................................
[root@799da60f020d apr-1.7.0]# cd ../apr-util-1.6.1
[root@799da60f020d apr-util-1.6.1]# ls
aprutil.dep configure.in Makefile.win
aprutil.dsp crypto memcache
aprutil.dsw dbd misc
aprutil.mak dbm NOTICE
apr-util.pc.in docs NWGNUmakefile
apr-util.spec encoding README
apu-config.in export_vars.sh.in README.cmake
buckets hooks README.FREETDS
build include redis
build.conf ldap renames_pending
buildconf libaprutil.dep strmatch
build-outputs.mk libaprutil.dsp test
CHANGES libaprutil.mak uri
CMakeLists.txt libaprutil.rc xlate
config.layout LICENSE xml
configure Makefile.in
[root@799da60f020d apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking for working mkdir -p... yes
APR-util Version: 1.6.1
checking for chosen layout... apr-util
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
......................................
include/private/apu_select_dbm.h
config.status: creating include/apr_ldap.h
config.status: creating include/apu.h
config.status: creating include/apu_want.h
config.status: creating test/Makefile
config.status: creating include/private/apu_config.h
config.status: executing default commands
[root@799da60f020d apr-util-1.6.1]# make && make install
make[1]: Entering directory '/root/apr-util-1.6.1'
/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/root/apr-util-1.6.1/include -I/root/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_brigade.lo -c buckets/apr_brigade.c && touch buckets/apr_brigade.lo
/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/root/apr-util-1.6.1/include -I/root/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_buckets.lo -c buckets/apr_buckets.c && touch buckets/apr_buckets.lo
/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/root/apr-util-1.6.1/include -I/root/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_buckets_alloc.lo -c buckets/apr_buckets_alloc.c && touch buckets/apr_buckets_alloc.lo
//编译安装httpd
[root@799da60f020d ~]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
--2022-08-09 01:22:33-- https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
Resolving downloads.apache.org (downloads.apache.org)... 135.181.214.104, 88.99.95.219, 2a01:4f9:3a:2c57::2, ...
Connecting to downloads.apache.org (downloads.apache.org)|135.181.214.104|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9743277 (9.3M) [application/x-gzip]
Saving to: ‘httpd-2.4.54.tar.gz’
httpd-2.4.54.tar 100%[========>] 9.29M 41.7KB/s in 3m 22s
2022-08-09 01:25:56 (47.1 KB/s) - ‘httpd-2.4.54.tar.gz’ saved [9743277/9743277]
[root@799da60f020d ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.gz httpd-2.4.54.tar.gz
anaconda-post.log apr-util-1.6.1 original-ks.cfg
apr-1.7.0 apr-util-1.6.1.tar.gz
[root@799da60f020d ~]# tar xf httpd-2.4.54.tar.gz
[root@799da60f020d ~]# cd httpd-2.4.54
[root@799da60f020d httpd-2.4.54]# ./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
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... yes
......................................
[root@799da60f020d httpd-2.4.54]# make && make install
.................................
mkdir /usr/local/apache/cgi-bin
Installing header files
mkdir /usr/local/apache/include
Installing build system files
mkdir /usr/local/apache/build
Installing man pages and online manual
mkdir /usr/local/apache/man
mkdir /usr/local/apache/man/man1
mkdir /usr/local/apache/man/man8
mkdir /usr/local/apache/manual
make[1]: Leaving directory '/root/httpd-2.4.54'
//安装后配置
[root@799da60f020d ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@799da60f020d ~]# ls /usr/local/
apache apr-util etc include lib64 sbin src
apr bin games lib libexec share
[root@799da60f020d ~]# source /etc/profile.d/httpd.sh
[root@799da60f020d ~]# which httpd
/usr/local/apache/bin/httpd
[root@799da60f020d ~]# ls /usr/local/apache/
bin cgi-bin error icons logs manual
build conf htdocs include man modules
[root@799da60f020d ~]# ln -s /usr/local/apache/include /usr/include/apache
[root@799da60f020d ~]# vim /usr/local/apache/conf/httpd.conf
ServerName www.example.com:80 //取消注释
//启动
[root@799da60f020d ~]# apachectl start
[root@799da60f020d ~]# 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:*
//设置httpd服务的启动脚本
[root@799da60f020d ~]# vim start.sh
[root@799da60f020d ~]# cat start.sh
#!/bin/sh
/usr/local/apache/bin/httpd
/bin/bash
[root@799da60f020d ~]# chmod a+x start.sh
制作镜像
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
799da60f020d centos "/bin/bash" About an hour ago Up About an hour frosty_faraday
6a9ae218e276 httpd "httpd-foreground" 3 days ago Up 3 days 80/tcp confident_banach
[root@localhost ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' 799da60f020d web/httpd:v0.1
sha256:d950d0ef139d6203941a8f4a13c1d9ab8596c7f215b1cd1d0834847013261f3f
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
web/httpd v0.1 d950d0ef139d 8 seconds ago 778MB
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
centos latest 5d0da3dc9764 10 months ago 231MB
测试
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
web/httpd v0.1 d950d0ef139d 8 seconds ago 778MB
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]# docker run -dit -p 80:80 --name test web/httpd:v0.1 /bin/bash
8f286976c579116e176ddc02db667b2e9b19104a056249a72f8ec2169c5a93e8
[root@localhost ~]# docker exec -it test /bin/bash
[root@8f286976c579 /]# exit
exit