一、项目-使用docker部署project-exam-system
1、背景
在一台主机之内,实现容器的编排,发布考试系统
2、环境准备
# 部署docker环境
[root@docker ~]# sh docker.sh
[root@docker ~]# vim /etc/docker/daemon.json
{
"registry-mirrors" : [
"https://do.nark.eu.org",
"https://dc.j8.work",
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn"
],
"host" : [
"tcp://0.0.0.0:2375",
"unix:///var/run/docker.sock"
],
"insecure-registries" : [
"http://10.0.0.7:5000"
]
}
[root@docker ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd
[root@docker ~]# systemctl daemon-reload
[root@docker ~]# systemctl start docker
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
3、前端配置
前端使用nginx服务
1)测试:容器搭建nginx服务
# 拉取nginx镜像
[root@docker ~]# docker pull nginx
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5ef79149e0ec 2 weeks ago 188MB
# 创建一个容器测试一下nginx服务
[root@docker ~]# docker run -itd -p80:80 nginx:latest
fc8a6e06f77d877c8341c8a5aed3ec50fa448918bc672b2e330db8060b947a57
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
fc8a6e06f77d nginx:latest "/docker-entrypoint.…" 10 seconds ago Up 9
seconds 0.0.0.0:80->80/tcp, :::80->80/tcp busy_mccarthy
[root@docker ~]# docker exec -it fc8a ls /usr/share/nginx/html
50x.html index.html
# 在容器启动后,nginx服务就自动发布了
[root@docker ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
2)上传前端项目映射到容器中
# 上传前端项目dist
# 创建一个目录,不单独放置前端的资料,将后端的所有资料也都放在这里
[root@docker ~]# mkdir project_exam_system
[root@docker ~]# cd project_exam_system/
[root@docker project_exam_system]# mkdir web
[root@docker ~]# unzip project_exam_system.zip
[root@docker ~]# mv dist project_exam_system/web/
[root@docker ~]# cd project_exam_system
[root@docker project_exam_system]# ls
web
[root@docker project_exam_system]# cd web/
[root@docker web]# ls
dist
# 将测试容器删除
[root@docker web]# docker rm -f fc8
fc8
# 再创建容器,映射端口,挂载目录
[root@docker project_exam_system]# docker run -itd -p80:80 -v
/root/project_exam_system/web/dist/:/usr/share/nginx/html/ nginx:latest
98646a277c8bd126a3fd39453707b5ab66a8869be963e3680653610c991e46af
[root@docker project_exam_system]# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
98646a277c8b nginx:latest "/docker-entrypoint.…" 9 seconds ago Up 9
seconds 0.0.0.0:80->80/tcp, :::80->80/tcp unruffled_gauss
3 ) 结 果 展 示
出 现 问 题 , 在 实 现 登 录 时 , 出 现 后 端 异 常 , 原 因 是 后 端 还 没 有 配 置
从上面图片可以看出来,请求的域名为bu.yuanyu.zhangmin,现在我们需要将域名劫持下来,将该 域名指向我们的linux主机,在linux主机中使用容器搭建后端服务器,所以要在本机的hosts文件中写入 域名的对应关系,使其指向linux主机
配置完成后,本机能够ping通bu.yuanyu.zhangmin域名并由10.0.0.7主机回应即配置成功
4、后端配置
后端的业务springboot
优点:不用单独部署tomcat,已经在springboot中内置tomcat
1)测试:在centos容器中配置java环境,修改数据库地址并运行war包
# 拉取基础centos镜像
[root@docker ~]# docker pull centos
# 创建centos容器
[root@docker ~]# docker run -it centos:latest /bin/bash
[root@d34e6c6d34d7 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@d34e6c6d34d7 /]# [root@docker ~]#
# 试图使用scp将服务器上的war包上传给容器中去,发现上传不了
[root@docker ~]# scp Project_ExamSystem-V1.0.0.war root@172.17.0.3:~
ssh: connect to host 172.17.0.3 port 22: Connection refused
lost connection
# 查看docker帮助文档
[root@docker ~]# docker --help
cp Copy files/folders between a container and the local filesystem
# 查看docker cp帮助文档
[root@docker ~]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
d34e6c6d34d7 centos:latest "/bin/bash" 3 minutes ago Up
3 minutes gifted_brown
98646a277c8b nginx:latest "/docker-entrypoint.…" 31 minutes ago Up
31 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp unruffled_gauss
# 使用cp命令将服务器中的war包、jdk包、application.properties文件上传到容器中
# 需要jdk17版本
[root@docker ~]# docker cp Project_ExamSystem-V1.0.0.war d34e:/
Successfully copied 41MB to d34e:/
[root@docker ~]# docker cp jdk-17_linux-x64_bin.tar.gz d34e:/
Successfully copied 183MB to d34e:/
[root@docker ~]# docker cp application.properties d34e:/
Successfully copied 4.1kB to d34e:/
# 进入到容器中
# 对已经传到容器的文件进行部署
[root@docker ~]# docker attach d34e
# 查看,是否上传成功
[root@d34e6c6d34d7 /]# ls
Project_ExamSystem-V1.0.0.war dev jdk-17_linux-x64_bin.tar.gz lost+found
opt run sys var
application.properties etc lib media proc sbin
tmp
bin home lib64 mnt root srv usr
# 创建jdk目录
[root@d34e6c6d34d7 /]# mkdir /usr/local/jdk
# 将jdk包解压到jdk目录下
[root@d34e6c6d34d7 /]# tar -zxf jdk-17_linux-x64_bin.tar.gz -C
/usr/local/jdk/ --strip-components=1
# 查看解压是否成功
[root@d34e6c6d34d7 /]# ls /usr/local/jdk/
LICENSE README bin conf include jmods legal lib man release
# 修改application.properties文件内容,指定数据库位置
[root@d34e6c6d34d7 /]# vi application.properties
spring.datasource.url=jdbc:mysql://10.0.0.7:3306/project_exam_system
# 使用java命令运行war包
[root@d34e6c6d34d7 /]# /usr/local/jdk/bin/java -jar Project_ExamSystemV1.0.0.war
: Started ProjectExamSystemApplication in 16.425 seconds (process
running for 18.114)
# ctrl p q 退出容器
read escape sequence
# 测试war包是否正常运行
# 以下显示表示正常
[root@docker ~]# curl 172.17.0.3:8080
{"code":20002,"msg":"账号不存在或密码错误"}[root@docker ~]#
2)将centos容器封装为java镜像并创建java容器
# 查看commit帮助文档
[root@docker ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
# 根据容器创建镜像
[root@docker ~]# docker commit d34 java:v0
sha256:1d715d202236751037fc5f67084c2e437e4aeb3159f5a9e74017d8f555e1767d
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 1d715d202236 31 seconds ago 774MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
centos latest 5d0da3dc9764 2 years ago 231MB
# 停止centos容器
[root@docker ~]# docker stop d34
d34
# 移除centos容器
[root@docker ~]# docker rm d34
d34
# 根据新建的镜像,创建容器
[root@docker ~]# docker run -itd -p8080:8080 java:v0 /usr/local/jdk/bin/java
-jar Project_ExamSystem-V1.0.0.war
9337c1beb386d63b6c9ddc3bf25c1830af16505e6e29844a4599a10b2888f16c
# 查看容器的进程
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
9337c1beb386 java:v0 "/usr/local/jdk/bin/…" 4 seconds ago Up
3 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp boring_einstein
98646a277c8b nginx:latest "/docker-entrypoint.…" About an hour ago Up
About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp unruffled_gauss
3)结果展示
浏览器访问,出现500错误,说明java项目部署不对,主要是数据库未导入数据。
5、数据持久化业务 mysql-5.7.44
1)配置mysql容器并导入sql数据
# 拉取mysql:5.7.44
[root@docker ~]# docker pull mysql:5.7.44
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 1d715d202236 3 hours ago 774MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
mysql 5.7.44 5107333e08a8 8 months ago 501MB
centos latest 5d0da3dc9764 2 years ago 231MB
# 创建mysql容器
[root@docker ~]# docker run -itd -e MYSQL_ROOT_PASSWORD=root -p3306:3306
mysql:5.7.44
9f30dfb770753a5b50872f29d961da87e41e7dcdfcd3e9e187193a8384cd2990
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
9f30dfb77075 mysql:5.7.44 "docker-entrypoint.s…" 13 seconds ago Up 11
seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
heuristic_varahamihira
9337c1beb386 java:v0 "/usr/local/jdk/bin/…" 3 hours ago Up 3
hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
boring_einstein
98646a277c8b nginx:latest "/docker-entrypoint.…" 4 hours ago Up 4
hours 0.0.0.0:80->80/tcp, :::80->80/tcp
unruffled_gauss
# 创建mysql目录
[root@docker ~]# mkdir mysql
# 将sql文件移动到mysql目录下
[root@docker ~]# mv project_exam_system.sql project_exam_system/mysql/
# 使用Navicat Premium应用软件将sql文件中的数据导入到数据库中
2)进入数据库中创建指定用户并为用户授权
# 进入数据库中查看数据
[root@docker project_exam_system]# docker exec -it 9f30 mysql -uroot -proot
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| mysql |
| performance_schema |
| project_exam_system |
| sys |
+---------------------+
5 rows in set (0.01 sec)
mysql> use project_exam_system
mysql> show tables;
+-------------------------------+
| Tables_in_project_exam_system |
+-------------------------------+
| admin |
| category |
| com_city |
| com_nation |
| course |
| dept |
| dept_student |
| exam |
| exam_dept |
| exam_process |
| loginlog |
| major |
| menu |
| paper |
| paper_topic |
| role |
| role_admin |
| role_menu |
| semester |
| student |
| teacher |
| topic |
| user |
+-------------------------------+
23 rows in set (0.00 sec)
mysql> select * from user;
+-----+--------------+----------+--------+---------------------+----------+--
-------------------+--------+-------+
| id | userName | password | status | createTime | createOr |
editTime | editOr | isDel |
+-----+--------------+----------+--------+---------------------+----------+--
-------------------+--------+-------+
| 1 | 21001189 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-22 22:04:44 | NULL | 0 |
| 2 | 21001190 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-21 09:45:05 | NULL | 0 |
| 3 | 21001191 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-21 09:45:05 | NULL | 0 |
| 4 | 21001192 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-21 09:45:05 | NULL | 0 |
| 5 | 21001193 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-21 09:45:05 | NULL | 0 |
| 6 | 21001194 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-21 09:45:05 | NULL | 0 |
| 7 | 21001195 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-21 09:45:05 | NULL | 0 |
| 8 | 21001196 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-21 09:45:05 | NULL | 0 |
| 9 | 21001197 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-21 09:45:05 | NULL | 0 |
| 10 | 21001198 | 111111 | 0 | 2024-08-21 09:45:05 | NULL |
2024-08-21 09:45:05 | NULL | 0 |
# 由于连接数据库使用的用户是zhangsan,所以我们需要创建zhangsan用户并授权
mysql> create user 'zhangmin'@'%' identified by 'zhangmin';
Query OK, 0 rows affected (0.09 sec)
mysql> grant all on *.* to 'zhangmin'@'%';
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)
3)结果展示
6、优化(使用Dockerfile直接制作java镜像)
1)创建java目录,将所需文件移动到java目录下
[root@docker ~]# mkdir project_exam_system/java
[root@docker ~]# mv jdk-17_linux-x64_bin.tar.gz project_exam_system/java/
[root@docker ~]# mv application.properties project_exam_system/java/
[root@docker ~]# mv Project_ExamSystem-V1.0.0.war project_exam_system/java/
[root@docker ~]# cd project_exam_system/java/
[root@docker java]# tar -zxf jdk-17_linux-x64_bin.tar.gz
[root@docker java]# mv jdk-17.0.12/ jdk
2)在java目录下编辑Dockerfile文件和java.sh脚本
[root@docker java]# vim java.sh
#!/bin/bash
/usr/local/jdk/bin/java -jar /Project_ExamSystem-V1.0.0.war
[root@docker java]# vim Dockerfile
FROM centos:latest
ADD application.properties /application.properties
ADD Project_ExamSystem-V1.0.0.war /Project_ExamSystem-V1.0.0.war
ADD jdk /usr/local/jdk
ADD java.sh /java.sh
WORKDIR /
EXPOSE 8080
RUN chmod +x /java.sh
CMD ["/bin/bash","/java.sh"]
3)使用Dockerfile文件创建java镜像并创建一个java容器测试
[root@docker java]# docker build -t java:v2 .
[+] Building 3.1s (11/11) FINISHED
# 发现java:v2比java:v1小很多,由于使用的是centos镜像为基础镜像是关闭状态一层一层封装为
java镜像的
[root@docker java]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v2 d06359c88e57 23 minutes ago 591MB
java v0 1d715d202236 6 hours ago 774MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
mysql 5.7.44 5107333e08a8 8 months ago 501MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@docker java]# docker run -itd -p8081:8080 java:v2
3bebbb51648947c5893e27da41540fab92d956812bf8def0595c46881dd54e72
[root@docker java]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
3bebbb516489 java:v2 "/bin/bash /java.sh" 10 seconds ago Up 8
seconds 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp
nervous_zhukovsky
9f30dfb77075 mysql:5.7.44 "docker-entrypoint.s…" 2 hours ago Up 2
hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
heuristic_varahamihira
9337c1beb386 java:v0 "/usr/local/jdk/bin/…" 5 hours ago Up 5
hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
boring_einstein
98646a277c8b nginx:latest "/docker-entrypoint.…" 6 hours ago Up 6
hours 0.0.0.0:80->80/tcp, :::80->80/tcp
unruffled_gauss
[root@docker java]# curl 10.0.0.7:8081
{"code":20002,"msg":"账号不存在或密码错误"}[root@docker java]#
[root@docker java]# docker exec -it 3b ss -tupln | grep 8080
tcp LISTEN 0 128 *:8080 *:* users:
(("java",pid=7,fd=9))