基于容器的方式安装编译apache做成镜像

 1. 首先下载3个安装包,apr,apr-util,httpd.

1 [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
2 [root@localhost ~]# ls
3 anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz   下载到真机

2. 拉取centos最新版的镜像,启动容器,把3个包复制过来。

复制代码
 1 [root@localhost ~]# docker pull centos      # 拉取镜像
 2 [root@localhost ~]# docker images
 3 REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
 4 busybox      latest    beae173ccac6   3 months ago   1.24MB
 5 httpd        latest    dabbfbe0c57b   4 months ago   144MB
 6 centos       latest    5d0da3dc9764   7 months ago   231MB
 7 [root@localhost ~]# docker run -it --name c1 centos /bin/bash   #  启动容器
 8 [root@localhost ~]# mkdir software     #   创建一个目录
 9 [root@localhost ~]# mv *.gz software/   #   把包放进去
10 [root@localhost software]# ls
11 apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
12 [root@localhost ~]# docker cp software c1:/usr/src/   #   整个目录复制到容器里
13 [root@ca70ec7cb9fc src]# ls
14 debug  kernels    software
15 [root@ca70ec7cb9fc software]# ls
16 apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
复制代码

3.  解压,编译,安装

复制代码
[root@ca70ec7cb9fc software]# tar xf apr-1.7.0.tar.gz 
[root@ca70ec7cb9fc software]# tar xf apr-util-1.6.1.tar.gz 
[root@ca70ec7cb9fc software]# tar xf httpd-2.4.53.tar.gz
[root@ca70ec7cb9fc software]# ls
apr-1.7.0  apr-1.7.0.tar.gz  apr-util-1.6.1  apr-util-1.6.1.tar.gz  httpd-2.4.53  httpd-2.4.53.tar.gz
[root@ca70ec7cb9fc software]# cd apr-1.7.0     #   进去
[root@ca70ec7cb9fc apr-1.7.0]# vi configure  #   编辑
$RM "$cfgfile"    #  找到此行删除
[root@ca70ec7cb9fc apr-1.7.0]# ./configure --prefix=/usr/local/apr  #  报错没有C编译器
[root@ca70ec7cb9fc apr-1.7.0]# cd /etc/yum.repos.d/  
[root@ca70ec7cb9fc yum.repos.d]# ls
CentOS-Linux-AppStream.repo         CentOS-Linux-Debuginfo.repo  CentOS-Linux-FastTrack.repo          CentOS-Linux-Plus.repo
CentOS-Linux-BaseOS.repo         CentOS-Linux-Devel.repo      CentOS-Linux-HighAvailability.repo  CentOS-Linux-PowerTools.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Extras.repo      CentOS-Linux-Media.repo          CentOS-Linux-Sources.repo
[root@ca70ec7cb9fc yum.repos.d]# rm -rf *    #  删掉换国内源
[root@ca70ec7cb9fc yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@ca70ec7cb9fc yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo    #  配置基础源
[root@ca70ec7cb9fc yum.repos.d]# ls
CentOS-Base.repo
[root@ca70ec7cb9fc yum.repos.d]# dnf clean all
[root@ca70ec7cb9fc yum.repos.d]# dnf makecache
[root@ca70ec7cb9fc apr-1.7.0]# dnf -y install gcc make    #   缺什么装什么  保证最小化
[root@ca70ec7cb9fc apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@ca70ec7cb9fc apr-1.7.0]# make -j 4
[root@ca70ec7cb9fc apr-1.7.0]# make install

[root@ca70ec7cb9fc apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@ca70ec7cb9fc apr-util-1.6.1]# dnf -y install expat-devel   #   安装依赖包
[root@ca70ec7cb9fc apr-util-1.6.1]# make -j 4
[root@ca70ec7cb9fc apr-util-1.6.1]# make install
[root@ca70ec7cb9fc httpd-2.4.53]# useradd -r -M -s /sbin/nologin apache    创建一个apache用户
[root@ca70ec7cb9fc 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@ca70ec7cb9fc httpd-2.4.53]# dnf -y install pcre-devel    #  安装
[root@ca70ec7cb9fc httpd-2.4.53]# dnf -y install openssl-devel
[root@ca70ec7cb9fc httpd-2.4.53]# make -j 4
[root@ca70ec7cb9fc httpd-2.4.53]# make install
复制代码

启动

复制代码
[root@ca70ec7cb9fc apache]# ./bin/httpd    #   后台启动
[root@ca70ec7cb9fc apache]# 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@ca70ec7cb9fc apache]# pkill httpd 
[root@ca70ec7cb9fc ~]# cd /   #  进到根目录
[root@ca70ec7cb9fc /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@ca70ec7cb9fc /]# vi entrypoint.sh   #  创建一个脚本启动

#!/bin/bash  

/usr/local/apache/bin/httpd && sleep 5d   #  启动然后执行
[root@ca70ec7cb9fc /]# chmod +x entrypoint.sh   #   给脚本加执行权限
[root@ca70ec7cb9fc /]# ls
bin  dev  entrypoint.sh  etc  home  lib  lib64    lost+found  media  mnt    opt  proc  root  run  sbin  srv  sys  tmp  usr    var
[root@ca70ec7cb9fc /]# 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@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS       PORTS     NAMES
ca70ec7cb9fc   centos    "/bin/bash"   4 hours ago   Up 4 hours             c1
[root@localhost ~]# curl 172.17.0.2      
<html><body><h1>It works!</h1></body></html>
复制代码

制作镜像

复制代码
[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
[root@localhost ~]# docker commit -a 'sun <1@2.com>' -c 'CMD ["/entrypoint.sh"]' -p c1 httpd:v1.0    #制作镜像
sha256:678c8c1a431fd2e78b46d9fc40220bed2ca13f1b5790badfa59bc70b88b67aef
[root@localhost ~]# docker run -d --name web -p 80:80 httpd:v1.0         #   用作的镜像启动容器
0d14c5b1c3ad3b38c4214901d41cdc2d549ad1603cfa69f0318496c56dbea11a
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE        COMMAND            CREATED          STATUS          PORTS                               NAMES
0d14c5b1c3ad   httpd:v1.0   "/entrypoint.sh"   25 seconds ago   Up 14 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web
ca70ec7cb9fc   centos       "/bin/bash"        4 hours ago      Up 14 minutes                                       c1
复制代码

 

 前台运行制作制作镜像

复制代码
[root@ca70ec7cb9fc /]# /usr/local/apache/bin/httpd -DFOREGROUND      #  前台运行
CONTAINER ID   IMAGE        COMMAND            CREATED          STATUS          PORTS                               NAMES
0d14c5b1c3ad   httpd:v1.0   "/entrypoint.sh"   24 minutes ago   Up 24 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   web
ca70ec7cb9fc   centos       "/bin/bash"        5 hours ago      Up 38 minutes                                       c1
[root@localhost ~]# docker inspect c1      #  找id
"IPAddress": "172.17.0.2",
[root@localhost ~]# curl 172.17.0.2       #   访问成功
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# docker commit -a 'sun <1@2.com>' -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' -p c1 httpd:v2.0   #   创建镜像
sha256:48e7fec51ecf0c7e6891eca600363ba6919f5e0d5d6251dad55a0d488a2101fc
[root@localhost ~]# docker images   
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
httpd        v2.0       48e7fec51ecf   2 minutes ago    628MB
httpd        v1.0      678c8c1a431f   37 minutes ago   628MB
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 rm -f web       #   80端口号被web占用  删除掉
web
[root@localhost ~]# ss -antl
State             Recv-Q            Send-Q                       Local Address:Port                        Peer Address:Port            Process            
LISTEN            0                 128                                0.0.0.0:22                               0.0.0.0:*                                  
LISTEN            0                 128                                   [::]:22                                  [::]:*                                  
[root@localhost ~]# docker start web1     #  启动
web1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS          PORTS                               NAMES
964278fd4c8c   httpd:2.0   "/usr/local/apache/b…"   4 minutes ago   Up 12 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web1
ca70ec7cb9fc   centos      "/bin/bash"              5 hours ago     Up 56 minutes                                       c1
复制代码

 

 

缩减镜像的大小

复制代码
[root@ca70ec7cb9fc /]# du -sh *    #  查看哪些目录过大
0    bin
0    dev
4.0K    entrypoint.sh
2.4M    etc
0    home
0    lib
0    lib64
0    lost+found
0    media
0    mnt
0    opt
du: cannot access 'proc/24/task/24/fd/4': No such file or directory
du: cannot access 'proc/24/task/24/fdinfo/4': No such file or directory
du: cannot access 'proc/24/fd/4': No such file or directory
du: cannot access 'proc/24/fdinfo/4': No such file or directory
0    proc
36K    root
4.0K    run
0    sbin
0    srv
0    sys
12K    tmp
547M    usr
53M    var             #  var有56m进去查看
[root@ca70ec7cb9fc var]# du -sh *
0    adm
26M    cache      #   缓存没用可以删掉
0    crash
0    db
0    empty
0    ftp
0    games
0    gopher
0    kerberos
25M    lib
0    local
0    lock
1.7M    log
0    mail
0    nis
0    opt
0    preserve
0    run
0    spool
0    tmp
0    yp
[root@ca70ec7cb9fc var]# cd  cache
[root@ca70ec7cb9fc cache]# ls
bpf  dnf  ldconfig  private
[root@ca70ec7cb9fc cache]# rm -rf *
[root@ca70ec7cb9fc cache]# ls
[root@ca70ec7cb9fc /]# cd usr/
[root@ca70ec7cb9fc usr]# du -sh *
36M    bin
0    games
15M    include
41M    lib
126M    lib64
56M    libexec
47M    local
11M    sbin
68M    share
151M    src
0    tmp
[root@ca70ec7cb9fc usr]# cd src
[root@ca70ec7cb9fc src]# du -sh *
0    debug
0    kernels
151M    software
[root@ca70ec7cb9fc src]# rm -rf software/       #  安装包安装之后也不需要
[root@ca70ec7cb9fc usr]# dnf -y remove make gcc  #  编译的工具也不需要了可以清除一部分空间
[root@ca70ec7cb9fc /]# rm -rf entrypoint.sh   #  脚本也不需要了
[root@localhost ~]# docker commit -a 'sun <1@2.com>' -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' -p c1 httpd:v3.0
sha256:51efb88f38a58d2f8ede30a283838c6a7f332e6102b8407431fc75b72849e14f
[root@localhost ~]# docker images      #   缩减将近一半
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
httpd        v3.0       51efb88f38a5   23 seconds ago      365MB
httpd        v2.0       48e7fec51ecf   31 minutes ago      628MB
httpd        v1.0      678c8c1a431f   About an hour ago   628MB
busybox      latest    beae173ccac6   3 months ago        1.24MB
httpd        latest    dabbfbe0c57b   4 months ago        144MB
centos       latest    5d0da3dc9764   7 months ago        231MB
复制代码

 

posted @   孙一鸣  阅读(82)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示