diff --git a/Dockerfile b/Dockerfile index 74be427..018caca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,20 @@ ## 第一阶段 ====> Nodejs打包编译输出前端资源 -FROM node:18-alpine AS buildNode - -ENV FRONTEND_DIR = "/jenkins/.jenkins/workspace/ems_frontend_vue3" +FROM node:18-alpine AS build-nodejs ## 工作目录存放程序源码 WORKDIR /frontend ## 复制实际需要的文件到工作目录 -COPY ${FRONTEND_DIR} ./ +COPY ./frontend ./ RUN npm config set registry https://registry.npmmirror.com -RUN npm install && npm run build +RUN cd /frontend && npm install && npm run build ## 第二阶段 ====> GO打包编译输出后端程序 -FROM golang:alpine AS buildGo +FROM golang:alpine AS build-go ARG VERSION -ENV BACKEND_DIR = "/jenkins/.jenkins/workspace/ems_backend" - ENV CGO_ENABLED 0 ENV GOOS linux ENV GOPROXY https://goproxy.cn,direct @@ -26,20 +22,21 @@ ENV GOPROXY https://goproxy.cn,direct WORKDIR /backend ## 复制实际需要的文件到工作目录 -COPY ${BACKEND_DIR}/docker ./docker -COPY ${BACKEND_DIR}/restagent/config ./restagent/config -COPY ${BACKEND_DIR}/restagent/etc ./restagent/etc -COPY ${BACKEND_DIR}/restagent/restagent.go ./restagent/restagent.go -COPY ${BACKEND_DIR}/features ./features -COPY ${BACKEND_DIR}/lib ./lib -COPY ${BACKEND_DIR}/src ./src -COPY ${BACKEND_DIR}/go.sum ./ -COPY ${BACKEND_DIR}/go.mod ./ +COPY ./backend/docker ./docker +COPY ./backend/restagent/config ./restagent/config +COPY ./backend/restagent/etc ./restagent/etc +COPY ./backend/restagent/restagent.go ./restagent/restagent.go +COPY ./backend/features ./features +COPY ./backend/lib ./lib +COPY ./backend/src ./src +COPY ./backend/go.sum ./ +COPY ./backend/go.mod ./ RUN cd /backend && go mod download -RUN cd /backend/restagent && go build -o omc -v -ldflags "-X 'ems.agt/lib/global.Version=${VERSION}' -X 'ems.agt/lib/global.BuildTime=`date`' -X 'ems.agt/lib/global.GoVer=`go version`'" +#RUN cd /backend/restagent && go build -o backend -v -ldflags "-X 'ems.agt/lib/global.Version=${VERSION}' -X 'ems.agt/lib/global.BuildTime=`date`' -X 'ems.agt/lib/global.GoVer=`go version`'" +RUN cd /backend/restagent && go build -ldflags="-s -w" -o backend -## 第三阶段 ====> 构建前后端融合镜像 +## 镜像基座 ====> GO打包编译输出后端程序 FROM alpine ## 安装时区工具 @@ -55,11 +52,13 @@ ENV TZ="Asia/Shanghai" ENV LANG="en_US.UTF-8" WORKDIR /usr/local/omc -COPY --from=buildNode /frontend/dist /usr/local/omc/bin/frontend -COPY --from=buildGo /backend/restagent/omc /usr/local/omc/bin/backend -COPY --from=buildGo /backend/docker/config.yaml /usr/local/omc/etc/config.yaml -COPY --from=buildGo /backend/docker/nginx.conf /etc/nginx/nginx.conf +# 分步编译 +COPY --from=build-nodejs /frontend/dist /usr/local/omc/bin/frontend +COPY --from=build-go /backend/restagent/backend /usr/local/omc/bin/backend +# 配置文件 +COPY ./backend/docker/config.yaml /usr/local/omc/etc/config.yaml +COPY ./backend/docker/nginx.conf /etc/nginx/nginx.conf -EXPOSE 3030 80 22 +EXPOSE 22 80 3030 -CMD ["/usr/sbin/sshd", "-D", ";", "nginx", "-g", "daemon off;", ";", "/usr/local/omc/bin/backend", "--env", "prod", "-c", "/usr/local/omc/etc/config.yaml"] +CMD ["/bin/sh", "-c", "/usr/sbin/sshd && nginx && /usr/local/omc/bin/backend --env prod -c /usr/local/omc/etc/config.yaml"] diff --git a/README.md b/README.md index 0ecf6b7..c806b4e 100644 --- a/README.md +++ b/README.md @@ -39,4 +39,28 @@ BUILD_ID=dontKillMe nohup ./omcDevApp -c ./dev.yaml > ./dev.log 2>&1 & echo ok -``` \ No newline at end of file +``` + + +## Docker + +docker build --build-arg VERSION=240110 -t omc:240110 . + +docker run -it omc:240110 sh + +-v /var/log/omc +-v /etc/nginx +-v /usr/local/omc/etc +-v /usr/local/omc/static +-v /usr/local/omc/upload + +docker run -d \ +--privileged=true \ +--restart=always \ +-p 3222:22 \ +-p 3280:80 \ +-p 3230:3030 \ +-e TZ="Asia/Shanghai" \ +-m 512M \ +--name omc_001 \ +omc:240115 \ No newline at end of file diff --git a/docker/nginx.conf b/docker/nginx.conf index cc6a2e1..4307f14 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -1,15 +1,50 @@ -worker_processes 1; +user nginx; +worker_processes auto; + +error_log /var/log/omc/nginx/error.log notice; +pid /var/run/nginx.pid; + events { - worker_connections 1024; + 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/omc/nginx/access.log main; + + sendfile on; + #tcp_nopush on; + + keepalive_timeout 65; + + server_tokens off; + + #开启gzip功能 + gzip on; + #开启gzip静态压缩功能 + gzip_static on; + #gzip缓存大小 + gzip_buffers 4 16k; + #gzip http版本 + gzip_http_version 1.1; + #gzip 压缩级别 1-10 + gzip_comp_level 5; + #gzip 压缩类型 + gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; + # 是否在http header中添加Vary: Accept-Encoding,建议开启 + gzip_vary on; + server { - listen 80; + listen 80; + listen [::]:80; + server_name localhost; location /omc-api/ { proxy_pass http://127.0.0.1:3030/;