docker制作web服务站点

拉取centos镜像

[root@localhost ~]# docker pull centos
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   10 months ago   231MB

创建容器

[root@localhost ~]# docker run -it --name z1 centos /bin/bash

在容器中编译apache

//配置阿里云yum源
[root@cec0041db18c /]# cd /etc/yum.repos.d/
[root@cec0041db18c yum.repos.d]# rm -rf *
[root@cec0041db18c 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  13271      0 --:--:-- --:--:-- --:--:-- 13271
[root@cec0041db18c yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

//安装开发环境
[root@cec0041db18c ~]# yum groups mark install "Development Tools"

//安装依赖包
[root@cec0041db18c ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel gcc gcc-c++ make wget 

//创建apache系统用户
[root@cec0041db18c ~]# useradd -rMs /sbin/nologin apache
[root@cec0041db18c ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)

下载编译apr

[root@cec0041db18c ~]# wget https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
--2022-08-11 01:52:03--  https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.215, 45.253.17.211, 45.253.17.213, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.215|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1093896 (1.0M) [application/octet-stream]
Saving to: ‘apr-1.7.0.tar.gz’

apr-1.7.0.tar.gz    100%[================>]   1.04M   193KB/s    in 5.6s    

2022-08-11 01:52:09 (191 KB/s) - ‘apr-1.7.0.tar.gz’ saved [1093896/1093896]

[root@cec0041db18c ~]# tar -xf apr-1.7.0.tar.gz 
[root@cec0041db18c ~]# cd apr-1.7.0
[root@cec0041db18c apr-1.7.0]# vi configure
[root@cec0041db18c apr-1.7.0]# 
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
   #$RM "$cfgfile"    //注释这行

[root@cec0041db18c apr-1.7.0]# ./configure --prefix=/usr/local/apr
过程省略......
[root@cec0041db18c apr-1.7.0]# make && make install
过程省略......

下载编译apr-util

[root@cec0041db18c ~]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
--2022-08-11 01:55:37--  https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.217, 45.253.17.216, 45.253.17.213, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.217|: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. 100%[================>] 541.31K   190KB/s    in 2.8s    

2022-08-11 01:55:40 (190 KB/s) - ‘apr-util-1.6.1.tar.gz’ saved [554301/554301]

[root@cec0041db18c ~]# ls
anaconda-ks.cfg    apr-1.7.0	     apr-util-1.6.1.tar.gz
anaconda-post.log  apr-1.7.0.tar.gz  original-ks.cfg
[root@cec0041db18c ~]# tar -xf apr-util-1.6.1.tar.gz 
[root@cec0041db18c ~]# cd apr-util-1.6.1
[root@cec0041db18c apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
过程省略......
[root@cec0041db18c apr-util-1.6.1]# make && make install
过程省略......

下载编译httpd

[root@cec0041db18c ~]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz
--2022-08-11 02:00:07--  https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.212, 45.253.17.211, 45.253.17.226, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.212|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9743277 (9.3M) [application/octet-stream]
Saving to: ‘httpd-2.4.54.tar.gz’

httpd-2.4.54.tar.gz 100%[================>]   9.29M   287KB/s    in 43s     

2022-08-11 02:00:50 (220 KB/s) - ‘httpd-2.4.54.tar.gz’ saved [9743277/9743277]

[root@cec0041db18c ~]# tar -xf httpd-2.4.54.tar.gz 
[root@cec0041db18c ~]# cd httpd-2.4.54
[root@cec0041db18c 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@cec0041db18c httpd-2.4.54]# make && make install
过程省略......

配置Apache服务,验证服务

[root@cec0041db18c ~]#  echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
[root@cec0041db18c ~]# source /etc/profile.d/httpd.sh  
[root@cec0041db18c ~]# vi /etc/httpd24/httpd.conf 
ServerName www.example.com:80   //将此行取消注释
[root@cec0041db18c ~]# httpd 
[root@cec0041db18c ~]# 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 -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' z1 zzking1/z1:v0.1
sha256:6b62e97eae34dace4331144733f7083e83020b45d8c95ac7c3d8aa3f49c0c02c
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
zzking1/z1   v0.1      6b62e97eae34   10 seconds ago   730MB
nginx        latest    b692a91e4e15   8 days ago       142MB
httpd        latest    f2a976f932ec   8 days ago       145MB
busybox      latest    7a80323521cc   12 days ago      1.24MB
centos       latest    5d0da3dc9764   10 months ago    231MB

用镜像制作容器,把web服务跑起来

[root@localhost ~]# docker run -d --name web1 -p 80:80 -v /var/www/html/:/usr/local/apache/htdocs zzking1/z1:v0.1
12de0565c9886b24d6c43b49fac850baa4f43613b6ee97c63936dcd3e46f54aa
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED             STATUS             PORTS                               NAMES
12de0565c988   zzking1/z1:v0.1   "/usr/local/apache/b…"   6 seconds ago       Up 4 seconds       0.0.0.0:80->80/tcp, :::80->80/tcp   web1
cec0041db18c   centos            "/bin/bash"              About an hour ago   Up About an hour                                       z1

//进行测试
[root@localhost ~]# echo "zzz" > /var/www/html/index.html
[root@localhost ~]# curl 127.0.0.1
zzz
[root@localhost ~]# rm -rf /var/www/html/index.html 

//导入html源码
[root@localhost html]# ls
AC小空调源码.zip
[root@localhost html]# unzip AC小空调源码.zip 
[root@localhost html]# ls
AC小空调源码.zip  fonts   index.html  manifest.json  static
css               images  js          README.txt     资源PRO分享站.url

posted @ 2022-08-11 11:07  世界的尽头*  阅读(101)  评论(0)    收藏  举报