From ac23c3a9f1f4de8c2d7be9d05f3ebc07f59338f3 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Mon, 19 May 2025 19:00:59 +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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build-docker.sh | 19 +++--- pkg.sh | 4 +- pkg/docker/Dockerfile.ubuntu22.04 | 66 ++++++++++++------- .../usr/local/etc/omc/script/entrypoint.sh | 30 +++++++++ .../linux/usr/local/etc/omc/script/omc-env.sh | 15 +++++ .../usr/local/etc/omc/script/postunpack.sh | 32 +++++++++ .../linux/usr/local/etc/omc/script/run.sh | 21 ++++++ pkg/docker/{release => tar}/omc-docker.sh | 6 +- 8 files changed, 155 insertions(+), 38 deletions(-) create mode 100644 pkg/docker/linux/usr/local/etc/omc/script/entrypoint.sh create mode 100644 pkg/docker/linux/usr/local/etc/omc/script/omc-env.sh create mode 100644 pkg/docker/linux/usr/local/etc/omc/script/postunpack.sh create mode 100644 pkg/docker/linux/usr/local/etc/omc/script/run.sh rename pkg/docker/{release => tar}/omc-docker.sh (96%) diff --git a/build-docker.sh b/build-docker.sh index 00bb362..0e7698e 100644 --- a/build-docker.sh +++ b/build-docker.sh @@ -48,7 +48,7 @@ if [ -n "$PLATFORM" ]; then arm64) ;; *) echo "Error: platform be: $PLATFORM" - echo "can only be 'amd64' , 'arm64'" + echo "can only be amd64/arm64" exit 1 ;; esac @@ -109,6 +109,7 @@ docker_build() { # Replacement Tags {version} {arch} sed -i "s/{version}/${VERSION}/g" ./${ReleaseFileName}/omc-docker.sh sed -i "s/{arch}/${PLATFORM}/g" ./${ReleaseFileName}/omc-docker.sh + sed -i "s/{system}/${SYSTEM}/g" ./${ReleaseFileName}/omc-docker.sh # check docker image if ! docker images | grep -q "mariadb-$PLATFORM\s*10.6.21"; then @@ -127,15 +128,17 @@ docker_build() { fi # build omc - docker build --platform linux/${PLATFORM} --build-arg VERSION=${VERSION} -t omc:r${VERSION}-${SYSTEM}-${PLATFORM} . - docker save omc:r${VERSION}-${SYSTEM}-${PLATFORM} -o ${BuildDir}/${ReleaseFileName}/tar/omc_r${VERSION}-${SYSTEM}-${PLATFORM}.tar + docker build --platform linux/${PLATFORM} --build-arg TARGETARCH=${PLATFORM} --build-arg VERSION=${VERSION} -t omc:${VERSION}-${SYSTEM}-${PLATFORM} . + docker save omc:${VERSION}-${SYSTEM}-${PLATFORM} -o ${BuildDir}/${ReleaseFileName}/tar/omc_${VERSION}-${SYSTEM}-${PLATFORM}.tar # tar package - tar -czvf ${ReleaseDir}/${ReleaseFileName}.tar ./${ReleaseFileName} + output=${ReleaseDir}/${ReleaseFileName}.tar.gz + tar -czf ${output} ./${ReleaseFileName} + echo "tar output ${output}" # Generate MD5 file rm -f omc_md5sum.txt - md5sum ${ReleaseDir}/${ReleaseFileName}.tar >${ReleaseDir}/omc_md5sum.txt + md5sum ${output} >${ReleaseDir}/omc_md5sum.txt cat ${ReleaseDir}/omc_md5sum.txt } @@ -144,10 +147,10 @@ docker_build() { echo echo "building omc..." mkdir -p ${BuildDir}/${ReleaseFileName}/tar -cp -rf ${BuildPackagelDir}/docker/Dockerfile.${SYSTEM} ${BuildDir}/Dockerfile -cp -rf ${BuildPackagelDir}/docker/release/* ${BuildDir}/${ReleaseFileName} cp -rf ${BuildLinuxDir} ${BuildDir} -ls -ls ${BuildDir} +cp -rf ${BuildPackagelDir}/docker/linux ${BuildDir} +cp -rf ${BuildPackagelDir}/docker/tar/* ${BuildDir}/${ReleaseFileName} +cp -rf ${BuildPackagelDir}/docker/Dockerfile.${SYSTEM} ${BuildDir}/Dockerfile app docker_build diff --git a/pkg.sh b/pkg.sh index 7cfc62a..c19f595 100644 --- a/pkg.sh +++ b/pkg.sh @@ -56,8 +56,8 @@ if [ -n "$BuildWeb" ]; then git pull # remote replication - # scpDir=/root/omc.git/fe.ems.vue3 - # scp -r -P 18422 root@192.168.9.58:$WebScpDir/dist $WebDir/ + #scpDir=/root/omc.git/fe.ems.vue3 + #scp -r -P 18422 root@192.168.9.58:$scpDir/dist $WebDir/ # local compilation npm install --registry https://registry.npmmirror.com npm run build diff --git a/pkg/docker/Dockerfile.ubuntu22.04 b/pkg/docker/Dockerfile.ubuntu22.04 index e995d70..85b8085 100644 --- a/pkg/docker/Dockerfile.ubuntu22.04 +++ b/pkg/docker/Dockerfile.ubuntu22.04 @@ -9,65 +9,81 @@ COPY ./omc_api /api COPY ./omc_web /web 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 ## 第二阶段 ====> Go打包编译输出后端程序 -FROM golang:1.24-alpine AS build-golang +FROM ubuntu:22.04 AS build-golang +ARG TARGETARCH +ARG VERSION # 从上个阶段复制文件 COPY --from=build-node /web/dist /web COPY --from=build-node /api /api COPY --from=build-node /linux /linux -# 更换镜像源 -RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories -# 安装其他依赖 -RUN apk update && apk add gcc musl-dev libpcap-dev && rm -rf /var/cache/apk/* +# 安装 Go 和其他依赖 +RUN apt-get update && \ + apt-get install -y build-essential libpcap-dev curl \ + && curl -OL https://dl.google.com/go/go1.24.2.linux-$TARGETARCH.tar.gz \ + && tar -C /usr/local -xzf go1.24.2.linux-$TARGETARCH.tar.gz \ + && rm go1.24.2.linux-$TARGETARCH.tar.gz \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* # 设置变量 +ENV PATH="/usr/local/go/bin:${PATH}" +ENV OS_ARCH="${TARGETARCH:-amd64}" ENV CGO_ENABLED=1 ENV GOOS=linux ENV GOPROXY=https://goproxy.cn,direct -ARG VERSION RUN cd /api && go mod download RUN cd /api && go build -o omc -v -ldflags "-s -w \ -X 'be.ems/src/framework/config.Version=$VERSION' \ -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.BuildTime=$(date)' \ - -X 'be.ems/lib/global.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.BuildTime=$(date)' \ +# -X 'be.ems/lib/global.GoVer=$(go version)'" ## 第三阶段 ====> 构建前后端融合镜像 FROM ubuntu:22.04 +# 从上个阶段复制文件 +COPY --from=build-golang /linux/usr/local/etc/omc /usr/local/etc/omc +COPY --from=build-golang /linux/usr/local/omc /usr/local/omc +COPY --from=build-golang /api/omc /usr/local/bin/omc +# COPY --from=build-golang /api/sshsvc/sshsvc /usr/local/bin/sshsvc +COPY --from=build-golang /web /usr/local/etc/omc/web + # 更新apt并安装必要工具 -RUN apt-get update && apt-get install -y sudo systemd libpcap-dev nginx +RUN apt-get update && apt-get install -y libpcap-dev nginx RUN DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata -RUN apt-get clean && rm -rf /var/lib/apt/lists/* +RUN apt-get clean && rm -rf /var/lib/apt/lists /var/cache/apt/archives # 设置时区和语言环境 ENV LANG="en_US.UTF-8" ENV TZ="Asia/Shanghai" WORKDIR /usr/local/etc/omc -COPY --from=build-golang /linux/lib/systemd/system/* /lib/systemd/system -COPY --from=build-golang /linux/usr/local/* /usr/local -COPY --from=build-golang /api/omc /usr/local/bin/omc -COPY --from=build-golang /api/sshsvc/sshsvc /usr/local/bin/sshsvc -COPY --from=build-golang /web /usr/local/etc/omc/web +SHELL ["/bin/bash", "-o", "errexit", "-o", "nounset", "-o", "pipefail", "-c"] +RUN /usr/local/etc/omc/script/postunpack.sh -EXPOSE 80 443 33030 +EXPOSE 80 443 33030 33443 33033 33034 33060 -CMD ["/sbin/init"] +USER 1001 +ENTRYPOINT [ "/usr/local/etc/omc/script/entrypoint.sh" ] +CMD [ "/usr/local/etc/omc/script/run.sh" ] + +# build 546 MB +# docker docker build --platform linux/amd64 --build-arg TARGETARCH=amd64 --build-arg VERSION=2.2505.2 -t omc:r2.2505.2-ubuntu22.04-amd64 . -# 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 -# docker run --rm omc:r1.0.0-docker-ubuntu22.04-amd64 bash -# docker run --privileged=true --restart=always -m 512M --name omc_2.2505.2 -d omc:r1.0.0-docker-ubuntu22.04-amd64 +# docker run -it omc:r2.2505.2-ubuntu22.04-amd64 /bin/bash + +# docker run --privileged=true --restart=always -m 512M --name omc_2.2505.2 -d omc:r2.2505.2-ubuntu22.04-amd64 + +# docker rm -f omc_2.2505.2 +# docker run -it omc_2.2505.2 bash diff --git a/pkg/docker/linux/usr/local/etc/omc/script/entrypoint.sh b/pkg/docker/linux/usr/local/etc/omc/script/entrypoint.sh new file mode 100644 index 0000000..45acccf --- /dev/null +++ b/pkg/docker/linux/usr/local/etc/omc/script/entrypoint.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# shellcheck disable=SC1091 + +set -o errexit +set -o nounset +set -o pipefail +# set -o xtrace # Uncomment this line for debugging purposes + +# Load OMC environment variables +. /usr/local/etc/omc/script/omc-env.sh + +# Initialize OMC DB +if [ ! -f /usr/local/etc/omc/omc.conf ]; then + bash /usr/local/etc/omc/script/setup.sh -i -m $M_PARAM -c $C_PARAM +fi + +# We add the copy from default config in the entrypoint to not break users +# case someone mounts a configuration file in /usr/local/etc/omc/default) +cp -nr "$BASE_DIR"/default/. "$BASE_DIR" + +if [[ "$*" = *"/usr/local/etc/omc/script/run.sh"* || "$*" = *"run.sh"* ]]; then + # Ensure OMC is initialized + omcd version + # Start Nginx server + nginx -g "daemon off;" +fi + +echo "" +exec "$@" \ No newline at end of file diff --git a/pkg/docker/linux/usr/local/etc/omc/script/omc-env.sh b/pkg/docker/linux/usr/local/etc/omc/script/omc-env.sh new file mode 100644 index 0000000..eb07258 --- /dev/null +++ b/pkg/docker/linux/usr/local/etc/omc/script/omc-env.sh @@ -0,0 +1,15 @@ +# Paths +export BASE_DIR="/usr/local/etc/omc" +export DATA_DIR="/usr/local/omc" + +# OMC settings +export M_PARAM="${M_PARAM:std}" +export C_PARAM="${C_PARAM:-omc}" + +# OMC Config settings +export API_HTTP_PORT="${API_HTTP_PORT:-33030}" +export API_HTTPS_PORT="${API_HTTPS_PORT:-33443}" +export WEB_HTTP_PORT="${WEB_HTTP_PORT:-80}" +export WEB_HTTPS_PORT="${WEB_HTTPS_PORT:-443}" + +export CMD_EXTRA_FLAGS="" diff --git a/pkg/docker/linux/usr/local/etc/omc/script/postunpack.sh b/pkg/docker/linux/usr/local/etc/omc/script/postunpack.sh new file mode 100644 index 0000000..af2f38f --- /dev/null +++ b/pkg/docker/linux/usr/local/etc/omc/script/postunpack.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# shellcheck disable=SC1091 + +set -o errexit +set -o nounset +set -o pipefail +# set -o xtrace # Uncomment this line for debugging purposes + +# Load OMC environment variables +. /usr/local/etc/omc/script/omc-env.sh + +chmod -R g+rwX "$BASE_DIR" "$DATA_DIR" + +# We add the copy from default config in the entrypoint to not break users +# bypassing the setup.sh logic. If the file already exists do not overwrite (in +# case someone mounts a configuration file in /usr/local/etc/omc/default) +cp -rf /usr/local/etc/omc/default/* "$BASE_DIR" + +# OMC config +sed -i 's/port: 33030/port: $API_HTTP_PORT/' "$BASE_DIR"/omc.yaml +sed -i 's/port: 33443/port: $API_HTTPS_PORT/' "$BASE_DIR"/omc.yaml +sed -i '/webServer:/,/-/s/^\( *enabled:\) true/\1 false/' "$BASE_DIR"/omc.yaml + +# Nginx config +sed -i 's/# gzip_/gzip_/' /etc/nginx/nginx.conf +sed -i 's/include \/etc\/nginx\/sites-enabled/# include \/etc\/nginx\/sites-enabled/' /etc/nginx/nginx.conf +cp -rf /usr/local/etc/omc/nginx/omc.conf /etc/nginx/conf.d +touch /usr/local/etc/omc/web/dist/config.js && echo 'sessionStorage.clear()' | tee /web/dist/config.js +sed -i 's/33030;/$API_HTTP_PORT;/' /etc/nginx/conf.d/omc.conf +sed -i 's/33443;/$API_HTTPS_PORT;/' /etc/nginx/conf.d/omc.conf +sed -i 's/80;/$WEB_HTTP_PORT;/' /etc/nginx/conf.d/omc.conf +sed -i 's/443;/$WEB_HTTPS_PORT;/' /etc/nginx/conf.d/omc.conf diff --git a/pkg/docker/linux/usr/local/etc/omc/script/run.sh b/pkg/docker/linux/usr/local/etc/omc/script/run.sh new file mode 100644 index 0000000..87f90ec --- /dev/null +++ b/pkg/docker/linux/usr/local/etc/omc/script/run.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# shellcheck disable=SC1091 + +set -o errexit +set -o nounset +set -o pipefail +# set -o xtrace # Uncomment this line for debugging purposes + +# Load OMC environment variables +. /usr/local/etc/omc/script/omc-env.sh + +# Parse CLI flags to pass to the 'omc' call +args=("--config" "${BASE_DIR}/omc.conf") +# Add flags specified via the 'CMD_EXTRA_FLAGS' environment variable +read -r -a extra_flags <<< "$CMD_EXTRA_FLAGS" +[[ "${#extra_flags[@]}" -gt 0 ]] && args+=("${extra_flags[@]}") +# Add flags passed to this script +args+=("$@") + +exec omc "${args[@]}" diff --git a/pkg/docker/release/omc-docker.sh b/pkg/docker/tar/omc-docker.sh similarity index 96% rename from pkg/docker/release/omc-docker.sh rename to pkg/docker/tar/omc-docker.sh index aa65102..3c65500 100644 --- a/pkg/docker/release/omc-docker.sh +++ b/pkg/docker/tar/omc-docker.sh @@ -1,6 +1,6 @@ #!/bin/bash -OMC_CONTAINER_NAME="omc_{version}_{arch}" +OMC_CONTAINER_NAME="omc_{version}-{system}-{arch}" MYSQL_CONTAINER_NAME="omc_mariadb_10.6.21_{arch}" REDIS_CONTAINER_NAME="omc_keydb_6.3.4_{arch}" @@ -16,9 +16,9 @@ install(){ echo "Install mysql and redis service program, default N (Y/N):" read DB_SERVICE DB_SERVICE=${DB_SERVICE:-"N"} - echo "Select the image in the tar directory (omc_{version}_{arch}.tar):" + echo "Select the image in the tar directory (omc_{version}-{system}-{arch}.tar):" read OMC_FILE - OMC_FILE=${OMC_FILE:-"omc_{version}_{arch}.tar"} + OMC_FILE=${OMC_FILE:-"omc_{version}-{system}-{arch}.tar"} echo "Container time zone (Asia/Shanghai):" read OMC_TZ OMC_TZ=${OMC_TZ:-"Asia/Shanghai"}