Docker部署Vue+SpringBoot+MySQL

1、部署vue

设置前端映射到后端项目所暴露的服务器端口号

前端项目

1、前端项目下运行npm run build,生成了dist文件夹,里面有项目的相应的打包文件

在这里插入图片描述

2、复制项目下的dist文件夹到另外的文件夹,在同级目录下建立DockerFile和default.conf文件

DockerFile

# 设置基础镜像
FROM nginx
# 删除目录下的default.conf文件
RUN rm /etc/nginx/conf.d/default.conf
# 将default.conf复制到/etc/nginx/conf.d/下,用default.conf配置来替换nginx镜像里的默认配置
ADD default.conf /etc/nginx/conf.d/
# 将项目根目录下dist文件夹下的所有文件复制到镜像/usr/share/nginx/html/目录下
COPY dist/ /usr/share/nginx/html/

default.conf

server {
    listen       8092;	//部署到服务器上访问的前端端口
    server_name  10.1.248.121; //部署到的服务器的地址

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html =404;
    }
	
	location /nwdaf {
		proxy_pass   http://10.1.248.121:8091/nwdaf;	//与前端对应的后端的地址
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

3、将如下文件上传到服务器的/mnt/docker-web-test文件夹下

4、制作镜像(build后面有.)

docker build -t docker-web-test .

5、运行容器

docker run --net=host -d  docker-web-test
2、部署mysql

1、启动mysql容器

# 下载镜像
docker pull mysql:5.7

# 查看镜像
docker images|grep mysql

#启动容器,将服务器的配置文件,数据文件等挂载到容器中对应的目录中,(设置root用户的初始密码是123456)
docker run -p 13306:3306 --name nwdaf-mysql -v /usr/local/workspace/mysql/conf:/etc/mysql -v /usr/local/workspace/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

2、查看容器

docker ps|grep mysql

3、进入mysql容器

# 登录容器
[root@cbov10-sso55-xxx ~]# docker exec -it nwdaf-mysql bash

# 登录mysql
root@5291ed3fe987:/# mysql -uroot -p
Enter password: 

mysql> show databases;

4、设置能远程登录mysql

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
| localhost | test          |
+-----------+---------------+
5 rows in set (0.00 sec)

# 设置root用户在任何地方进行远程登录,并具有所有库任何操作权限,(公司绝对不能这么做,暴露的攻击面太大),这里只是做测试。
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

# 刷新权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

# 退出mysql 
mysql> exit
Bye

5、远程登录测试

Navicat 登录服务器地址+宿主机暴露的端口,本例13306:3306中的宿主机端口13306

docker容器重启之后,由于宿主机的数据文件挂载到容器中的文件,宿主机的文件保存完好,mysql容器重启后,文件仍然不会丢失

6、执行数据库初始化的sql文件

  • 拷贝数据库初始化sql文件到服务器的目录下:/usr/local/workspace/mysql/sql/

  • 将sql文件复制到docker容器中

    • docker ps|grep mysql #查看mysql容器
    • docker inspect mysql容器的短id| grep Id #获取mysql容器的完整id
    • docker cp 本地文件路径 ID全称:容器路径[docker cp mysql.sql 12345:/tmp/]
  • 进入docker容器,执行sql文件

    • docker exec -it nwdaf-mysql bash #进入容器
    • mysql -u root -p #进入mysql服务
    • show databases #查看数据库;
    • use database #进入数据库
    • source /tmp/nwdaf_dev.sql #执行.sql文件

7、sql_mode设置
起因:三个服务搭起来之后,在查询某个列表时报错:

In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'school.student.s_id'; this is incompatible with sql_mode=only_full_group_by

是由于mysql容器的sql_mode是默认方式,和本地不一致(能使服务正常运行的环境)

则修改mysql容器的sql_mode(设置为本地mysql的sql_mode一样的设置,防止查询报错)

本地sql_mode:STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

(1)进入运行的mysql容器:docker exec -it nwdaf-mysql bash

(2)查看配置文件:cat /etc/mysql/mysql.conf.d/mysqld.cnf。没有显示sql_mode,表示使用了mysql默认的sql_mode

(3)将该文件内容复制到本地,并在mysqld后面加上sql_mode:

# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
#log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
sql_mode	= STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

(4)回到MySQL容器的命令行中,退出,关闭并删除原先启动的容器

ctrl+p+q #退出
docker stop nwdaf-mysql
docker rm nwdaf-mysql

(5)将配置文件传输到服务器的mysql配置文件夹/usr/local/workspace/mysql/conf,并启动容器,将此文件夹下的配置文件挂载到mysql容器中。

docker run -p 13306:3306 --name nwdaf-mysql -v /usr/local/workspace/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -v /usr/local/workspace/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

可再次进入mysql容器,查看是否使用了修改后的sql_mode。方法同前

3、容器方式部署自己的springboot项目

项目设置访问mysql地址为容器所在服务器的端口号:

将自己的springboot项目,打成jar包。制作镜像,并在云服务器上用做容器运行。

1、maven clean->maven package或者在右侧maven执行命令clean install -Dmaven.test.skip。target目录中生成jar包

2、将jar包上传到云主机的/home/usr/docker或者/mnt/项目名文件夹下

3、在相同的文件夹下新建DockerFile

FROM java:8 
VOLUME /tmp
ADD nwdaf-web-service.jar app.jar
ENTRYPOINT ["java","-Xmx512m","-Xms512m","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8084

4、制作镜像

在该文件夹下(DockerFile和jar包所在的文件夹下)

docker build -t xxx(打包好的docker镜像名称) .

5、启动容器

host方式,使得容器暴露的端口8084和云主机暴露的端口一致

docker run --net=host -d  xxx

6、查看容器是否启动

在这里插入图片描述

7、通过访问查看项目是否部署成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H6KQllQR-1605078883929)(E:\LIB\SCI\Java\狂神\Docker\Docker笔记.assets\image-20201016105026837.png)]
至此,三个服务通过容器方式启动,可以测试是否前后端和数据库构成了一个完整项目。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值