后端打包
参考SpringBoot+Maven多环境部署
将后端项目打包
编写 Dockerfile 文件
1 2 3 4 5 6 7 8
| FROM java:8
ADD myblog-0.0.1-SNAPSHOT.jar /blog.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/blog.jar"]
MAINTAINER dzgu
|
自动化脚本
可以作为通用脚本来使用的模板脚本,只需改变其中的一些参数即可,具体执行流程为:停止旧服务->删除旧容器->删除旧镜像->打包新镜像->运行新镜像。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #!/usr/bin/env bash
SOURCE_PATH=/mydata/docker
SERVER_NAME=blog-springboot-0.0.1.jar TAG=latest SERVER_PORT=8090
echo "删除容器$SERVER_NAME" docker stop $SERVER_NAME docker rm $SERVER_NAME
echo "删除镜像$SERVER_NAME" docker rmi $SERVER_NAME echo "开始构建镜像" cd $SOURCE_PATH docker build -t $SERVER_NAME:$TAG .
docker run --name $SERVER_NAME -e TZ="Asia/Shanghai" -v /mydata/upload:/mydata/upload -d -p $SERVER_PORT:$SERVER_PORT $SERVER_NAME:$TAG echo "$SERVER_NAME容器创建完成"
|
Docker 运行后端项目
进入服务器/mydata/docker 下,构建后端镜像
1 2
| cd /mydata/docker sh ./blog-start.sh
|
需要重新部署只需重新传 jar 包,执行 sh 脚本即可
打包前端项目
nginx 配置
有域名
在/mydata/nginx 下创建 nginx.conf 文件,格式如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65;
client_max_body_size 50m; client_body_buffer_size 10m; client_header_timeout 1m; client_body_timeout 1m;
gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 4; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on;
server { listen 80; server_name spring.dzgu.top;
location / { root /mydata/vue/blog; index index.html index.htm; try_files $uri $uri/ /index.html; }
location ^~ /api/ { proxy_pass http://你的ip:8090/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
}
server { listen 80; server_name admin.dzgu.top;
location / { root /mydata/vue/admin; index index.html index.htm; try_files $uri $uri/ /index.html; }
location ^~ /api/ { proxy_pass http://你的ip:8090/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
} server { listen 80; server_name ws://ws.dzgu.top;
location / { proxy_pass http://你的ip:8090/websocket; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }
} server { listen 80; server_name static.dzgu.top;
location / { root /mydata/upload/; }
}
}
|
我前台和后台时分为两个域名,所以写了两个 server,前端项目路径为之前传输的路径,其他两个为文件上传域名和 websocket 转发域名。
docker 启动 nginx 服务
1 2
| docker run --name nginx --restart=always -p 80:80 -d -v /mydata/nginx/nginx.conf:/etc/nginx/nginx.conf -v /mydata/vue:/mydata/vue -v /mydata/upload:/mydata/upload -d nginx
|
无域名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65;
client_max_body_size 50m; client_body_buffer_size 10m; client_header_timeout 1m; client_body_timeout 1m;
gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 4; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on;
server { listen 80; server_name 你的ip;
location / { root /mydata/vue/blog; index index.html index.htm; try_files $uri $uri/ /index.html; }
location ^~ /api/ { proxy_pass http://你的ip:8090/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
}
server { listen 81; server_name 你的ip;
location / { root /mydata/vue/admin; index index.html index.htm; try_files $uri $uri/ /index.html; }
location ^~ /api/ { proxy_pass http://你的ip:8090/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
}
server { listen 82; server_name 你的ip;
location / { proxy_pass http://你的ip:8090/websocket; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }
}
server { listen 83; server_name 你的ip;
location / { alias /mydata/upload/; autoindex on; autoindex_exact_size on; autoindex_localtime on; }
}
}
|
docker 启动 nginx 服务
1
| docker run --name nginx --restart=always -p 80:80 -p 81:81 -p 82:82 -p 83:83 -d -v /mydata/nginx/nginx.conf:/etc/nginx/nginx.conf -v /mydata/vue:/mydata/vue -v /mydata/upload:/mydata/upload -d nginx
|
ps:需要通过 ip + 端口号访问项目