基于容器的源码部署httpd服务及制作镜像
一、源码安装httpd
//拉取centos的镜像到本地仓库
[root@localhost ~]# 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@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 10 months ago 231MB
//启动centos。-it分配伪终端打开标准输入,-p把容器的80端口映射到宿主机的80端口
//-v把容器的目录挂载到宿主机上的目录上,编译安装所需的包放到宿主机的目录里就可以同步到容器里
[root@localhost ~]# docker run -d -it --name httpd -p 80:80 -v /data:/data 5d0da3dc9764 /bin/bash
a5bd52e64bf28c6664b59d43c2aed8414b597f83426c151a3fbccfb3fe7e47c8
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5bd52e64bf2 5d0da3dc9764 "/bin/bash" 4 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp httpd
//下载编译安装httpd所需的源码包
[root@localhost ~]# cd /data/
[root@localhost data]# wget https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
--2022-08-08 19:54:17-- https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.48.33.212, 111.48.33.211, 111.48.33.206, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.48.33.212|: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 --.-KB/s in 0.04s
2022-08-08 19:54:17 (26.6 MB/s) - ‘apr-1.6.5.tar.gz’ saved [1073556/1073556]
[root@localhost data]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
--2022-08-08 19:54:27-- https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.48.33.209, 111.48.33.207, 111.48.33.213, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.48.33.209|: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 --.-KB/s in 0.09s
2022-08-08 19:54:27 (5.77 MB/s) - ‘apr-util-1.6.1.tar.gz’ saved [554301/554301]
[root@localhost data]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
--2022-08-08 19:55:54-- https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.48.33.206, 111.48.33.209, 111.48.33.211, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.48.33.206|: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 18.4MB/s in 0.4s
2022-08-08 19:55:54 (18.4 MB/s) - ‘httpd-2.4.54.tar.bz2’ saved [7434530/7434530]
[root@localhost data]# ls
apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.bz2
//进入httpd容器
[root@localhost data]# docker exec -it httpd /bin/bash
[root@a5bd52e64bf2 /]# cd data/
[root@a5bd52e64bf2 data]# ls
apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.bz2 //可以看到有源码包
//把官方源换成阿里云源
[root@a5bd52e64bf2 data]# cd /etc/yum.repos.d/
[root@a5bd52e64bf2 yum.repos.d]# ls
CentOS-Linux-AppStream.repo CentOS-Linux-Devel.repo CentOS-Linux-Media.repo
CentOS-Linux-BaseOS.repo CentOS-Linux-Extras.repo CentOS-Linux-Plus.repo
CentOS-Linux-ContinuousRelease.repo CentOS-Linux-FastTrack.repo CentOS-Linux-PowerTools.repo
CentOS-Linux-Debuginfo.repo CentOS-Linux-HighAvailability.repo CentOS-Linux-Sources.repo
[root@a5bd52e64bf2 yum.repos.d]# rm -rf *
[root@a5bd52e64bf2 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 8573 0 --:--:-- --:--:-- --:--:-- 8573
[root@a5bd52e64bf2 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//安装开发工具包以及httpd服务所需的依赖包,创建apache的用户与组
[root@a5bd52e64bf2 ~]# dnf -y groups mark install "Development Tools"
.........安装过程省略...........
[root@a5bd52e64bf2 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
.........安装过程省略...........
[root@a5bd52e64bf2 ~]# useradd -Mrs /bin/nologin apache
[root@a5bd52e64bf2 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
//把httpd、apr、apr-util的包解压至/usr/local/src/下
[root@a5bd52e64bf2 ~]# tar -xf /data/apr-1.6.5.tar.gz -C /usr/local/src/
[root@a5bd52e64bf2 ~]# tar -xf /data/apr-util-1.6.1.tar.gz -C /usr/local/src/
[root@a5bd52e64bf2 ~]# tar -xf /data/httpd-2.4.54.tar.bz2 -C /usr/local/src/
//编译安装apr
[root@a5bd52e64bf2 ~]# cd /usr/local/src/apr-1.6.5/
[root@a5bd52e64bf2 apr-1.6.5]# vi configure
# $RM "$cfgfile" //把这一行注释掉或删掉
[root@a5bd52e64bf2 apr-1.6.5]# ./configure --prefix=/usr/local/apr
..........预编译过程略............
[root@a5bd52e64bf2 apr-1.6.5]# make && make install
...........编译安装过程略............
//编译安装apr-util
[root@a5bd52e64bf2 apr-1.6.5]# cd /usr/local/src/apr-util-1.6.1/
[root@a5bd52e64bf2 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
..............预编译过程略..............
[root@a5bd52e64bf2 apr-util-1.6.1]# make && make install
..............编译安装过程略.............
//编译安装httpd
[root@a5bd52e64bf2 apr-util-1.6.1]# cd /usr/local/src/httpd-2.4.54/
[root@a5bd52e64bf2 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@a5bd52e64bf2 httpd-2.4.54]# make && make install
............编译安装过程略.................
//配置httpd的全局变量并启动服务
[root@a5bd52e64bf2 httpd-2.4.54]# cd /usr/local/apache/
[root@a5bd52e64bf2 apache]# ls
bin build cgi-bin conf error htdocs icons include logs man manual modules
[root@a5bd52e64bf2 apache]# echo 'export PATH=$PATH:/usr/local/apache/bin/' >> /etc/profile.d/apache.sh
[root@a5bd52e64bf2 apache]# source /etc/profile.d/apache.sh
[root@a5bd52e64bf2 apache]# apachectl start
[root@a5bd52e64bf2 apache]# ss -anlt
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
a5bd52e64bf2 5d0da3dc9764 "/bin/bash" 14 hours ago Up 14 hours 0.0.0.0:80->80/tcp, :::80->80/tcp httpd
//-p是让运行中的容器在制作镜像时暂停一下,-m是描述信息
[root@localhost ~]# docker commit -p -m "Source deployment httpd service" a5bd52e64bf2 httpd:v0.1
sha256:b79da2045172d4b7248b5344830818565c233f68c51407daf599a2744a3b788a
//查看本地仓库,看镜像是否制作成功
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd v0.1 b79da2045172 7 seconds ago 671MB
//给镜像打上标签,上传至Docker Hub需要标注作者名
[root@localhost ~]# docker tag httpd:v0.1 guguniao/httpd:v0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
guguniao/httpd v0.1 b79da2045172 8 minutes ago 671MB
httpd v0.1 b79da2045172 8 minutes ago 671MB
//登录docker Hub的账号
[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: guguniao
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
//把本地仓库的镜像上传至Docker Hub中
[root@localhost ~]# docker push guguniao/httpd:v0.1
The push refers to repository [docker.io/guguniao/httpd]
0d000cab5248: Pushed
74ddd0ec08fa: Mounted from library/centos
v0.1: digest: sha256:13c91a081cdd4a6eacfb1c90ee98b93c380635ec2d789a6eab77adde1d2adc87 size: 742
浏览器访问:
三、测试镜像
[root@localhost ~]# docker pull guguniao/httpd:v0.1
v0.1: Pulling from guguniao/httpd
a1d0c7532777: Pull complete
24f59b1ddf5a: Pull complete
Digest: sha256:13c91a081cdd4a6eacfb1c90ee98b93c380635ec2d789a6eab77adde1d2adc87
Status: Downloaded newer image for guguniao/httpd:v0.1
docker.io/guguniao/httpd:v0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
guguniao/httpd v0.1 b79da2045172 About an hour ago 671MB
[root@localhost ~]# docker run -d --name web1 -p 80:80 guguniao/httpd:v0.1
66dfc485b38abd947746a9db17c8cefb9f731ea4ee8609786fea39ff0d28bbc7
[root@localhost ~]# docker exec -it web1 /bin/bash
[root@66dfc485b38a /]# apachectl
[root@66dfc485b38a /]# exit
[root@localhost ~]# docker inspect web1
..............................
"IPAddress": "172.17.0.2",
...............................
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
浏览器访问: