8.30-使用docker容器部署考试项目+使用Dockerfile部署java项目
部署考试系统
一、在容器中部署nginx
1.编写docker脚本
[root@docker001 ~]# vim docker.sh
cat << EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat << EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
# 运行docker脚本
[root@docker001 ~]# source docker.sh
2.修改docker配置文件daemon.json
[root@docker001 ~]# 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"
]
}
3.修改docker.service文件
[root@docker001 ~]# vim /usr/lib/systemd/system/docker.service
13 ExecStart=/usr/bin/dockerd
[root@docker001 ~]# systemctl daemon-reload
4.重启docker服务
[root@docker001 ~]# systemctl restart docker
5.拉取nginx镜像
[root@docker001 ~]# docker pull nginx
6.查看镜像
[root@docker001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5ef79149e0ec 2 weeks ago 188MB
7.创建容器
# 创建容器
[root@docker001 ~]# docker run -itd -p80:80 nginx:latest
db8ffd17378657990222bb65accff54631c41e9251c844f1f95c8b66d44a0c7a
# 查看创建的容器
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8ffd173786 nginx:latest "/docker-entrypoint.…" 26 seconds ago Up 25 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp upbeat_stonebraker
# 访问测试
[root@docker001 ~]# 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>
# 使用exec命令查看容器nginx中的/usr/share/nginx/html目录
[root@docker001 ~]# docker exec -it db8 ls /usr/share/nginx/html
50x.html index.html
8.在容器启动之后,nginx服务就发布了
二、部署考试系统的前端
创建一个目录,不单独放置前端的资料,将后端的所有资料也都放在这里
1.创建目录
[root@docker001 ~]# mkdir project_exam_system
[root@docker001 ~]# cd project_exam_system/
[root@docker001 project_exam_system]# mkdir web
[root@docker001 project_exam_system]# cd web/
[root@docker001 web]# rz -E
rz waiting to receive.
2.将dist文件放到web目录下
# 解压dist.zip包
[root@docker001 web]# unzip dist.zip
# 查看解压后的文件
[root@docker001 web]# ls
dist dist.zip
# 删除dist.zip包
[root@docker001 web]# rm -rf dist.zip
# 只需要留下解压后的dist文件
[root@docker001 web]# ls
dist
3.查看web目录下dist文件中的内容
[root@docker001 web]# cd ..
[root@docker001 project_exam_system]# ls web/dist/
assets favicon.ico index.html
4.删除测试nginx页面所创建容器
# 停止db8容器的运行
[root@docker001 project_exam_system]# docker stop db8
db8
# 删除容器
[root@docker001 project_exam_system]# docker rm db8
db8
5.创建容器 ,映射端口为80,挂载/usr/share/nginx/html目录
[root@docker001 web]# docker run -itd -p80:80 -v /root/project_exam_system/web/dist/:/usr/share/nginx/html nginx:latest
11568e4a88646ea4b1cb9243678fe220ae263ea70591441f093dd72a7f4660a8
6.查看创建的容器
[root@docker001 web]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11568e4a8864 nginx:latest "/docker-entrypoint.…" 29 seconds ago Up 28 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
7.修改hosts文件
在物理机的C:\Windows\System32\drivers\etc中找到hosts文件,添加IP地址和域名
8.测试前端页面
浏览器访问:192.168.2.60
三、部署考试系统的后端
1.拉取centos镜像
[root@docker001 ~]# docker pull centos
2.将软件包拖到root下
#将软件包jdk-17_linux-x64_bin.tar.gz 和软件包project_exam_system.zip拖到root下
[root@docker001 ~]# ls
anaconda-ks.cfg docker.sh jdk-17_linux-x64_bin.tar.gz project_exam_system.zip
3.解压软件包project_exam_system.zip
# 解压project_exam_system.zip软件包
[root@docker001 ~]# unzip project_exam_system.zip
Archive: project_exam_system.zip
# 查看解压包后的文件
[root@docker001 ~]# ls
anaconda-ks.cfg docker.sh Project_ExamSystem-V1.0.0.war
application.properties jdk-17_linux-x64_bin.tar.gz project_exam_system.zip
dist project_exam_system.sql
4.使用centos镜像创建容器
# 使用centos镜像创建容器
[root@docker001 ~]# docker run -itd centos:latest /bin/bash
d107d28c1503012c6f9aa43d09c94d64f439c60de61d2c3c2d4d565690fdca0f
# 查看创建的容器
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d107d28c1503 centos:latest "/bin/bash" 12 seconds ago Up 11 seconds quirky_chandrasekhar
11568e4a8864 nginx:latest "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
5.将宿主机root目录下的微服务项目文件复制到用centos镜像创建的容器中
# 将宿主机的jdk包复制到容器中
[root@docker001 ~]# docker cp jdk-17_linux-x64_bin.tar.gz d107:/
Successfully copied 183MB to d107:/
# 将宿主机的Project包复制到容器中
[root@docker001 ~]# docker cp Project_ExamSystem-V1.0.0.war d107:/
Successfully copied 41MB to d107:/
# 将宿主机的application文件复制到容器中
[root@docker001 ~]# docker cp application.properties d107:/
Successfully copied 4.1kB to d107:/
6.挂载容器的终端
[root@docker001 ~]# docker attach d107
7.查看有没有将软件包和文件复制到容器中
[root@d107d28c1503 /]# ls
Project_ExamSystem-V1.0.0.war home media run usr
application.properties jdk-17_linux-x64_bin.tar.gz mnt sbin var
bin lib opt srv
dev lib64 proc sys
etc lost+found root tmp
8.在容器中创建目录,解压包
# 在容器中创建目录
[root@d107d28c1503 /]# mkdir /usr/local/jdk
# 解压软件包到指定目录,并且跳过第一层目录
[root@d107d28c1503 /]# tar -zxf jdk-17_linux-x64_bin.tar.gz -C /usr/local/jdk/ --strip-components=1
[root@d107d28c1503 /]# ls /usr/local/jdk/
LICENSE README bin conf include jmods legal lib man release
9.在容器中修改application.properties文件
[root@d107d28c1503 /]# vi application.properties
spring.datasource.url=jdbc:mysql://192.168.2.60:3306/project_exam_system
10.在容器中运行Project_ExamSystem-V1.0.0.war并访问验证
[root@d107d28c1503 /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.2)
2024-08-30 05:03:07.508 INFO 21 --- [ main] com.dn.es.ProjectExamSystemApplication : Starting ProjectExamSystemApplication vV1.0.0 using Java 17.0.12 with PID 21 (/Project_ExamSystem-V1.0.0.war started by root in /)
2024-08-30 05:03:07.511 INFO 21 --- [ main] com.dn.es.ProjectExamSystemApplication : No active profile set, falling back to 1 default profile: "default"
2024-08-30 05:03:09.435 INFO 21 --- [ main] o.s.d.r.config.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2024-08-30 05:03:09.440 INFO 21 --- [ main] o.s.d.r.config.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-08-30 05:03:09.497 INFO 21 --- [ main] o.s.d.r.config.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 31 ms. Found 0 Redis repository interfaces.
2024-08-30 05:03:11.178 INFO 21 --- [ main] o.s.boot.web.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2024-08-30 05:03:11.211 INFO 21 --- [ main] org.apache.coyote.http11.Http11NioProtocol : Initializing ProtocolHandler ["http-nio-8080"]
2024-08-30 05:03:11.213 INFO 21 --- [ main] org.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-08-30 05:03:11.213 INFO 21 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.26]
2024-08-30 05:03:11.623 INFO 21 --- [ main] o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-08-30 05:03:11.624 INFO 21 --- [ main] o.s.b.w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3998 ms
2024-08-30 05:03:14.976 INFO 21 --- [ main] org.apache.coyote.http11.Http11NioProtocol : Starting ProtocolHandler ["http-nio-8080"]
2024-08-30 05:03:15.732 INFO 21 --- [ main] o.s.boot.web.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2024-08-30 05:03:15.815 INFO 21 --- [ main] com.dn.es.ProjectExamSystemApplication : Started ProjectExamSystemApplication in 9.652 seconds (process running for 10.863)
read escape sequence
[root@docker001 ~]# curl 173.17.0.3:8080
{"code":20002,"msg":"账号不存在或密码错误"}[root@docker001 ~]#
11.使用容器创建java镜像
[root@docker001 ~]# docker commit d107 java:v0
sha256:64b4ea040bff2b7d3476273d62a5ae235fabe90e495ea81c6ac4efc6e33f2fd3
# 查看创建的java镜像
[root@docker001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 64b4ea040bff 10 seconds ago 774MB
<none> <none> 22eb536c0d76 54 minutes ago 774MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
centos latest 5d0da3dc9764 2 years ago 231MB
12.查看容器
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d107d28c1503 centos:latest "/bin/bash" 14 minutes ago Up 14 minutes quirky_chandrasekhar
11568e4a8864 nginx:latest "/docker-entrypoint.…" 17 minutes ago Up 17 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
13.删除使用centos镜像创建的容器
因为已经用这个容器导出java镜像了
# 停止容器
[root@docker001 ~]# docker stop d107
d107
# 删除容器
[root@docker001 ~]# docker rm d107
d107
14.使用java镜像创建容器
[root@docker001 ~]# docker run -itd -p8080:8080 java:v0
464d9b2b5f0b4a0cb80ac134128130393f67f7d0bf37c84763cdbf5a29015c1f
15.查看用java镜像创建的容器
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
464d9b2b5f0b java:v0 "/bin/bash" 9 seconds ago Up 8 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp epic_pascal
11568e4a8864 nginx:latest "/docker-entrypoint.…" 19 minutes ago Up 19 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
16.在创建的容器中运行
# 挂载创建的容器的终端
[root@docker001 ~]# docker attach 464d
# 运行
[root@464d9b2b5f0b /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
17.访问测试:
浏览器访问:192.168.2.60,出现200和500就是对的
四、数据库持久化-5.7.44
1.拉取mysql镜像
[root@docker001 ~]# docker pull mysql:5.7.44
2.使用mysql镜像创建容器
# 使用mysql镜像创建端口号为3306,用户和密码为root的容器
[root@docker001 ~]# docker run -itd -e MYSQL_ROOT_PASSWORD=root -p3306:3306 mysql:5.7.44
1a07feb046ee3038c2364c5b655ff4cfd31acf28db5e747810fc42b05195338d
# 查看创建的容器
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a07feb046ee mysql:5.7.44 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp optimistic_herschel
464d9b2b5f0b java:v0 "/bin/bash" 2 hours ago Up About an hour 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp epic_pascal
11568e4a8864 nginx:latest "/docker-entrypoint.…" 3 hours ago Up About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
3.在project_exam_system/目录下创建mysql目录
[root@docker001 ~]# mkdir mysql
[root@docker001 ~]# mv mysql project_exam_system/
[root@docker001 ~]# ls project_exam_system/
mysql web
4.远程登录mysql运行project_exam_system.sql文件
5.使用exec命令登录mysql
[root@docker001 ~]# docker exec -it 1a mysql -uroot -proot
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 5
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, 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> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| mysql |
| performance_schema |
| project_exam_system |
| sys |
+---------------------+
5 rows in set (0.00 sec)
# 创建和java项目中的用户
mysql> create user 'zhangmin'@'%' identified by 'zhangmin';
Query OK, 0 rows affected (0.00 sec)
# 给创建的用户所有的权限
mysql> grant all on *.* to 'zhangmin'@'%';
Query OK, 0 rows affected (0.00 sec)
# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
6.在远程工具中查看用户和密码
7.进行用户登录
8.进入考试系统
五、实现镜像的自动创建
1.查看root下的文件
[root@docker001 ~]# ls
anaconda-ks.cfg jdk-17_linux-x64_bin.tar.gz Project_ExamSystem-V1.0.0.war
application.properties project_exam_system project_exam_system.zip
docker.sh project_exam_system.sql.0
2.创建project_exam_system/java目录
[root@docker001 ~]# mkdir project_exam_system/java
3.将文件拷贝到project_exam_system/java/目录下
# 将root目录下application.properties文件拷贝到project_exam_system/java/目录下
[root@docker001 ~]# cp application.properties project_exam_system/java/
# 将root目录下jdk包拷贝到project_exam_system/java/目录下
[root@docker001 ~]# cp jdk-17_linux-x64_bin.tar.gz project_exam_system/java/
# 将root目录下Project_ExamSystem-V1.0.0.war包拷贝到project_exam_system/java/目录下
[root@docker001 ~]# cp Project_ExamSystem-V1.0.0.war project_exam_system/java/
4.解压jdk软件包
[root@docker001 java]# tar -zxvf jdk-17_linux-x64_bin.tar.gz
5.查看解压后的文件
[root@docker001 java]# ls
application.properties jdk-17.0.12 Project_ExamSystem-V1.0.0.war
Dockerfile jdk-17_linux-x64_bin.tar.gz
6.给解压后的jdk-17.0.12文件改名为jdk
[root@docker001 java]# mv jdk-17.0.12/ jdk
7.查看改名后的文件
[root@docker001 java]# ls
application.properties jdk Project_ExamSystem-V1.0.0.war
Dockerfile jdk-17_linux-x64_bin.tar.gz
8.编写start.sh文件
[root@docker001 java]# vim start.sh
# !/bin/bash
/usr/local/jdk/bin/java -jar /Project_ExamSystem-V1.0.0.war
9.编辑Dockerfile文件
[root@docker001 java]# vim Dockerfile
FROM centos:latest
ADD application.properties /application.properties
ADD jdk/ /usr/local/jdk/
ADD Project_ExamSystem-V1.0.0.war /Project_ExamSystem-V1.0.0.war
ADD start.sh /start.sh
RUN chmod -v +x start.sh
WORKDIR /
EXPOSE 8080
CMD [ "/start.sh","/bin/bash" ]
10.创建一个名为java:v1镜像
[root@docker001 java]# docker build -t java:v1 .
[+] Building 0.4s (11/11) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 369B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/7] FROM docker.io/library/centos:latest 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 54.75kB 0.0s
=> CACHED [2/7] ADD application.properties /application.properties 0.0s
=> CACHED [3/7] ADD jdk/ /usr/local/jdk/ 0.0s
=> CACHED [4/7] ADD Project_ExamSystem-V1.0.0.war /Project_ExamSystem-V1.0.0.w 0.0s
=> [5/7] ADD start.sh /start.sh 0.0s
=> [6/7] RUN chmod -v +x start.sh 0.2s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:1afee7532f08bf1bb19320165a076f4293abee9094e4c164317 0.0s
=> => naming to docker.io/library/java:v1 0.0s
11.使用java:v1创建一个容器,映射端口为8081
[root@docker001 java]# docker run -itd -p8081:8080 java:v1
1aa141a489fbe18838c0d56c316446faa807eeb7da152a64203eb73e1ab15f1e
12.查看创建的容器
[root@docker001 java]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1aa141a489fb java:v1 "/start.sh /bin/bash" 5 seconds ago Up 5 seconds 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp jolly_buck
1a07feb046ee mysql:5.7.44 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp optimistic_herschel
464d9b2b5f0b java:v0 "/bin/bash" 3 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp epic_pascal
11568e4a8864 nginx:latest "/docker-entrypoint.…" 4 hours ago Up 2 hours 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
13.查看容器是否开启服务
# 使用exec命令查看容器内开启的端口
[root@docker001 java]# docker exec 1aa141 ss -tuln|grep 8080
tcp LISTEN 0 128 *:8080 *:*
14.访问测试
[root@docker001 java]# curl localhost:8081
{"code":20002,"msg":"账号不存在或密码错误"}
15.在宿主机上检查有没有开启服务
[root@docker001 java]# netstat -lnput|grep 8081
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 20459/docker-proxy
tcp6 0 0 :::8081 :::* LISTEN 20463/docker-proxy