一、打包springboot程序
1.如果打包出现问题
在File–>Setting–> File Encoding 中 改为UTF-8,防止乱码
将原有的编译插件 更换为
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
2.将blog-admin添加到blog-parent里
修改blog-parent的pom文件
3.创建application-prod.properties文件
blog-admin的配置文件:
server.port=8889
spring.application.name=mszlu_admin_blog
# datasource
spring.datasource.url=jdbc:mysql://mysql的docker容器ip:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC
spring.datasource.username=root
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis-plus
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.table-prefix=ms_
blog-api的配置文件:
server.port=8888
spring.application.name=mszlu_blog
spring.mvc.servlet.path=/api
#server.servlet.contextPath=/api
#数据库的配置
# datasource
spring.datasource.url=jdbc:mysql://mysql的docker容器ip:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC
spring.datasource.username=root
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis-plus
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.table-prefix=ms_
spring.redis.host=redis的docker容器ip
spring.redis.port=6379
spring.servlet.multipart.max-request-size=20MB
spring.servlet.multipart.max-file-size=2MB
qiniu.accessKey=111
qiniu.accessSecretKey=111
修改跨域配置
在com/mszlu/blog/config/WebMVCConfig.java
中:
5.打包
二、安装docker
# 1、yum 包更新到最新
yum update
# 2、安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
yum install -y yum-utils device-mapper-persistent-data lvm2
# 3、 设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 4、 安装docker,出现输入的界面都按 y
yum install -y docker-ce
# 5、 查看docker版本,验证是否验证成功
docker -v
#启动docker
/bin/systemctl start docker.service
三、安装Docker Compose
运行以下命令以下载 Docker Compose 的当前稳定版本:
$ sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
将可执行权限应用于二进制文件:
$ sudo chmod +x /usr/local/bin/docker-compose
创建软链:
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
测试是否安装成功:
$ docker-compose --version
四、拉取镜像
1.拉取镜像
下面四个镜像中,mysql和redis先创建容器运行起来,不通过compose编排运行
docker pull nginx
docker pull redis:5.0.3
docker pull java:8
docker pull mysql:5.7
2.mysql
创建容器,设置端口映射、目录映射
mkdir /mnt/docker/mysql
cd /mnt/docker/mysql
docker run -id \
-p 3306:3306 \ # 前面的操作系统端口,后面的为容器端口
--name=c_mysql \
-v /mnt/docker/mysql/conf:/etc/mysql/conf.d \
-v /mnt/docker/mysql/logs:/logs \
-v /mnt/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
mysql:5.7
在/mnt/docker/mysql/conf 创建my.cnf
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
default-time_zone = '+8:00'
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names=1
pid-file=/var/run/mysqld/mysqld.pid
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
去查看mysql对应的docker容器ip地址,项目配置文件中要用
docker inspect c_mysql
3.redis
启动容器
docker run -id --name=redis -p 6379:6379 redis:5.0.3
去查看mysql对应的docker容器ip地址,项目配置文件中要用
docker inspect redis
五、对用到的东西做一些简介
1.docker镜像:将其理解为一个镜像
2.docker容器:先要有镜像,然后通过镜像来创建启动容器,可将每个docker容器看做单独的系统
删除镜像或者容器,要先删容器再删镜像:
docker rmi 镜像id或者名称
docker rm 容器id或者名称
3.Dockerfile:本项目中将程序的jar包构建成镜像,使其能够作为容器启动
4.Docker Compose 服务编排:这个项目中用它启动nginx容器,以及自己程序jar包的容器。当对其执行down后编排的所有容器都会停止
六、目录结构
1.后面参照这个结构来上传或者创建文件
2.创建所有需要的文件
app目录下:
blog_dockerfile:
FROM java:8
EXPOSE 8888
ADD ./blog_api.jar /api.jar
RUN bash -c 'touch /api.jar'
ENTRYPOINT ["java","-jar","/api.jar","--spring.profiles.active=prod"]
这个文件是将blog_api.jar构建成镜像的,blog_admin.jar的修改如下:
FROM java:8
EXPOSE 8889
ADD ./blog_admin.jar /admin.jar
RUN bash -c 'touch /admin.jar'
ENTRYPOINT ["java","-jar","/admin.jar","--spring.profiles.active=prod"]
mysql目录下:
my.cnf,前面已经创建
docker-compose目录下:
docker-compose.yml
version: '3'
services:
nginx:
image: nginx
container_name: nginx
ports: #ports:绑定容器的端口到主机的端口,前者为主机端口;expose:将当前容器的端口暴露给link到本容器的容器。
- 80:80
links:
- app
depends_on:
- app
volumes: #将本地目录挂载到容器的目录下,前者为linux的目录,后者为容器目录。(访问容器的目录即可访问到被挂载目录下的所有文件)
- /mnt/docker/docker-compose/nginx/:/etc/nginx/
# privileged: true
network_mode: "bridge"
app:
image: app
container_name: app
ports:
- 8888:8888
# expose:
# - "8888"
network_mode: "bridge"
admin:
image: admin
container_name: admin
ports:
- 8889:8889
# expose:
# - "8889"
network_mode: "bridge"
nginx目录下:
mime.types:
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
font/woff woff;
font/woff2 woff2;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation
pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document
docx;
application/vnd.wap.wmlc wmlc;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
conf.d目录下:
blog.conf:
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_vary off;
upstream appstream{
server 云服务器的私网ip:8888;
}
server {
listen 80;
server_name 公网ip;
index index.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /api {
proxy_pass http://appstream;
}
location / {
root /etc/nginx/html;
index index.html;
}
}
七、发布springboot项目(将jar包构建成docker镜像)
1.将springboot项目打好的包上传到app目录下,分别改名为blog_admin.jar和blog_api.jar
2.使用cd命令进入到app目录下,执行构建命令
# 对应blog_api.jar
docker build -f ./blog_dockerfile -t app .
和
# 对应blog_admin.jar
docker build -f ./blog_dockerfile -t admin .
八、前端项目打包和上传
1.修改config\prod.env.js
'use strict'
module.exports = {
NODE_ENV: '"production"',
BASE_API: '"http://公网ip/api/"'
}
2.打包
npm run build
生成:
3.上传
将static文件夹和index.html文件上传到html目录里
九、编排启动
使用cd命令进入docker-compose目录
在里面执行启动命令,建议先不后台启动,观察是否有启动失败容器
docker-compose up #构建(重新构建)并启动容器
docker-compose up -d #-d 表示后台构建
docker-compose down #停止并删除容器
docker-compose start #启动已有容器
docker-compose stop #停止运行的容器
然后访问
博客网址:http://公网ip/
后台管理系统网址:http://公网ip:8889/login.html