参考:docker 构建nginx服务
环境
Redhat 9
步骤:
1、docker部署MySQL
安装yum 工具包
[root@admin ~]# yum -y install yum-utils.noarch
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
AppStream 3.1 MB/s | 3.2 kB 00:00
BaseOS 756 kB/s | 2.7 kB 00:00
依赖关系解决。
=====================================================================================================================================================================================
软件包 架构 版本 仓库 大小
=====================================================================================================================================================================================
安装:
yum-utils noarch 4.3.0-5.el9_2 BaseOS 45 k
事务概要
=====================================================================================================================================================================================
安装 1 软件包
总计:45 k
安装大小:23 k
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
安装 : yum-utils-4.3.0-5.el9_2.noarch 1/1
运行脚本: yum-utils-4.3.0-5.el9_2.noarch 1/1
验证 : yum-utils-4.3.0-5.el9_2.noarch 1/1
已更新安装的产品。
已安装:
yum-utils-4.3.0-5.el9_2.noarch
完毕!
[root@admin ~]#
增加阿里云仓库 并去除缓存
[root@admin ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
添加仓库自:http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@admin ~]# yum clean all && yum makecache
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
13 个文件已删除
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
Docker CE Stable - x86_64 82 kB/s | 41 kB 00:00
AppStream 85 MB/s | 6.3 MB 00:00
BaseOS 75 MB/s | 1.7 MB 00:00
元数据缓存已建立。
[root@admin ~]#
安装docker
root@admin ~]# yum -y install docker-ce*
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
上次元数据过期检查:0:00:43 前,执行于 2024年04月02日 星期二 15时40分19秒。
依赖关系解决。
=============================================
省略。。。。。。。。
配置docker镜像加速(直接复制粘贴即可)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://7f40piyw.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
拉取mysql 镜像文件
root@admin ~]# systemctl restart docker.service
[root@admin ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@admin ~]#
创建mysql数据卷目录
[root@admin ~]# mkdir /docker_mysql/data -p
[root@admin ~]#
创建容器拷⻉配置⽂件⽬录到/docker_mysql⽬录 (由于数据卷目录没有MySQL所需的配置文件所以需要此步)
[root@admin ~]# docker run -id --name mysql -e mysql_root_password=1 mysql:5.7
4ebc146b1082d5c814476d6c76ccff11f893f1279e071840f1b9482018cc07e6
[root@admin ~]# docker cp mysql:/etc/mysql/ /docker_mysql/
Successfully copied 12.3kB to /docker_mysql/
[root@admin ~]#
删除容器
[root@admin ~]# docker rm mysql
mysql
[root@admin ~]#
[root@admin ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@admin ~]#
创建MySQL容器并挂载数据卷
[root@admin ~]# docker run -id --name msql -p 3306:3306 -v /docker_mysql/mysql/:/etc/mysql -v /docker_mysql/data/mysql:/var/lib/mysql -v /docker_mysql/logs:/var/log/mysql -e MYSQL_ROOT_PASSWORD=1 mysql:5.7
138fe053e936f66787adf2c8242122ea1283d2e00e1647ea0da78d5b0c5227ca
[root@admin ~]#
进入数据库测试
[root@admin ~]# docker exec -it msql /bin/bash
root@138fe053e936:/# mysql -uroot -p1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
2、docker部署nginx
拉取镜像
[root@admin ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@admin ~]#
创建数据卷目录
[root@admin ~]# mkdir -p /docker_nginx/html
[root@admin ~]# mkdir -p /docker_nginx/logs
[root@admin ~]#
拷贝nginx配置文件
[root@admin ~]# docker run -id --name nginx nginx:latest
576c4841c72b52696d991905d4bb357a9ab5e69257d4b2c0014213f155e5b126
[root@admin ~]# docker cp nginx:/etc/nginx /docker_nginx/
Successfully copied 16.9kB to /docker_nginx/
[root@admin ~]# cd /d
dev/ docker_mysql/ docker_nginx/
[root@admin ~]# cd /docker_nginx/
[root@admin docker_nginx]# ls
html logs nginx
[root@admin docker_nginx]#
删除容器
[root@admin ~]# docker stop nginx
nginx
[root@admin ~]# docker rm nginx
nginx
[root@admin ~]#
创建nginx容器并挂载数据卷
[root@admin ~]# docker run -id --name nginx -p 80:80 -v /docker_nginx/nginx/:/etc/nginx -v /docker_nginx/html:/usr/share/nginx/html -v /docker_nginx/logs/:/var/log/nginx nginx
curl命令测试
[root@admin ~]# curl 192.168.200.133
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
[root@admin ~]#
发现403错误(没有index.html文件)
检查配置文件没有index.html文件
进入nginx/html下增加文件
[root@admin html]# ls
[root@admin html]# vim index.html
[root@admin html]# cat index.html
sadggh1k
[root@admin html]#
curl命令测试
[root@admin html]# curl 192.168.200.133
sadggh1k
[root@admin html]
3、docker部署tomcat
拉取tomcat镜像文件
[root@admin html]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
0e29546d541c: Pull complete
9b829c73b52b: Pull complete
cb5b7ae36172: Pull complete
6494e4811622: Downloading [========================================> ] 44.38MB/54.57MB
6494e4811622: Downloading [=========================================> ] 44.91MB/54.57MB
6494e4811622: Downloading [============================================> ] 48.61MB/54.57MB
8f7c0eebb7b1: Downloading [========> ] 34.34MB/203.1MB
6494e4811622: Downloading [=============================================> ] 49.14MB/54.57MB
6494e4811622: Downloading [=============================================> ] 49.67MB/54.57MB
6494e4811622: Pull complete
668f6fcc5fa5: Pull complete
dc120c3e0290: Pull complete
8f7c0eebb7b1: Pull complete
77b694f83996: Pull complete
0f611256ec3a: Pull complete
4f25def12f23: Pull complete
Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
创建数据卷目录
[root@admin ~]# mkdir /tomcat
[root@admin ~]#
创建容器拷贝配置文件
[root@admin ~]# docker run -id --name=tomcat tomcat
/bin/bash
049add93e21773d1175e9de2c5aecbde99069bc552ea5e58fe43486dd7dc19af
[root@admin ~]#
[root@admin ~]# docker cp tomcat:/usr/local/tomcat /tomcat/
Successfully copied 19.3MB to /tomcat/
[root@admin ~]# cd /tomcat/
[root@admin tomcat]# ls
tomcat
[root@admin tomcat]#
删除容器
[root@admin tomcat]# docker rm tomcat
tomcat
创建容器挂载数据卷
[root@admin ~]# docker run -id --name=tomcat -p 8080:8080 -v /tomcat/tomcat:/usr/local/tomcat tomcat
e91774eaac35ff405287d52876cd59779f2bcab577e4550b26591eaa013fd124
docker: Error response from daemon: driver failed programming external connectivity on endpoint tomcat (b69726032861fe417817221d9f3a175fa2df831efbc849e9367fc6c3158c22d2): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8080 -j DNAT --to-destination 172.17.0.4:8080 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)).
报出错误重启docker即可
[root@admin ~]# systemctl restart docker.service
[root@admin ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@admin ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e91774eaac35 tomcat "catalina.sh run" 3 minutes ago Created tomcat
31405e9d01c0 nginx "/docker-entrypoint...." 20 minutes ago Exited (0) 13 seconds ago nginx
138fe053e936 mysql:5.7 "docker-entrypoint.s..." 28 minutes ago Exited (0) 11 seconds ago msql
[root@admin ~]# docker start tomcat
tomcat
创建tomcat网页文件
[root@admin ~]# mkdir /tomcat/tomcat/webapps/ROOT
[root@admin ~]# cd /tomcat/tomcat/webapps/ROOT
[root@admin ROOT]# vim index.html
[root@admin ROOT]# cat index.html
sagdshgk1
[root@admin ROOT]#
测试
[root@admin ~]# curl 192.168.200.133:8080
sagdshgk1
[root@admin ~]#