回顾
1
、使用
harbor
仓库
python
--version
yum
-y
update
yum
-y
install python2-pip
pip install
--upgrade
pip
==
20
.3
-i
.......
pip install docker-compose
-i
.......
sh
docker.sh
解压
harbor
vim
harbor.yml
./prepare
./installed
使用
nginx
发布
web
服务
使用
registry
管理
2
、
docker-compose
的编排
在学习
kubernetes
之前的热身
通过运行一个
yum
管理
docker
容器
docker-compose.yml
vim
docker-compose.yml
version:
"3"
services:
nginx:
contanner_name: name0
image: nginx:latest
ports:
-
"80:80"
volums:
-
/opt/web/:/usr/share/nginx/html/
一、项目
-
使用
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_ExamSystem
V1.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
#
修改连接后端数据库的
IP
地址
[root@docker java]
# vim application.properties
spring.datasource
.url
=
jdbc:mysql://10.0.0.7:3306/project_exam_system
2
)在
java
目录下编辑
Dockerfile
文件
[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
WORKDIR /
EXPOSE
8080
CMD [
"/usr/local/jdk/bin/java"
,
"-jar"
,
"Project_ExamSystem-V1.0.0.war"
]
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
))
4
)使用
Dockerfile
文件创建
nginx
镜像
[root@docker web]
# ls
dist Dockerfile
[root@docker web]
# vim Dockerfile
FROM nginx:latest
ADD dist/ /usr/share/nginx/html/
EXPOSE
80
CMD [
"nginx"
,
"-g"
,
"daemon off;"
]
[root@docker web]
# docker run -itd --name n0 -p80:80 nginx:v1
5
)使用
Dockerfile
文件创建
mysql
镜像
[root@docker mysql]
# ls
Dockerfile project_exam_system.sql
[root@docker mysql]
# vim project_exam_system.sql
最后加入
CREATE USER
'zhangmin'
@
'%'
IDENTIFIED BY
'zhangmin'
;
GRANT ALL PRIVILEGES ON *.* TO
'zhangmin'
@
'%'
WITH GRANT OPTION;
flush privileges;
[root@docker mysql]
# vim Dockerfile
FROM mysql:5.7.44
ENV
MYSQL_ROOT_PASSWORD
=
root
ADD project_exam_system.sql /docker-entrypoint-initdb.d/
EXPOSE
3306
[root@docker mysql]
# docker run -itd --name m0 -p3306:3306 mysql:v1