系统采用前后端分别部署方式, 后端采用SpringBoot框架, 部署在单独的容器内,对外放8080端口。前端通过Nginx的反向代理访问后端8080端口。
后端容器部署
如果前后端容器部署在同一台物理机, 为保障系统安全,可以把后端、数据的端口不映射到主机端口,创建名为sshl的容器(镜像名为seed-server)时,如下:
docker run --restart=always --name sshl -d seed-server
查看容器地址:
docker inspect sshl
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f0c831e7265d9edc3a0541aa1a7483448b067757687d28e229284c32a54b7dd9",
"EndpointID": "a210475489c8f7d1b481a0fce3e4f0c8e4caf64e9ffa3461d856eb9cf9f47999",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
可以看到容器地址是172.17.0.4
前端Nginx 容器部署
安装Nginx 容器
(略)
修改nginx.conf
docker run -d nginx
# b72e67077708 为新建的容器ID
docker cp b72e67077708:/etc/nginx/nginx.conf /mnt/data/nginx/conf/
docker cp b72e67077708:/usr/share/nginx/html/index.html /mnt/data/nginx/www/
docker stop b72e67077708
docker rm b72e67077708
修改:/mnt/data/nginx/conf/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;
# 下面两个参数设置为on用于防止网络阻塞
tcp_nopush on;
tcp_nodelay on;
#客户端连接保持活动的超时时间
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
# server_name 为宿主机地址
server_name 192.168.204.181;
root /usr/share/nginx/html;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
index index.html index.htm;
}
# 系统里添加反向代理/api ,172.17.0.4:8080 是后端地址
location /api {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://172.17.0.4:8080;
}
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
重建Nginx 容器
docker run -p 80:80 --restart=always --name nginx \
-v /mnt/data/nginx/www:/usr/share/nginx/html \
-v /mnt/data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mnt/data/nginx/logs:/var/log/nginx \
-d nginx
- 访问系统:http://192.168.204.181 即可