docker编译安装apache镜像

容器编译安装httpd

环境准备

//配置yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# rm -rf *
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@localhost 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 yum.repos.d]# dnf clean all
[root@localhost yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@localhost yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# dnf clean all 
[root@localhost yum.repos.d]# dnf makecache

//安装docker
[root@localhost yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# dnf -y install docker-ce --allowerasing 
[root@localhost ~]# systemctl enable --now docker

//配置镜像加速器
[root@localhost ~]# cat /etc/docker/daemon.json 
{
          "registry-mirrors": ["https://zs81wprp.mirror.aliyuncs.com"]
}
[root@localhost docker]# systemctl daemon-reload 
[root@localhost docker]# systemctl restart docker

//安装依赖包
[root@2701856ad535 src]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make

//将所需包cp到docker
[root@localhost ~]# docker cp apr-1.7.0.tar.gz httpd:/usr/src
[root@localhost ~]# docker cp apr-util-1.6.1.tar.gz httpd:/usr/src
[root@localhost ~]# docker cp httpd-2.4.53.tar.gz httpd:/usr/src

创建容器

[root@localhost ~]# docker pull centos:8
8: Pulling from library/centos
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Image is up to date for centos:8
docker.io/library/centos:8
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       8         5d0da3dc9764   7 months ago   231MB
[root@localhost ~]# docker run -it --name httpd centos:8 /bin/bash
[root@2701856ad535 ]# 

基于容器源码编译httpd

//解压安装包
[root@2701856ad535 src]# tar -xf apr-1.7.0.tar.gz 
[root@2701856ad535 src]# tar -xf apr-util-1.6.1.tar.gz 
[root@2701856ad535 src]# tar -xf httpd-2.4.53.tar.gz   

//apr-1.7.0
[root@2701856ad535 src]# cd apr-1.7.0
[root@2701856ad535 apr-1.7.0]# vim configure
   $RM "$cfgfile"  注释或删除这一行
[root@2701856ad535 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@2701856ad535 apr-1.7.0]# make && make install

//apr-util-1.6.1
[root@2701856ad535 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@2701856ad535 apr-util-1.6.1]# make && make install

//httpd-2.4.53
[root@2701856ad535 src]# cd httpd-2.4.53
[root@2701856ad535 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@2701856ad535 httpd-2.4.53]# make && make install

配置httpd

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

//添加映射关系
[root@2701856ad535 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
[root@2701856ad535 ~]# ls -l /usr/include/|grep apache
lrwxrwxrwx.  1 root root     26 Apr 26 17:15 apache -> /usr/local/apache/include/

//头文件
[root@2701856ad535 ~]# cd /usr/local/apache/conf/
[root@2701856ad535 conf]# vim httpd.conf 
ServerName www.example.com:80  取消此行注释

//编写启动脚本
[root@2701856ad535 conf]# cd /
[root@2701856ad535 /]# cat start.sh 
#! /bash
/usr/local/apache/bin/apachectl start
/bin/bash
[root@2701856ad535 /]# chmod a+x start.sh

//启动httpd
[root@2701856ad535 ~]# apachectl start
[root@2701856ad535 ~]# 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@2701856ad535 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
[root@2701856ad535 ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>

制作httpd镜像

//查看运行的容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND       CREATED       STATUS       PORTS     NAMES
2701856ad535   centos:8   "/bin/bash"   2 hours ago   Up 2 hours             httpd

//制作httpd镜像(另起一个终端,httpd容器不能停止)
[root@localhost ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' 2701856ad535 lzx17683765691/v1.0
sha256:e7068fccdd09ebecdf36e2bb93e726900e3001680f0e3a597a8b12f354275912
[root@localhost ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
lzx17683765691/v1.0   latest    e7068fccdd09   2 minutes ago   773MB
centos                8         5d0da3dc9764   7 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: lzx17683765691
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 lzx17683765691/v1.0
Using default tag: latest
The push refers to repository [docker.io/lzx17683765691/v1.0]
d1009eb4a5de: Pushed 
74ddd0ec08fa: Mounted from library/centos 
latest: digest: sha256:c805cf8d04df3f9916441b1d9d1da6b494fa7a436d481cb51a4ec5ab883bf9b5 size: 742

使用node1主机拉取镜像验证

[root@node1 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@node1 ~]# docker pull lzx17683765691/v1.0:latest
latest: Pulling from lzx17683765691/v1.0
a1d0c7532777: Pull complete 
d1bde94f6c0c: Pull complete 
Digest: sha256:c805cf8d04df3f9916441b1d9d1da6b494fa7a436d481cb51a4ec5ab883bf9b5
Status: Downloaded newer image for lzx17683765691/v1.0:latest
docker.io/lzx17683765691/v1.0:latest
[root@node1 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
lzx17683765691/v1.0   latest    e7068fccdd09   29 minutes ago   773MB
设置在前台运行
1. 设置在前台运行
[root@6a41da7c72b8 ~]# ss -antl 
State    Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process    
[root@6a41da7c72b8 ~]# /usr/local/apache/bin/httpd -DFOREGROUND

//访问
[root@6a41da7c72b8 ~]# docker inspect c1
[root@6a41da7c72b8 ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@6a41da7c72b8 ~]# 
posted @ 2022-04-27 02:36  夏天的海  阅读(65)  评论(0)    收藏  举报