基于docker部署web站点
准备工作
安装三个软件包
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz daemon.json httpd-2.4.54.tar.gz
拉取镜像
[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
[root@localhost ~]# docker run -it --name yy1 centos /bin/bash
[root@adce88f2c411 /]# exit
exit
[root@localhost ~]# mkdir software 创建一个文件夹
[root@localhost ~]# mv *.gz software/ 把所有gz结尾的文件放到software目录
[root@localhost ~]# ls
anaconda-ks.cfg daemon.json software
把software目录复制到容器c1里面
[root@localhost ~]# docker cp software yy1:/usr/src/
配置yum源
[root@adce88f2c411 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 16633 0 --:--:-- --:--:-- --:--:-- 16523
[root@adce88f2c411 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@adce88f2c411 yum.repos.d]# ls
CentOS-Base.repo
安装依赖包
[root@adce88f2c411 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel gcc gcc-c++
创建apache系统用户
[root@adce88f2c411 ~]# useradd -rMs /sbin/nologin apache
[root@adce88f2c411 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
配置apr
[root@adce88f2c411 ~]# tar -xf apr-1.7.0.tar.gz -C /usr/local/src/
[root@adce88f2c411 ~]# cd /usr/local/src/apr-1.7.0/
[root@adce88f2c411 apr-1.7.0]# vi configure
$RM "$cfgfile" //注释或删除此行
[root@adce88f2c411 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@adce88f2c411 apr-1.7.0]# make && make install
配置apr-util
[root@adce88f2c411 ~]# tar -xf apr-util-1.6.1.tar.gz -C /usr/local/src/
[root@adce88f2c411 ~]# cd /usr/local/src/apr-util-1.6.1/
[root@adce88f2c411 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@adce88f2c411 apr-util-1.6.1]# make && make install
配置httpd
[root@adce88f2c411 ~]# tar -xf httpd-2.4.54.tar.gz -C /usr/local/src/
[root@adce88f2c411 ~]# cd /usr/local/src/httpd-2.4.54/
[root@adce88f2c411 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@adce88f2c411 httpd-2.4.54]# make && make install
配置环境变量
[root@adce88f2c411 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@adce88f2c411 ~]# source /etc/profile.d/apache.sh
映射头文件
[root@adce88f2c411 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
去除提示信息
[root@adce88f2c411 ~]# vi /usr/local/apache/conf/httpd.conf
ServerName www.example.com:80 //将此行取消注释
[root@adce88f2c411 ~]# 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"]' yy1 adce88f2c411/httpd:v0.1
sha256:e5a79a7315b1b29664a1269ba5620c137411338624abb984f19e8eb075d572ca
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
adce88f2c411/httpd v0.1 e5a79a7315b1 12 seconds ago 728MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]#
启动容器
[root@localhost ~]# docker run -d --name web -p 80:80 -v /root/html/:/usr/local/apache/htdocs adce88f2c411/httpd:v0.1
001dbff3e1f7f76f7fee3ab0f5b3aed3425f4e1f3d9a4bd6130e3b509480ed72
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
001dbff3e1f7 adce88f2c411/httpd:v0.1 "/usr/local/apache/b…" 6 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
adce88f2c411 centos "/bin/bash" 42 minutes ago Up 36 minutes yy1
[root@localhost ~]# echo "yyyz dada" > html/index.html
[root@localhost ~]# docker inspect adce88f2c411
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
[root@adce88f2c411 /]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
效果
导入源码
[root@localhost html]# pwd
/root/html
[root@localhost html]# ls
css
favicon.ico
images
index.html
js
vx.jpg
''$'\346\234\215\345\212\241\345\231\250\344\271\213\345\256\266''.url'
''$'\347\262\276\345\223\201\345\205\215\350\264\271\345\225\206\344\270\232\346\272\220\347\240\201\344\270\213\350\275\275''.url'
访问