基于容器编译安装apache制作为镜像

本文介绍如何从CentOS 8拉取镜像,编译安装HTTPD,并创建可运行的容器。通过更换软件源、安装依赖、配置编译参数等步骤,最终实现HTTPD的服务启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

拉取centos8镜像

[root@localhost ~]# 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@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       8         5d0da3dc9764   10 months ago   231MB

制作httpd容器

[root@localhost ~]# docker run -it --name httpd centos:8 /bin/bash
[root@96d95580f706 /]# cd /etc/yum.repos.d/
[root@96d95580f706 yum.repos.d]# rm -rf *
[root@96d95580f706 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  10895      0 --:--:-- --:--:-- --:--:-- 10895
[root@96d95580f706 yum.repos.d]# ls
CentOS-Base.repo
[root@96d95580f706 yum.repos.d]# yum clean all
Failed to set locale, defaulting to C.UTF-8
0 files removed
[root@96d95580f706 yum.repos.d]# yum makecache
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.ali 966 kB/s | 4.6 MB     00:04    
CentOS-8.5.2111 - Extras - mirrors.a  58 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - mirror 664 kB/s | 8.4 MB     00:13    
Metadata cache created.
[root@96d95580f706 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

//开启一个新终端,不要退出退出容器会停止
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED         STATUS         PORTS     NAMES
96d95580f706   centos:8   "/bin/bash"   5 minutes ago   Up 5 minutes             httpd
//将源码包传到容器内
[root@localhost ~]# docker cp apr-1.7.0.tar.gz httpd:/root
[root@localhost ~]# docker cp apr-util-1.6.1.tar.gz httpd:/root
[root@localhost ~]# docker cp httpd-2.4.54.tar.gz httpd:/root

#安装依赖包
[root@96d95580f706 ~]# yum groups mark install 'Development Tools' -y
[root@96d95580f706 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make vim

编译httpd

[root@96d95580f706 ~]# useradd -r -M -s /sbin/nologin apache
[root@96d95580f706 ~]# cd /root/
[root@96d95580f706 ~]# ls
anaconda-ks.cfg    apr-1.7.0.tar.gz	  httpd-2.4.54.tar.gz
anaconda-post.log  apr-util-1.6.1.tar.gz  original-ks.cfg
[root@96d95580f706 ~]# tar xf apr-1.7.0.tar.gz
[root@96d95580f706 ~]# tar xf apr-util-1.6.1.tar.gz 
[root@96d95580f706 ~]# tar xf httpd-2.4.54.tar.gz   
[root@96d95580f706 ~]# cd apr-1.7.0
[root@96d95580f706 apr-1.7.0]# vim configure
//搜索下面这条 注释掉 或者删除
$RM "$cfgfile"
//编译apr
[root@96d95580f706 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@96d95580f706 apr-1.7.0]# make && make install

//编译apr-util
[root@96d95580f706 apr-1.7.0]# cd 
[root@96d95580f706 ~]# cd apr-util-1.6.1
[root@96d95580f706 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@96d95580f706 apr-util-1.6.1]# make && make install

//编译httpd
[root@96d95580f706 httpd-2.4.54]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --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@96d95580f706 httpd-2.4.54]# make && make install

设置环境变量

//环境变量
[root@96d95580f706 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@96d95580f706 ~]# source /etc/profile.d/apache.sh
[root@96d95580f706 ~]# which httpd       
/usr/local/apache/bin/httpd
[root@96d95580f706 ~]# which apachectl
/usr/local/apache/bin/apachectl

//软连接
[root@96d95580f706 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
[root@96d95580f706 ~]# vim /etc/httpd24/httpd.conf
#ServerName www.example.com:80   // 此行取消注释
[root@96d95580f706 ~]# apachectl start
[root@96d95580f706 ~]# 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@96d95580f706 ~]# vi start.sh
[root@96d95580f706 ~]# cat start.sh
#!/bin/sh

/usr/local/apache/bin/httpd 
/bin/bash
[root@96d95580f706 ~]# chmod a+x start.sh

制作httpd镜像并创建一个能够访问web网站的容器

制作httpd的镜像

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS          PORTS     NAMES
96d95580f706   centos:8   "/bin/bash"   59 minutes ago   Up 59 minutes             httpd
[root@localhost ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' 96d95580f706 chenlang123/httpd:v0.1
sha256:0a876bd309f11221bf200f01606f3d31a75a5c2be51df23fa28ab9ad2d69904d
[root@localhost ~]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
chenlang123/httpd   v0.1      0a876bd309f1   23 seconds ago   744MB
centos              8         5d0da3dc9764   10 months ago    231MB
[root@localhost ~]# 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: chenlang123
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@localhost ~]# docker push chenlang123/httpd:v0.1
The push refers to repository [docker.io/chenlang123/httpd]
12da7c3b1809: Pushed 
74ddd0ec08fa: Pushed 
v0.1: digest: sha256:66462b764cd54659d99eae5c43000cab9b73d267ce275ba049d11877e38d0ab7 size: 742

在这里插入图片描述

[root@localhost ~]# docker pull chenlang123/httpd:v0.1
v0.1: Pulling from chenlang123/httpd
a1d0c7532777: Already exists 
ea6686a0982d: Already exists 
Digest: sha256:66462b764cd54659d99eae5c43000cab9b73d267ce275ba049d11877e38d0ab7
Status: Downloaded newer image for chenlang123/httpd:v0.1
docker.io/chenlang123/httpd:v0.1
[root@localhost ~]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED         SIZE
chenlang123/httpd   v0.1      0a876bd309f1   4 hours ago     744MB
centos              8         5d0da3dc9764   10 months ago   231MB
[root@localhost ~]# docker run -dit -p 80:80 --name web chenlang123/httpd:v0.1 /bin/bash
679e17935af96b5488f157064c11263358e710bb35517c1570acb92c72c03619
[root@localhost ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128        127.0.0.1:9000      0.0.0.0:*          
LISTEN 0      128          0.0.0.0:80        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*          
LISTEN 0      80                 *:3306            *:*          
LISTEN 0      128             [::]:80           [::]:*
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值