[笔记]常用Dockerfile & nginx.conf

备注: 均需要启动docker buildkit

示例命令:

DOCKER_BUILDKIT=1 docker build -f ./docker/Dockerfile-xxx -t ly-admin:1.0.1 .

如果无法使用buildkit, 则需要去除--mount=type=cache,target=?,sharing=shared,id=?这段代码

SPA前端应用

  • dockerfile

    # build stage, 打包
    FROM node:16-alpine as build-stage
    WORKDIR /app
    ARG PACKAGE_JSON=/package.json
    #ARG YARNLOCK=/yarn.lock
    COPY ${PACKAGE_JSON} ./
    #COPY ${YARNLOCK} ./
    RUN yarn config set cache-folder "/root/.yarn"
    RUN --mount=type=cache,target=/root/.yarn,sharing=shared,id=yarn-cahce yarn install  --registry https://registry.npm.taobao.org/
    COPY / .
    RUN yarn build:prod
    
    # production stage
    FROM nginx:stable-alpine as production-stage
    RUN rm -f /usr/share/nginx/html/*
    COPY --from=build-stage /app/dist /usr/share/nginx/html
    COPY /docker/dev/nginx.conf /etc/nginx/conf.d/default.conf
    
    CMD ["nginx", "-g", "daemon off;"]
    
  • dockerfile 服务器资源有限, 本地打包, 仅上传dist到nginx

    # production stage
    FROM nginx:stable-alpine as production-stage
    RUN rm -f /usr/share/nginx/html/*
    COPY /dist /usr/share/nginx/html
    COPY /docker/dev/nginx.conf /etc/nginx/conf.d/default.conf
    
    CMD ["nginx", "-g", "daemon off;"]
    
  • nginx.conf

    server {
        listen       80;
        server_name  server-name;
    
        # gzip压缩
        gzip on;
        gzip_comp_level 4;
        gzip_static on;
        gzip_vary on;
        gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype;
        # 前端代理
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files  $uri $uri/ /index.html;
        }
        # 后端代理
        location /api/ {
            proxy_pass http://xxx.xxx.xxx.xxx:xxxx/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            # WebSocket代理
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }
    }
    

网关, 同时代理后台和前端

  • Dockerfile

    FROM nginx:stable-alpine
    RUN rm -f /use/share/nginx/html/*
    
    COPY /nginx.conf /etc/nginx/conf.d/default.conf
    
    CMD ["nginx", "-g", "daemon off;"]
    
  • nginx.conf

    # 后台服务地址
    upstream backend {
        ip_hash;
        # 负载均衡, 多的话继续往下写
        # weight表示权重, 权重越大, 则往此服务上转发的可能性越大
        # max_fails表示最大失败次数, 如果失败超过最大次数后将暂时下线此服务(注: http status为500时也算)
        # fail_timeout 复合max_fails后, 持续时间内将不再转发到此服务(如果都失败了, 还是会强制重试)
        server 192.168.1.1:8080 weight=5 max_fails=2 fail_timeout=60s;
        server 192.168.1.2:8080 weight=5 max_fails=2;
        # down 表示此服务已下线, 不会被转发
        server 192.168.1.3:8080 down;
        # backup 备用服务, 其他服务不可用时将转发到此服务
        server 192.168.1.4:8080 backup;
        # 最简配置
        server 192.168.1.5:8080;
    }
    # 前端服务1, 访问地址为http://192.168.2.6:80/font1
    upstream font1 {
        server 192.168.2.6:80;
    }
    # 前端服务2, 访问地址为http://192.168.2.6:80/font2
    upstream font2 {
        server 192.168.2.6:80;
    }
    
    
    server {
        listen       88;
        server_name  xx-server-forwart;
    
        proxy_set_header Host $proxy_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 100m;
    
        location /api/ {
            proxy_pass http://gateway/;
            # webstocket支持
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }
    
        location /basic {
            proxy_pass http://basic/basic;
        }
        location /portal {
            proxy_pass http://portal/portal;
        }
        location /screen {
            proxy_pass http://screen/screen;
        }
    }
    

后端多模块打包springboot

  • dockerfile

    # 打包
    FROM maven:3.8.6-openjdk-8 as build-stage
    WORKDIR /maven
    COPY / .
    # 多模块单独打包子模块, 打包模块为parent-module/sub-module
    RUN --mount=type=cache,target=/root/.m2,sharing=shared,id=mvn-cache mvn package -pl parent-module/sub-module -am -DskipTests=true
    
    FROM anapsix/alpine-java:8_server-jre_unlimited as production-stage
    MAINTAINER LyChn
    WORKDIR /app
    # alpine镜像时区
    RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && apk add --no-cache tzdata
    ENV TZ=Asia/Shanghai
    # 端口
    ENV SERVER_PORT=8080
    EXPOSE ${SERVER_PORT}
    
    COPY --from=build-stage /maven/parent-module/sub-module/target/sub-module.jar ./app.jar
    
    ENTRYPOINT ["java", \
                "-Djava.security.egd=file:/dev/./urandom", \
                "-Dserver.port=${SERVER_PORT}", \
                "-jar", "-Xms512m", "-Xmx512m", "-XX:PermSize=256m", "-XX:MaxPermSize=256m", "-XX:MaxNewSize=512m",  \
                "app.jar"]
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值