容器里源码安装httpd

容器里源码安装httpd

1. 拉取centos的镜像到本地仓库

[root@docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
busybox      latest    7a80323521cc   10 days ago     1.24MB
nginx        latest    605c77e624dd   7 months ago    141MB
centos       latest    5d0da3dc9764   10 months ago   231MB

2. 启动centos。-it分配伪终端打开标准输入,-p把容器的80端口映射到宿主机的80端口

-v把容器的目录挂载到宿主机上的目录上,编译安装所需的包放到宿主机的目录里就可以同步到容器里

[root@docker ~]# docker run -d -it --name httpd -p 80:80 -v /data:/data 5d0da3dc9764 /bin/bash
ce54bb8a3988bf6048ffd4eb2212590604c6b26bf6c45b159f0e1ccad3cd828e
[root@docker ~]# docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS          PORTS                               NAMES
ce54bb8a3988   5d0da3dc9764   "/bin/bash"   24 seconds ago   Up 22 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   httpd
[root@docker ~]# 

3. 下载编译安装httpd所需的源码包

[root@docker ~]# cd /data/
[root@docker data]# wget https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
--2022-08-09 09:20:18--  https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.217, 45.253.17.216, 43.224.184.228, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.217|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073556 (1.0M) [application/octet-stream]
Saving to: ‘apr-1.6.5.tar.gz’

apr-1.6.5.tar.gz       100%[============================>]   1.02M   286KB/s    in 3.7s    

2022-08-09 09:20:22 (286 KB/s) - ‘apr-1.6.5.tar.gz’ saved [1073556/1073556]

[root@docker data]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
--2022-08-09 09:20:48--  https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 43.224.184.231, 45.253.17.212, 43.224.184.227, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|43.224.184.231|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 554301 (541K) [application/octet-stream]
Saving to: ‘apr-util-1.6.1.tar.gz’

apr-util-1.6.1.tar.gz  100%[============================>] 541.31K   276KB/s    in 2.0s    

2022-08-09 09:20:50 (276 KB/s) - ‘apr-util-1.6.1.tar.gz’ saved [554301/554301]

[root@docker data]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
--2022-08-09 09:21:01--  https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 43.224.184.226, 45.253.17.213, 45.253.17.215, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|43.224.184.226|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7434530 (7.1M) [application/octet-stream]
Saving to: ‘httpd-2.4.54.tar.bz2’

httpd-2.4.54.tar.bz2   100%[============================>]   7.09M   294KB/s    in 26s     

2022-08-09 09:21:27 (282 KB/s) - ‘httpd-2.4.54.tar.bz2’ saved [7434530/7434530]

[root@docker data]# ls
apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.bz2
[root@docker data]# 

4. 进入httpd容器,可以看到有源码包

[root@docker data]# docker exec -it httpd /bin/bash
[root@ce54bb8a3988 /]# cd data/
[root@ce54bb8a3988 data]# ls
apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.bz2
[root@ce54bb8a3988 data]# 

5. 把官方源换成阿里云源

[root@ce54bb8a3988 data]# cd /etc/yum.repos.d/
[root@ce54bb8a3988 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@ce54bb8a3988 yum.repos.d]# rm -rf *
[root@ce54bb8a3988 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
100  2495  100  2495    0     0   7920      0 --:--:-- --:--:-- --:--:--  7920
[root@ce54bb8a3988 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@ce54bb8a3988 yum.repos.d]#       

6. 安装开发工具包以及httpd服务所需的依赖包,创建apache的用户与组

[root@ce54bb8a3988 ~]# dnf -y  groups mark install "Development Tools"
[root@ce54bb8a3988 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
[root@ce54bb8a3988 ~]# useradd -Mrs /bin/nologin apache
[root@ce54bb8a3988 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
[root@ce54bb8a3988 ~]# 

7. 把httpd、apr、apr-util的包解压至/usr/local/src/下

[root@ce54bb8a3988 ~]# tar -xf /data/apr-1.6.5.tar.gz -C /usr/local/src/
[root@ce54bb8a3988 ~]# tar -xf /data/apr-util-1.6.1.tar.gz -C /usr/local/src/
[root@ce54bb8a3988 ~]# yum -y install bzip2
[root@ce54bb8a3988 ~]# tar -xf /data/httpd-2.4.54.tar.bz2 -C /usr/local/src/

8. 编译安装apr,apr-util,httpd

[root@ce54bb8a3988 apr-1.6.5]# vi configure
# $RM "$cfgfile"
[root@ce54bb8a3988 apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@ce54bb8a3988 apr-1.6.5]# make && make install
[root@ce54bb8a3988 apr-1.6.5]# cd /usr/local/src/apr-util-1.6.1/
[root@ce54bb8a3988 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@ce54bb8a3988 apr-util-1.6.1]# make && make install
[root@ce54bb8a3988 apr-util-1.6.1]# cd /usr/local/src/httpd-2.4.54/
[root@ce54bb8a3988 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
[root@ce54bb8a3988 httpd-2.4.54]# make && make install

9. 配置httpd的全局变量并启动服务

[root@ce54bb8a3988 httpd-2.4.54]# cd /usr/local/apache/
[root@ce54bb8a3988 apache]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs	man  manual  modules
[root@ce54bb8a3988 apache]# echo 'export PATH=$PATH:/usr/local/apache/bin/' >> /etc/profile.d/apache.sh
[root@ce54bb8a3988 apache]# source /etc/profile.d/apache.sh 
[root@ce54bb8a3988 apache]# apachectl start
[root@ce54bb8a3988 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@ce54bb8a3988 apache]# 
[root@ce54bb8a3988 ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>

10. 创建镜像

[root@docker ~]# docker commit -p httpd
sha256:b5aa9ca2da0ac5dd906b217c840c835a511fee257ce8e119bf174480697881b3
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
<none>       <none>    b5aa9ca2da0a   About a minute ago   671MB
centos       latest    5d0da3dc9764   10 months ago        231MB
[root@docker ~]# docker tag b5aa9ca2da0a wanli123/httpd:v0.1
[root@docker ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
wanli123/httpd   v0.1      b5aa9ca2da0a   4 minutes ago   671MB
centos           latest    5d0da3dc9764   10 months ago   231MB
[root@docker ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: wanli123
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@docker ~]# 
[root@docker ~]# docker push wanli123/httpd:v0.1
The push refers to repository [docker.io/wanli123/httpd]
0b4dec31b40e: Pushed 
74ddd0ec08fa: Mounted from library/centos 
v0.1: digest: sha256:c8186de25ecb62ffba9736beb589a02eeb6cad831fbaea733871feaa3e344ca3 size: 742
[root@docker ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
wanli123/httpd   v0.1      b5aa9ca2da0a   9 minutes ago   671MB
centos           latest    5d0da3dc9764   10 months ago   231MB
[root@docker ~]# 

在这里插入图片描述

11. 拉取镜像

[root@docker ~]# docker pull wanli123/httpd:v0.1
v0.1: Pulling from wanli123/httpd
a1d0c7532777: Already exists 
7d094a626095: Pull complete 
Digest: sha256:c8186de25ecb62ffba9736beb589a02eeb6cad831fbaea733871feaa3e344ca3
Status: Downloaded newer image for wanli123/httpd:v0.1
docker.io/wanli123/httpd:v0.1
[root@docker ~]# 
[root@docker ~]# 
[root@docker ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
wanli123/httpd   v0.1      b5aa9ca2da0a   37 minutes ago   671MB
centos           latest    5d0da3dc9764   10 months ago    231MB
[root@docker ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值