容器化Docker部署Java后端+Vue前端+MySQL数据库单机场景
资源规划
网段:192.168.0.0/16
DB:MySQL8/0
缓存:Redis
后端:Java开源项目
后端:Vue2.x
Web服务器:Nginx
一、创建容器POD网段
[root@pxc-node1 ~]# docker network create --subnet 192.168.0.0/16 alonenet
27311d6441e8467a0e4bb8b222baf8b60c12ee3610a057e90302aac5b8fd024b
[root@pxc-node1 ~]# docker network list
NETWORK ID NAME DRIVER SCOPE
27311d6441e8 alonenet bridge local
d77f72eb22a0 bridge bridge local
0fe5c359c61a host host local
3b0a84266bfe none null local
二、创建MySQL数据库
1、docker直接拉取MySQL指定版本即可
[root@pxc-node1 ~]# docker pull mysql:8.0.26
8.0.26: Pulling from library/mysql
33847f680f63: Already exists
5cb67864e624: Pull complete
1a2b594783f5: Pull complete
b30e406dd925: Pull complete
48901e306e4c: Pull complete
603d2b7147fd: Pull complete
802aa684c1c4: Pull complete
715d3c143a06: Pull complete
6978e1b7a511: Pull complete
f0d78b0ac1be: Pull complete
35a94d251ed1: Pull complete
36f75719b1a9: Pull complete
Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd
Status: Downloaded newer image for mysql:8.0.26
docker.io/library/mysql:8.0.26
[root@pxc-node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0.26 c60d96bd2b77 3 weeks ago 514MB
2、创建容器映射相关目录
[root@pxc-node1 ~]# mkdir -p /root/mysql/data /root/mysql/logs /root/mysql/conf
[root@pxc-node1 ~]# touch /root/mysql/conf/my.cnf
[root@pxc-node1 ~]# touch /root/mysql/conf/my.cnf
3、初始化MySQL数据库
[root@pxc-node1 ~]# docker run -p 3306:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/var/log/mysql -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.26
de9b06ef5c34fe99deea338d646e8037779f271b41bd9d7c5d22ae7bf561a7c9
-d: 后台运行容器,也可以使用镜像id
-p 将容器的端口映射到本机的端口
-v 将主机目录挂载到容器的目录
-e 设置参数 MYSQL_ROOT_PASSWORD 指定登录密码
4、校验MySQL服务是否正常
[root@pxc-node1 ~]# docker start mysql
mysql
[root@pxc-node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
de9b06ef5c34 mysql:8.0.26 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
5、容器内,登录MySQL
[root@pxc-node1 ~]# docker exec -it mysql /bin/bash
root@de9b06ef5c34:/# mysql -uroot -p
Enter password:root
6、校验文件是否本持久化到本地
[root@pxc-node1 ~]# ll /root/mysql/data/
7、客户端连接
Navicat连接配置
mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from information_schema.user_privileges;
Java应用连接,JDBC启动报链接异常,链接异常是因为缺少必要的参数,根据以下参数进行检测即可。
# url: jdbc:mysql://localhost:3306/renren_fast?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
url: jdbc:mysql://172.18.0.100:3306/renren_fast?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false
三、创建Redis缓存数据库
1、docker直接拉取指定Redis版本即可
[root@pxc-node1 ~]# docker pull redis:6.0.0
6.0.0: Pulling from library/redis
Digest: sha256:9c2d321d367c582fc103ad36b7326a0edd5b558e0c987d0bea3b58bac008b20f
Status: Image is up to date for redis:6.0.0
docker.io/library/redis:6.0.0
[root@pxc-node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 6.0.0 96812ab2b931 15 months ago 104MB
2、创建Redis卷相关目录
[root@pxc-node1 ~]# mkdir -p /root/redis/conf /root/redis/data
3、 初始化Redis数据库
[root@pxc-node1 ~]# docker run -d --privileged=true -p 6379:6379 --restart always -v /root/redis/conf/redis.conf:/etc/redis/redis.conf -v /root/redis/data:/data --name redis redis:6.0.0 redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456
7f22db46ef7afd2df052e5876ade90542a6b8ae2a6cc19e4cc9bfda6decdb3fc
命令解释:
-d:后台运行;
--privileged:是否允许容器获取宿主机的root权限;
-p:容器内应用映射宿主机的端口;
--restart:指定容器重启策略;
-v:容器挂载宿主机目录;
--name:镜像运行成容器,容器的名称;
4、校验登录Redis服务是否正常
[root@pxc-node1 ~]# docker exec -it redis redis-cli -a "123456"
四、Java项目配置
1、
五、Vue前端应用配置
六、Web前端服务器NGINX安装
[root@pxc-node1 ~]# docker pull nginx:1.20.1
1.20.1: Pulling from library/nginx
33847f680f63: Already exists
b89def51dd13: Pull complete
f3c07eca8dff: Pull complete
2f0bc188d92e: Pull complete
412cc00cedb9: Pull complete
b7768597b864: Pull complete
Digest: sha256:cd108d06120a7c0d3d073ba4e3eee7b5e6759f0c8a80799e2dbb35d07388840c
Status: Downloaded newer image for nginx:1.20.1
docker.io/library/nginx:1.20.1
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Lukey
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果