1. 程式人生 > 其它 >Docker 啟動前後端指令碼

Docker 啟動前後端指令碼

後端

docker 提前執行 consul,mongo,redis

進入到 Jar所在的資料夾(資料夾下只有一個Jar!)

新建 start.sh

CMD jar_name=$(ls -1 -F *.jar) && \
 java  -Xms450m -Xmx450m \
  -XX:+UseContainerSupport -XX:MaxRAMPercentage=80  -XX:MaxMetaspaceSize=178m -XX:MetaspaceSize=178m -Xss512k ${JAVA_OPTS} \
 -Dlog4j2.formatMsgNoLookups=true \
 -Dspring.cloud.bootstrap.enabled=true \
 -Dreactor.netty.pool.leasingStrategy=lifo \
 -Dloader.path=lib \
 -jar $jar_name \
 --spring.main.allow-bean-definition-overriding=true \
 --spring.main.allow-circular-references=true \
 --spring.profiles.active=consul,yuxh,amy $*

執行Docker (暴露埠,是給前端呼叫聯呼叫!)

docker run -d  --name gateway -v "$PWD":/app -w /app -p 8083:8083 --link consul:consul --link mongo:mongo --link redis:redis  openjdk:17  /bin/bash  /app/start.sh

前端

/vue/nginx.cnf

server {
    listen       80;
    listen  [::]:80;
    server_name _; 

    #access_log  /var/log/nginx/host.access.log  main;
    absolute_redirect off;
    index index.html;
    
    location /api/ {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Origin' "$http_origin";
            add_header 'Access-Control-Allow-Methods' '$http_access_control_request_method';
            add_header 'Access-Control-Allow-Headers' '$http_access_control_request_headers';
            add_header 'Access-Control-Expose-Headers' '$http_access_control_request_headers';
            add_header 'Access-Control-Max-Age' '2592000';
            add_header 'Content-Length' 0;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Access-Control-Allow-Headers' 'Authorization';
            return 204;
        } 
        
        proxy_pass http://mp-gateway-api:8083/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    
    location /{虛擬目錄} {
        alias   /vue/;
        try_files $uri $uri/ /index.html =404;
    } 
}

cp /vue/nginx.cnf /vue/{應用名}/nginx.cnf
cd /vue/{應用名}
修改 nginx.cnf 中的 虛擬目錄

docker run -d  -p 8010:80 --name {應用名} --link mp-gateway-api:mp-gateway-api -v "$PWD"/nginx.conf:/etc/nginx/conf.d/default.conf  -v "$PWD"/dist:/vue/   nginx