1.前端(nginx)
[root@localhost ~]# docker pull nginx //拉取nginx镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5ef79149e0ec 2 weeks ago 188MB
[root@localhost ~]# docker run -itd -p80:80 nginx:latest //创建nginx容器在后台运行
[root@localhost ~]# docker exec d18c ls /usr/share/nginx/html
50x.html
index.html
[root@localhost ~]# mkdir project_exam_system
[root@localhost ~]# cd project_exam_system/
[root@localhost project_exam_system]# mkdir web
[root@localhost web]# ls //上传自己的前端项目
dist
[root@localhost web]# docker run -itd -p80:80 -v /root/project_exam_system/web/dist/:/usr/share/nginx/html nginx:latest //创建容器并挂载前端项目
3035a713b57eaef83c979c7a4e04416d309cc52a3e5b61933bd3e058d951152b
浏览器访问本机ip
做个hosts域名劫持,就暂时不做dns了
windows系统在这个目录下:"C:\Windows\System32\drivers\etc\hosts"
添加下面这一行内容
192.168.1.23 bu.yuanyu.zhangmin
浏览器访问:bu.yuanyu.zhangmin
2.部署后端业务(jdk)
上传项目的其他部分以及jdk17
[root@localhost ~]# ls
anaconda-ks.cfg harbor-offline-installer-v2.11.1.tgz project_exam_system.sql
application.properties jdk-17_linux-x64_bin.tar.gz Project_ExamSystem-V1.0.0.war
docker.sh project_exam_system
[root@localhost ~]# docker pull centos //拉取
[root@localhost ~]# docker run -itd centos:latest //创建容器
aa6a96f74ed438bfe561bd9b7aed494a46bb2a189edff44573171dd2215a6738
[root@localhost ~]# docker cp jdk-17_linux-x64_bin.tar.gz aa6a:/ //把准备好的jdk17包传进容器中
Successfully copied 183MB to aa6a:/
[root@localhost ~]# docker cp application.properties aa6a:/ //把项目的其他部分上传到该容器中
Successfully copied 4.1kB to aa6a:/
[root@localhost ~]# docker cp Project_ExamSystem-V1.0.0.war aa6a:/
Successfully copied 41MB to aa6a:/
[root@localhost ~]# docker attach aa6a //进入容器
[root@aa6a96f74ed4 /]# 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
[root@aa6a96f74ed4 /]# mkdir /usr/local/jdk
[root@aa6a96f74ed4 /]# tar -zxvf jdk-17_linux-x64_bin.tar.gz -C /usr/local/jdk/ --strip-components=1 //--strip-components=1忽略归档中文件路径的第一个目录。
[root@aa6a96f74ed4 /]# ls /usr/local/jdk
LICENSE README bin conf include jmods legal lib man release
[root@aa6a96f74ed4 /]# vi application.properties //修改application.properties文件
[root@aa6a96f74ed4 /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war //启动服务
ctrl+p+q退出
[root@localhost ~]# curl 172.17.0.3:8080 //访问
{"code":20002,"msg":"账号不存在或密码错误"}[root@localhost ~]# //这样代表部署成功
[root@localhost ~]# docker commit aa6a java:v0 //使用docker commit将容器做成镜像可以保留历史记录
sha256:46529fd5a4b2cf6affb2cd559987423a29f533c2e6d42bd3f3739dc641ac7cfa
[root@localhost ~]# docker images //查看
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 46529fd5a4b2 44 seconds ago 774MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@localhost ~]# docker stop aa6a //停止刚才创建的容器
aa6a
[root@localhost ~]# docker rm aa6a //删除
aa6a
[root@localhost ~]# docker run -itd -p8080:8080 java:v0 /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war //使用刚刚创建的镜像创建容器映射端口并启动项目
[root@localhost ~]# docker ps --all //这里看到启动成功了
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a425c7feff30 java:v0 "/usr/local/jdk/bin/…" 27 seconds ago Up 25 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp upbeat_lamarr
3035a713b57e nginx:latest "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp compassionate_mayer
[root@localhost ~]# curl 192.168.1.23:8080
{"code":20002,"msg":"账号不存在或密码错误"}[root@localhost ~]#
本机浏览器访问:192.168.1.23
![](https://i-blog.csdnimg.cn/direct/e1e4f2c2184c4afb803b00743ab4460f.png)
3.mysql
[root@localhost ~]# docker pull mysql:5.7.44 //拉取mysql镜像
[root@localhost ~]# docker run -itd -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 mysql:5.7.44 //创建容器
[root@localhost ~]# cd project_exam_system/
[root@localhost project_exam_system]# mkdir mysql //创建mysql目录
[root@localhost project_exam_system]# mv /root/project_exam_system.sql mysql/ //把sql文件放进去
[root@localhost project_exam_system]# mkdir java //创建Java目录,把关于java的文件也移进去,方便管理
[root@localhost project_exam_system]# mv /root/application.properties java/
[root@localhost project_exam_system]# mv /root/jdk-17_linux-x64_bin.tar.gz java/
[root@localhost project_exam_system]# mv /root/Project_ExamSystem-V1.0.0.war java/
[root@localhost project_exam_system]# cd
[root@localhost ~]# docker cp project_exam_system/mysql/project_exam_system.sql b3cd:/ //上传sql文件
Successfully copied 11MB to b3cd:/
[root@localhost ~]# docker exec -it b3cd /bin/bash //进入容器
bash-4.2# ls
bin docker-entrypoint-initdb.d home media proc run sys var
boot entrypoint.sh lib mnt project_exam_system.sql sbin tmp
dev etc lib64 opt root srv usr
bash-4.2# mysql -uroot -proot < project_exam_system.sql //运行sql文件
bash-4.2# mysql -uroot -proot
mysql> show databases; //这里看到项目的数据库已经创建成功
+---------------------+
| Database |
+---------------------+
| information_schema |
| mysql |
| performance_schema |
| project_exam_system |
| sys |
+---------------------+
5 rows in set (0.00 sec)
mysql> create user 'zz'@'%' identified by 'zz'; //创建后端管理账号
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to 'zz'@'%'; //给权限
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; //刷新
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
bash-4.2# read escape sequence
现在就可以登陆了
访问bu.yuanyu.zhangmin,在数据库中随便找个学生的账号密码
登陆管理员页面:账号密码都是admin