Docker源码部署Web服务自启动

Docker源码部署Web服务自启动

基于容器源码部署httpd

//下载编译安装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-10 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-10 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-10 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-10 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-10 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-10 19:55:54 (18.4 MB/s) - ‘httpd-2.4.54.tar.bz2’ saved [7434530/7434530]

//拉取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系统的容器,使用-v将容器的/data目录挂载在宿主机上的/data目录,-it分配伪终端打开标准输入
[root@localhost ~]# docker run -it --name web1 -v /data:/data centos /bin/bash
//把源码包上传至宿主机的/data目录会同步至此目录
[root@b36366a06649 /]# ls /data/
apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.bz2

//把官方源换成阿里云源
[root@b36366a06649 /]# cd /etc/yum.repos.d/
[root@b36366a06649 yum.repos.d]# rm -rf *
[root@b36366a06649 yum.repos.d]# ls -l
total 0
[root@b36366a06649 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@b36366a06649 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@b36366a06649 ~]# dnf -y  groups mark install "Development Tools"
..........安装过程略............
[root@b36366a06649 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
..........安装过程略............
[root@b36366a06649 ~]# useradd -Mrs /bin/nologin apache
[root@b36366a06649 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)

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

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

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

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

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

基于容器制作镜像

在上述步骤的基础上,再打开一个终端,下面的操作都在该终端上完成

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

//-p参数是让正在运作中的容器先暂停等制作完镜像。-c后面跟上的命令是让该镜像作为容器启动时把httpd服务也启用
[root@localhost ~]# docker commit -p -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' eb139d53323c 15871816317/httpd:v1
sha256:a6a20aee6e453de386e3673850346a0a1c1d6faa2c0dc9db70778cededce99b8
//上面的sha256值用于甄别文件是否有被改动
[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
15871816317/httpd   v1      a6a20aee6e45   14 seconds ago   671MB

//将制作好的镜像上传至Docker Hub中
[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.7
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
[root@localhost ~]# docker logout		//退出登录
Removing login credentials for https://index.docker.io/v1/

测试镜像,部署家具商城网站

//启动一个容器。-d放入后台,--name指定容器名,--restart always让容器保持运行,-p映射端口,-v指定容器目录的挂载点
[root@localhost ~]# docker run -d --name web --restart always -p 80:80 -v /httpd_html:/usr/local/apache/htdocs 15871816317/httpd:v1
49ebd546d2399c91f2565258a56ad226462f49f8bdd42fefa028c8bbda7b2612
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED              STATUS              PORTS                               NAMES
47d9374bcbbe   15871816317/httpd:v1   "/usr/local/apache/b…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, :::80->80/tcp   web

//把源码放在httpd的网页内容存放目录
[root@localhost ~]# cd /httpd_html/
[root@localhost httpd_html]# ls
HTML5furniturestore.zip
[root@localhost httpd_html]# unzip HTML5furniturestore.zip
...........
[root@localhost httpd_html]# rm -f HTML5furniturestore.zip		//删除源码压缩包
[root@localhost httpd_html]# ls
51Aspx源码必读.txt  cart.html  fonts     images  index.html  product-details.html  style.css
blog.html           css        from.gif  img     js          shop.html            下载.url

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值