Docker基于apache镜像 使用存储挂载 制作web站点

在容器中部署安装HTTP服务

//下载编译安装httpd所需的源码包
[root@localhost ~]# mkdir /data
[root@localhost ~]# cd /data/
[root@localhost data]#  wget https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
--2022-08-11 10:46:15--  https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.211, 45.253.17.212, 45.253.17.217, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.211|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073556 (1.0M) [application/octet-stream]
Saving to: ‘apr-1.6.5.tar.gz.1’

apr-1.6.5.tar.gz.1    100%[========================>]   1.02M   451KB/s    in 2.3s    

2022-08-11 10:46:17 (451 KB/s) - ‘apr-1.6.5.tar.gz.1’ saved [1073556/1073556]

[root@localhost data]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz--2022-08-11 10:46:51--  https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.212, 45.253.17.214, 45.253.17.216, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.212|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 554301 (541K) [application/octet-stream]
Saving to: ‘apr-util-1.6.1.tar.gz.1’

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

2022-08-11 10:46:53 (364 KB/s) - ‘apr-util-1.6.1.tar.gz.1’ saved [554301/554301]


[root@localhost data]#  wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
--2022-08-11 10:47:23--  https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.215, 45.253.17.212, 45.253.17.211, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.215|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7434530 (7.1M) [application/octet-stream]
Saving to: ‘httpd-2.4.54.tar.bz2.1’

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

2022-08-11 10:47:43 (495 KB/s) - ‘httpd-2.4.54.tar.bz2.1’ saved [7434530/7434530]

//拉取centos的镜像到本地仓库
[root@localhost data]# cd
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Image is up to date for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        latest    dabbfbe0c57b   7 months ago    144MB
centos       latest    5d0da3dc9764   10 months ago   231MB

//启动一个centos系统的容器,使用-v将容器的/data目录挂载在宿主机上的/data目录,-it分配伪终端打开标准输入
[root@localhost ~]# docker run -itd --name web -v /data:/data centos /bin/bash
0931fd0921937e1467bc43c1b199c3393348ea1318f7f664a2af16788d798429

//把源码包上传至宿主机的/data目录会同步至此目录
[root@localhost ~]# docker exec -it web /bin/bash
[root@0931fd092193 /]# ls /data/
apr-1.6.5.tar.gz    apr-util-1.6.1.tar.gz    httpd-2.4.54.tar.bz2

