使用docker部署SSM的服务器,踩了一些坑,做一下记录,以此存照。
1、配置docker-compose.yml
version: '3'
services:
eam-application:
build:
context: ../eam
dockerfile: Dockerfile
# 指定容器名称
container_name: eam-application
# 重启机制
restart: always
# hub地址,image版本
image: eam-application
privileged: true
volumes:
- ../eam:/eam:rw
- /etc/localtime:/etc/localtime
# 访问端口
ports:
- "8087:8087"
environment:
# 指定时区
- TZ="Asia/Shanghai"
eam-nginx:
# 1)镜像名
image: nginx
# 2)自定义容器名
container_name: eam-nginx
# 3)Docker中日志声明 (此处配置为none)
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "10"
# 4)日志路径映射 ro 只读--访问模式; rw 读写
privileged: true
restart: always
volumes:
#compose文件与nginx文件夹目录结构必须一致
- ./eam-nginx/eam-front:/usr/share/nginx/dist:rw #前端静态文件目录(项目位置)
# - ./xihe-nginx/nginx:/etc/nginx:rw #nginx配置文件
- ./eam-nginx/nginx.conf:/etc/nginx/nginx.conf
- ./eam-nginx/conf.d:/etc/nginx/conf.d
- ./logs/eam-nginx:/var/log/nginx:rw #日志文件
# 5)环境配置
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
- JVM_OPTS=-Xmx512m -Xms512m -XX:MaxPermSize=256m
#extra_hosts:
# - www.test.com:119.28.137.66
# 6)端口映射
ports:
- "8001:80"
2、配置Dockerfile
FROM java:8
MAINTAINER Ling
# WORKDIR 工作目录
WORKDIR /home/eam/
# RUN 设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
# ADD 添加jar和yml配置文件到镜像
ADD eam-application.jar /eam-application.jar
ENTRYPOINT ["java","-Xmx512M","-Xms512M","-XX:MaxPermSize=256M","-Dloader.path=./lib/","-jar","/eam-application.jar"]
3、首先启动一次docker,拉取nginx,并映射相关的文件夹,之后对nginx的进行设置。
4、配置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 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 70;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
5、设置sever的conf
client_max_body_size 50m;
server {
listen 80;
server_name localhost,127.0.0.1, 192.168.2.242;
# gzip config
gzip on;
gzip_min_length 200;
gzip_buffers 4 16k;
gzip_comp_level 6;
gzip_types text/plain application/javascript text/css application/xml text/javascript application/json;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
gzip_proxied any;
location / {
#add_header Access-Control-Allow-Origin *;
#add_header Access-Control-Allow-Headers X-Requested-With;
#add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
#proxy_pass http://192.168.2.242:3100;
proxy_pass_request_headers on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root /usr/share/nginx/dist/;
index index.html;
#if (!-e $request_filename) {
# rewrite ^(.*)$ /index.html?s=$1 last;
# break;
#}
}
location /api {
#proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://192.168.2.242:8087; #设置后台转发地址
proxy_pass_request_headers on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
6 、将前端打包好的文件放到设置好的路径。使用docker-compose up -d 启动服务