From ffe9c4c498dd71d04c6da8f8a2efac3d0b0cda99 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Thu, 15 May 2025 21:41:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0Docker=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E8=84=9A=E6=9C=AC=E5=92=8CDockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build-docker.sh | 18 ++++----- pkg-docker.sh | 5 +-- pkg/docker/Dockerfile.ubuntu22.04.amd64 | 53 ++++++++----------------- 3 files changed, 28 insertions(+), 48 deletions(-) diff --git a/build-docker.sh b/build-docker.sh index 279aa52..6df52f3 100644 --- a/build-docker.sh +++ b/build-docker.sh @@ -1,5 +1,5 @@ #!/bin/bash - +set -xe # Check if Docker is installed if command -v docker &> /dev/null; then echo $(docker --version) @@ -117,22 +117,22 @@ docker_build() { cd ${BuildDir} # check docker image - if ! docker images | grep -q "mariadb:10.6.21"; then + if ! docker images | grep -q "mariadb\s*10.6.21"; then docker pull mariadb:10.6.21 fi - if [ ! -f ./$ReleaseFileName/tar/mariadb_10.6.21.tar ]; then - docker save mariadb:10.6.21 -o ./${ReleaseFileName}/tar/mariadb_10.6.21.tar + if [ ! -f ${BuildDir}/$ReleaseFileName/tar/mariadb_10.6.21.tar ]; then + docker save mariadb:10.6.21 -o ${BuildDir}/${ReleaseFileName}/tar/mariadb_10.6.21.tar fi - if ! docker images | grep -q "bitnami/keydb:6.3.4"; then + if ! docker images | grep -q "bitnami/keydb\s*6.3.4"; then docker pull bitnami/keydb:6.3.4 fi - if [ ! -f ./$ReleaseFileName/tar/keydb_6.3.4.tar ]; then - docker save bitnami/keydb:6.3.4 -o ./${ReleaseFileName}/tar/keydb_6.3.4.tar + if [ ! -f ${BuildDir}/$ReleaseFileName/tar/keydb_6.3.4.tar ]; then + docker save bitnami/keydb:6.3.4 -o ${BuildDir}/${ReleaseFileName}/tar/keydb_6.3.4.tar fi # build omc docker build --platform ${PLATFORM} --build-arg VERSION=${VERSION} -t omc:${VERSION} . - docker save omc:${VERSION} -o ./${ReleaseFileName}/tar/omc_${VERSION}.tar + docker save omc:${VERSION} -o ${BuildDir}/${ReleaseFileName}/tar/omc_${VERSION}.tar # tar package tar -czvf ${ReleaseDir}/${ReleaseFileName}.tar ./${ReleaseFileName} @@ -147,7 +147,7 @@ docker_build() { # =================== building echo echo "building omc..." -mkdir -p ${BuildDir}/${ReleaseFileName} +mkdir -p ${BuildDir}/${ReleaseFileName}/tar cp -rf ${BuildPackagelDir}/docker/Dockerfile.${SYSTEM}.${PLATFORM_ARCH} ${BuildDir}/Dockerfile cp -rf ${BuildPackagelDir}/docker/release/* ${BuildDir}/${ReleaseFileName} cp -rf ${BuildLinuxDir} ${BuildDir} diff --git a/pkg-docker.sh b/pkg-docker.sh index d506a8c..e5eae1d 100644 --- a/pkg-docker.sh +++ b/pkg-docker.sh @@ -11,7 +11,7 @@ BuildDir=/root/omc.git/build.ems BuildBranch=main BuildTmpDir=/root/omc.git/build.ems/tmp # Default Version Value -VERSION="2.2503.2" +VERSION="2.2505.2" # usage usage() { @@ -34,6 +34,7 @@ while [[ $# -gt 0 ]]; do *) usage ;; esac done +# clear tmp dir rm -rf ${BuildTmpDir} && mkdir -p ${BuildTmpDir} # =================== Web @@ -100,5 +101,3 @@ bash $BuildDir/build-docker.sh --version $VERSION --platform linux/amd64 --syste # Compile the front-end and back-end and then package the version # bash pkg-docker.sh -webBranch main -apiBranch main -v 2.2503.2 # bash pkg-docker.sh -v 2.2503.2 - -bash /root/omc.git/build.ems/build-docker.sh --version 2.2505.2 --platform linux/amd64 --system ubuntu22.04 diff --git a/pkg/docker/Dockerfile.ubuntu22.04.amd64 b/pkg/docker/Dockerfile.ubuntu22.04.amd64 index 62f4cff..e8bed73 100644 --- a/pkg/docker/Dockerfile.ubuntu22.04.amd64 +++ b/pkg/docker/Dockerfile.ubuntu22.04.amd64 @@ -1,39 +1,19 @@ ## 第一阶段 ====> Nodejs打包编译输出前端资源 FROM node:20-alpine AS build-node +# 系统文件 +COPY ./linux /linux +# 后端程序 +COPY ./omc_api /api # 前端程序 COPY ./omc_web /web -# COPY./omc_web/src /web/src -# COPY./omc_web/public /web/public -# COPY./omc_web/index.html /web/index.html -# COPY./omc_web/.env.production /web/.env.production -# COPY./omc_web/.env.development /web/.env.development -# COPY./omc_web/package-lock.json /web/package-lock.json -# COPY./omc_web/package.json /web/package.json -# COPY./omc_web/tsconfig.json /web/tsconfig.json -# COPY./omc_web/tsconfig.node.json /web/tsconfig.node.json -# COPY./omc_web/vite.config.ts /web/vite.config.ts RUN npm config set registry https://registry.npmmirror.com RUN cd /web && npm install && npm run build RUN touch /web/dist/config.js && echo 'sessionStorage.clear()' | tee /web/dist/config.js -# 后端程序 -COPY ./omc_api /api -# COPY ./omc_api/features /api/features -# COPY ./omc_api/lib /api/lib -# COPY ./omc_api/src /api/src -# COPY ./omc_api/sshsvc /api/sshsvc -# COPY ./omc_api/swagger_docs /api/swagger_docs -# COPY ./omc_api/go.sum /api/go.sum -# COPY ./omc_api/go.mod /api/go.mod -# COPY ./omc_api/main.go /api/main.go - -# 系统文件 -COPY ./omc_api/linux /linux - ## 第二阶段 ====> Go打包编译输出后端程序 -FROM --platform=linux/amd64 golang:1.24-alpine AS build-golang +FROM golang:1.24-alpine AS build-golang # 从上个阶段复制文件 COPY --from=build-node /web/dist /web @@ -44,9 +24,9 @@ COPY --from=build-node /linux /linux RUN apk add gcc musl-dev libpcap-dev # 设置变量 -ENV CGO_ENABLED 1 -ENV GOOS linux -ENV GOPROXY https://goproxy.cn,direct +ENV CGO_ENABLED=1 +ENV GOOS=linux +ENV GOPROXY=https://goproxy.cn,direct ARG VERSION RUN cd /api && go mod download @@ -55,23 +35,24 @@ RUN cd /api && go build -o omc -v -ldflags "-s -w \ -X 'be.ems/src/framework/config.BuildTime=$(date)' \ -X 'be.ems/src/framework/config.GoVer=$(go version)'" RUN cd /api/sshsvc && go build -o sshsvc -v -ldflags "-s -w \ - -X 'be.ems/lib/global.Version=${VERSION}' \ + -X 'be.ems/lib/global.Version=$VERSION' \ -X 'be.ems/lib/global.BuildTime=$(date)' \ -X 'be.ems/lib/global.GoVer=$(go version)'" ## 第三阶段 ====> 构建前后端融合镜像 -FROM --platform=linux/amd64 ubuntu:22.04 +FROM ubuntu:22.04 # 更新apt并安装必要工具 -RUN apt update -y && apt install -y \ - sudo systemd libpcap-dev tzdata nginx +RUN apt-get update && apt-get install -y sudo systemd libpcap-dev nginx +RUN DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata +RUN apt-get clean && rm -rf /var/lib/apt/lists/* # 设置时区和语言环境 ENV LANG="en_US.UTF-8" ENV TZ="Asia/Shanghai" - WORKDIR /usr/local/etc/omc + COPY --from=build-golang /linux / COPY --from=build-golang /web /usr/local/etc/omc/web COPY --from=build-golang /api/omc /usr/local/bin/omc @@ -81,7 +62,7 @@ EXPOSE 80 443 33030 CMD ["/sbin/init"] -# 构建 -# docker build --build-arg VERSION=241101 -t omc:2.241101 . -# docker run -it omc:2.241101 sh +# build +# docker build --platform linux/amd64 --build-arg VERSION=2.2505.2 -t omc:2.2505.2 . +# docker run -it omc:2.2505.2 sh # service ssh start && service nginx start && /usr/local/bin/omc --env prod -c /usr/local/etc/omc/omc.yaml