From 46bc90c2e488dcc4cb595d1c01a1dd1e36e1eea0 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Tue, 20 May 2025 18:47:08 +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 | 4 +- pkg/docker/Dockerfile.alpine3.20 | 81 +++++++++++++++++++ pkg/docker/Dockerfile.ubuntu22.04 | 18 +++-- .../usr/local/etc/omc/script/entrypoint.sh | 28 ++++--- .../linux/usr/local/etc/omc/script/omc-env.sh | 2 +- .../usr/local/etc/omc/script/postunpack.sh | 15 ++-- .../linux/usr/local/etc/omc/script/run.sh | 21 ----- pkg/docker/tar/omc-docker.sh | 13 +-- 8 files changed, 128 insertions(+), 54 deletions(-) create mode 100644 pkg/docker/Dockerfile.alpine3.20 delete mode 100644 pkg/docker/linux/usr/local/etc/omc/script/run.sh diff --git a/build-docker.sh b/build-docker.sh index 0e7698e..6db7fb7 100644 --- a/build-docker.sh +++ b/build-docker.sh @@ -22,7 +22,7 @@ usage() { echo "Build Software Package OPTION:" echo " -v, --version Specify the version" echo " -p, --platform Specify the platform architecture (amd64,arm64)" - echo " -s, --system Specify the system image (ubuntu22.04)" + echo " -s, --system Specify the system image (ubuntu22.04,alpine3.20)" echo " -h Display this help message" echo exit 1 @@ -155,4 +155,4 @@ app docker_build -# bash build-docker.sh --version 2.2505.2 --platform arm64 --system ubuntu22.04 +# bash build-docker.sh --version 2.2505.2 --platform amd64 --system ubuntu22.04 diff --git a/pkg/docker/Dockerfile.alpine3.20 b/pkg/docker/Dockerfile.alpine3.20 new file mode 100644 index 0000000..15540da --- /dev/null +++ b/pkg/docker/Dockerfile.alpine3.20 @@ -0,0 +1,81 @@ +## 第一阶段 ====> Nodejs打包编译输出前端资源 +FROM node:20-alpine AS build-node + +# 系统文件 +COPY ./linux /linux +# 后端程序 +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 + + +## 第二阶段 ====> Go打包编译输出后端程序 +FROM golang:1.24-alpine 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 + +# 设置变量 +ARG VERSION +ENV CGO_ENABLED 1 +ENV GOOS linux +ENV GOPROXY https://goproxy.cn,direct + +# 更新apt并安装必要工具 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk add gcc musl-dev libpcap-dev && rm -rf /var/cache/apk/* + +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)'" + + +## 第三阶段 ====> 构建前后端融合镜像 +FROM alpine:3.20 + +# 从上个阶段复制文件 +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 sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk add musl-dev libpcap-dev nginx tzdata && rm -rf /var/cache/apk/* + +# 设置时区和语言环境 +ENV LANG="en_US.UTF-8" +ENV TZ="Asia/Shanghai" +WORKDIR /usr/local/etc/omc + +SHELL ["/bin/bash", "-o", "errexit", "-o", "nounset", "-o", "pipefail", "-c"] +RUN chmod +x /usr/local/etc/omc/script/*.sh +RUN /usr/local/etc/omc/script/postunpack.sh + +EXPOSE 80 443 33030 33443 33033 33034 33060 + +CMD [ "/usr/local/etc/omc/script/entrypoint.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 . + +# service ssh start && service nginx start && /usr/local/bin/omc --env prod -c /usr/local/etc/omc/omc.yaml +# 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/Dockerfile.ubuntu22.04 b/pkg/docker/Dockerfile.ubuntu22.04 index 85b8085..8741ef3 100644 --- a/pkg/docker/Dockerfile.ubuntu22.04 +++ b/pkg/docker/Dockerfile.ubuntu22.04 @@ -69,21 +69,27 @@ ENV TZ="Asia/Shanghai" WORKDIR /usr/local/etc/omc SHELL ["/bin/bash", "-o", "errexit", "-o", "nounset", "-o", "pipefail", "-c"] +RUN chmod +x /usr/local/etc/omc/script/*.sh RUN /usr/local/etc/omc/script/postunpack.sh EXPOSE 80 443 33030 33443 33033 33034 33060 -USER 1001 -ENTRYPOINT [ "/usr/local/etc/omc/script/entrypoint.sh" ] -CMD [ "/usr/local/etc/omc/script/run.sh" ] +CMD [ "/usr/local/etc/omc/script/entrypoint.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 . # service ssh start && service nginx start && /usr/local/bin/omc --env prod -c /usr/local/etc/omc/omc.yaml -# 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 run -it omc:2.2505.2-ubuntu22.04-amd64 /bin/bash +# docker run --privileged=true --restart=always -m 512M -e M_PARAM=lite --name omc_2.2505.2 -d omc:2.2505.2-ubuntu22.04-amd64 +# docker run --privileged=true --restart=always -m 512M \ +# -e TZ=Asia/Shanghai \ +# -e M_PARAM=lite \ +# -p 80:80 \ +# -p 443:443 \ +# --network omcnet \ +# --name omc \ +# -d omc:2.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 index 45acccf..003c6fe 100644 --- a/pkg/docker/linux/usr/local/etc/omc/script/entrypoint.sh +++ b/pkg/docker/linux/usr/local/etc/omc/script/entrypoint.sh @@ -10,21 +10,25 @@ set -o pipefail # 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;" +# Initialize OMC DB +if [ ! -f /usr/local/etc/omc/machine.ini ]; then + bash /usr/local/etc/omc/script/setup.sh -i -m $M_PARAM -c $C_PARAM fi -echo "" -exec "$@" \ No newline at end of file +# Start Nginx server +nginx -g "daemon off;" + +# 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+=("$@") + +omc --version +exec omc "${args[@]}" 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 index eb07258..872b0e1 100644 --- a/pkg/docker/linux/usr/local/etc/omc/script/omc-env.sh +++ b/pkg/docker/linux/usr/local/etc/omc/script/omc-env.sh @@ -3,7 +3,7 @@ export BASE_DIR="/usr/local/etc/omc" export DATA_DIR="/usr/local/omc" # OMC settings -export M_PARAM="${M_PARAM:std}" +export M_PARAM="${M_PARAM:-std}" export C_PARAM="${C_PARAM:-omc}" # OMC Config settings diff --git a/pkg/docker/linux/usr/local/etc/omc/script/postunpack.sh b/pkg/docker/linux/usr/local/etc/omc/script/postunpack.sh index af2f38f..14ed9de 100644 --- a/pkg/docker/linux/usr/local/etc/omc/script/postunpack.sh +++ b/pkg/docker/linux/usr/local/etc/omc/script/postunpack.sh @@ -17,16 +17,19 @@ chmod -R g+rwX "$BASE_DIR" "$DATA_DIR" 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 "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 +sed -i '/type: "mysql"/,/-/s/^\( *host:\) "127.0.0.1"/\1 "omc_mariadb"/' "$BASE_DIR"/omc.yaml +sed -i 's/port: 33066/port: 3306/' "$BASE_DIR"/omc.yaml +sed -i '/port: 6379 # Redis port/,/-/s/^\( *host:\) "127.0.0.1"/\1 "omc_keydb"/' "$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 +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 deleted file mode 100644 index 87f90ec..0000000 --- a/pkg/docker/linux/usr/local/etc/omc/script/run.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/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/tar/omc-docker.sh b/pkg/docker/tar/omc-docker.sh index 3c65500..d43ba5b 100644 --- a/pkg/docker/tar/omc-docker.sh +++ b/pkg/docker/tar/omc-docker.sh @@ -1,8 +1,8 @@ #!/bin/bash -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}" +OMC_CONTAINER_NAME="omc" +MYSQL_CONTAINER_NAME="omc_mariadb" +REDIS_CONTAINER_NAME="omc_keydb" # usage usage() { @@ -62,9 +62,10 @@ install(){ docker load --input $(pwd)/tar/mariadb_10.6.21_{arch}.tar MYSQL_IMAGE="mariadb-{arch}:10.6.21" MYSQL_ROOT_PASSWORD="1000omc@kp!" - SQL_FILE_PATH="$(pwd)/sql/install/omc_db.sql" + # SQL_FILE_PATH="$(pwd)/sql/install/omc_db.sql" MYSQL_DATA=/usr/local/etc/$MYSQL_CONTAINER_NAME/data - mkdir -p $MYSQL_DATA + mkdir -p $MYSQL_DATA && chmod 777 $MYSQL_DATA + echo "CREATE DATABASE IF NOT EXISTS omc_db;" >> $MYSQL_DATA/database.sql docker run --privileged=true --restart=always -e TZ="$OMC_TZ" \ -e MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD \ -v $SQL_FILE_PATH:/docker-entrypoint-initdb.d/database.sql \ @@ -84,7 +85,7 @@ install(){ REDIS_IMAGE="keydb-{arch}:6.3.4" REDIS_PASSWORD="helloearth" REDIS_DATA=/usr/local/etc/$REDIS_CONTAINER_NAME/data - mkdir -p $REDIS_DATA + mkdir -p $REDIS_DATA && chmod 777 $REDIS_DATA docker run --privileged=true --restart=always -e TZ="$OMC_TZ" \ -e KEYDB_PASSWORD=$REDIS_PASSWORD \ -v $REDIS_DATA:/bitnami/keydb/data \