//把官方源换成阿里云源
[root@0931fd092193 /]# cd /etc/yum.repos.d/
[root@0931fd092193 yum.repos.d]# rm -rf *
[root@0931fd092193 yum.repos.d]# ls -l
total 0
[root@318464db055f 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  14339      0 --:--:-- --:--:-- --:--:-- 14339
[root@318464db055f 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@0931fd092193 ~]# dnf -y  groups mark install "Development Tools"

[root@0931fd092193 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel

[root@0931fd092193 ~]# useradd -Mrs /bin/nologin apache
[root@0931fd092193 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)

//把httpd、apr、apr-util的包解压至/usr/local/src/下
[root@0931fd092193 ~]# tar -xf /data/apr-1.6.5.tar.gz -C /usr/local/src/
[root@0931fd092193 ~]# tar -xf /data/apr-util-1.6.1.tar.gz -C /usr/local/src/
[root@0931fd092193 ~]# tar -xf /data/httpd-2.4.54.tar.bz2 -C /usr/local/src

//编译安装apr
[root@0931fd092193 ~]# cd /usr/local/src/apr-1.6.5/
[root@0931fd092193 apr-1.6.5]# vi configure
#    $RM "$cfgfile"    //把这一行注释掉或删掉
[root@0931fd092193 apr-1.6.5]# ./configure --prefix=/usr/local/apr

[root@0931fd092193 apr-1.6.5]# make && make install


//编译安装apr-util
[root@0931fd092193 apr-1.6.5]# cd /usr/local/src/apr-util-1.6.1/
[root@0931fd092193 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@0931fd092193 apr-util-1.6.1]# make && make install


//编译安装httpd
[root@0931fd092193 apr-util-1.6.1]# cd /usr/local/src/httpd-2.4.54/
[root@0931fd092193 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@0931fd092193 httpd-2.4.54]# make && make install

//配置httpd的全局变量,头文件映射,启动httpd服务
///配置编程变量
[root@0931fd092193 ~]# echo 'export PATH=$PATH:/usr/local/apache/bin/' >> /etc/profile.d/apache.sh
[root@0931fd092193 ~]# source /etc/profile.d/apache.sh
//映射头文件
[root@0931fd092193 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
//除去提示信息
[root@0931fd092193 ~]# sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
//启动服务
[root@0931fd092193 ~]# httpd
[root@0931fd092193 ~]# 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@0931fd092193 ~]# curl 127.0.0.1
<html><body><h1>It works!</h1></body></html>

基于制作好的HTTP容器制作镜像

//可以看到该容器正在运行
[root@0931fd092193 ~]# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
0931fd092193   centos    "/bin/bash"   16 minutes ago   Up 16 minutes             web

//-p参数是让正在运作中的容器先暂停等制作完镜像。-c后面跟上的命令是让该镜像作为容器启动时把httpd服务也启用
[root@localhost ~]# docker commit -p -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' 0931fd092193 kinghtsl/httpd:v0.1
sha256:2bfa1fc806484f83c85e1bb47195c36621a86e0a15246ae283f272fa8a7f01b6
[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
kinghtsl/httpd   v0.1      2bfa1fc80648   16 seconds ago   671MB
httpd            latest    dabbfbe0c57b   7 months ago     144MB
centos           latest    5d0da3dc9764   10 months ago    231MB

//将制作好的镜像上传
[root@localhost ~]# docker login
Authenticating with existing credentials...
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]
17e6d847754e: Pushed
74ddd0ec08fa: Layer already exists
Head "https://registry-1.docker.io/v2/guguniao/httpd/blobs/sha256:a6a20aee6e453de386e3673850346a0a1c1d6faa2c0dc9db70778cededce99b8": EOF

测试镜像,部署html网站

//启动一个容器。--restart always让容器在docker开启时就运行,-p映射端口,-v指定容器目录的挂载点
[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
kinghtsl/httpd   v0.1      2bfa1fc80648   7 minutes ago   671MB
httpd            latest    dabbfbe0c57b   7 months ago    144MB
centos           latest    5d0da3dc9764   10 months ago   231MB
[root@localhost ~]# docker run -d --name web --restart always -p 80:80 -v /httpd_html:/usr/local/apache/htdocs kinghtsl/httpd:v0.1 
f76dfbe827783b9a0c14fc3d0eff2ec8592c73158efdb95e14424440dfad6471
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                               NAMES
f76dfbe82778   kinghtsl/httpd:v0.1   "/usr/local/apache/b…"   9 seconds ago   Up 7 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web

//把“html”的源码放在httpd的网页内容存放目录
[root@localhost ~]# cd /httpd_html/
[root@localhost httpd_html]# ls
htmlxunakuhei.zip
[root@localhost httpd_html]# unzip htmlxunakuhei.zip 

[root@localhost httpd_html]# ls
htmlxunakuhei  htmlxunakuhei.zip
[root@localhost httpd_html]# rm -f htmlxunakuhei.zip 
[root@localhost httpd_html]# ls
htmlxunakuhei
[root@localhost httpd_html]# cd htmlxunakuhei/
[root@localhost htmlxunakuhei]# mv * /httpd_html/
[root@localhost httpd_html]# rm -rf htmlxunakuhei/
[root@localhost httpd_html]# ls
css  fonts  images  index.html  js  m  服务器之家.url  精品免费商业源码下载.url
[root@localhost httpd_html]# 

测试

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值