docker源码部署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
6f90dc569dd47e89b5ded1396546f7c255f5ffa03145375a64177ff49abadb3a
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f90dc569dd4 5d0da3dc9764 "/bin/bash" 2 minutes ago Up 2 minutes 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
[root@localhost data]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
[root@localhost data]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
[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@6f90dc569dd4 /]# cd data/
[root@6f90dc569dd4 data]# ls
apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.bz2
//先把把官方源换成阿里云源
[root@6f90dc569dd4 data]# cd /etc/yum.repos.d/
[root@6f90dc569dd4 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@6f90dc569dd4 yum.repos.d]# rm -rf *
[root@6f90dc569dd4 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@6f90dc569dd4 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@6f90dc569dd4 yum.repos.d]# yum makecache
//安装开发工具包以及httpd服务所需的依赖包,创建apache的用户与组
[root@6f90dc569dd4 data]# dnf -y groups mark install "Development Tools"
[root@6f90dc569dd4 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
[root@6f90dc569dd4 ~]# useradd -Mrs /bin/nologin apache
[root@6f90dc569dd4 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
//把httpd、apr、apr-util的包解压至/usr/local/src/下
[root@6f90dc569dd4 ~]# tar -xf /data/apr-1.6.5.tar.gz -C /usr/local/src/
[root@6f90dc569dd4 ~]# tar -xf /data/apr-util-1.6.1.tar.gz -C /usr/local/src/
[root@6f90dc569dd4 ~]# tar -xf /data/httpd-2.4.54.tar.bz2 -C /usr/local/src/
//编译安装apr
[root@6f90dc569dd4 ~]# cd /usr/local/src/apr-1.6.5/
[root@6f90dc569dd4 apr-1.6.5]# vi configure
# $RM "$cfgfile" //注释或删除这一行
[root@6f90dc569dd4 apr-1.6.5]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@6f90dc569dd4 apr-1.6.5]# make && make install
//编译安装apr-util
[root@6f90dc569dd4 apr-1.6.5]# cd /usr/local/src/apr-util-1.6.1/
[root@6f90dc569dd4 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@6f90dc569dd4 apr-util-1.6.1]# make && make install
//编译安装httpd
[root@6f90dc569dd4 httpd-2.4.54]# cd /usr/local/src/httpd-2.4.54/
[root@6f90dc569dd4 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@6f90dc569dd4 httpd-2.4.54]# make && make install
//配置httpd的全局变量并启动服务
[root@60baa880cf17 httpd-2.4.54]# cd ..
[root@60baa880cf17 src]# cd ..
[root@60baa880cf17 local]# cd apache/
[root@60baa880cf17 apache]# ls
bin cgi-bin error icons logs manual
build conf htdocs include man modules
[root@60baa880cf17 apache]# echo 'export PATH=$PATH:/usr/local/apache/bin/' >> /etc/profile.d/apache.sh
[root@60baa880cf17 apache]# source /etc/profile.d/apache.sh
[root@60baa880cf17 apache]# apachectl start
[root@60baa880cf17 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 commit -p httpd
sha256:e11bc12ce3511889560e0da12abff11bbd64cc342f6ab64da99cf64be164d96b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> e11bc12ce351 9 seconds ago 671MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]# docker tag e11bc12ce351 zjunwhite/httpd:v0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zjunwhite/httpd v0.1 e11bc12ce351 2 minutes ago 671MB
centos latest 5d0da3dc9764 10 months ago 231MB
//登录docker
[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: zjunwhite
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 zjunwhite/httpd:v0.1
The push refers to repository [docker.io/zjunwhite/httpd]
c46c49bd686d: Pushed
74ddd0ec08fa: Mounted from library/centos
v0.1: digest: sha256:44951d9ac847799cafe2a549bb038c1c94190d28fd397e7a42aee412e41de28c size: 742
//使用创建的镜像创建容器
[root@localhost ~]# docker run --name t1 -it zjunwhite/httpd:v0.1
[root@d6e228d4ece3 /]# ls
bin dev home lib64 media opt root sbin sys usr
data etc lib lost+found mnt proc run srv tmp var
[root@d6e228d4ece3 /]# cd /usr/local/src/
[root@d6e228d4ece3 src]# ls
apr-1.6.5 apr-util-1.6.1 httpd-2.4.54
[root@d6e228d4ece3 src]# apachectl start
[root@d6e228d4ece3 src]# 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 ~]# curl 172.17.0.3
<html><body><h1>It works!</h1></body></html>
镜像导入和导出
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
3318861575/httpd v0.1 e11bc12ce351 55 minutes ago 671MB
zjunwhite/httpd v0.1 e11bc12ce351 55 minutes ago 671MB
centos latest 5d0da3dc9764 10 months ago 231MB
//镜像导出为runtime.gz
[root@localhost ~]# docker save -o runtime.gz zjunwhite/httpd:v0.1
[root@localhost ~]# ls
anaconda-ks.cfg runtime.gz
[root@localhost ~]# docker rmi -f e11bc12ce351 //模拟删除,测试导入
Untagged: 3318861575/httpd:v0.1
Untagged: zjunwhite/httpd:v0.1
Untagged: zjunwhite/httpd@sha256:44951d9ac847799cafe2a549bb038c1c94190d28fd397e7a42aee412e41de28c
Deleted: sha256:e11bc12ce3511889560e0da12abff11bbd64cc342f6ab64da99cf64be164d96b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]# docker load -i runtime.gz //导入
Loaded image: zjunwhite/httpd:v0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zjunwhite/httpd v0.1 e11bc12ce351 About an hour ago 671MB
centos latest 5d0da3dc9764 10 months ago 231MB