From d3646c86ecc5004b8c4d5d8ae31609473869c252 Mon Sep 17 00:00:00 2001 From: luke Date: Sun, 24 Aug 2025 22:02:54 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0docker=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 57 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 8 +++++++ pom.xml | 46 ++++++++++++++++--------------------- 3 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a05335e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,57 @@ +# Stage 1: 官方 GraalVM 社区版(已含 native-image) +FROM ghcr.io/graalvm/native-image-community:latest AS builder + +RUN microdnf install -y \ + gcc gcc-c++ make git wget tar gzip which findutils maven \ + && microdnf clean all + +# ---------- 构建 musl ---------- +ARG MUSL_VER=1.2.5 +WORKDIR /tmp +RUN wget -q https://musl.libc.org/releases/musl-${MUSL_VER}.tar.gz \ + && tar -xzf musl-${MUSL_VER}.tar.gz \ + && cd musl-${MUSL_VER} \ + && ./configure --prefix=/opt/musl-${MUSL_VER} --disable-shared \ + && make -j"$(nproc)" \ + && make install \ + && ln -s /opt/musl-${MUSL_VER} /opt/musl \ + && cd / && rm -rf /tmp/musl-${MUSL_VER}* + +RUN ln -s /opt/musl/bin/musl-gcc /usr/local/bin/x86_64-linux-musl-gcc \ + && ln -s /opt/musl/bin/musl-gcc /usr/local/bin/x86_64-linux-musl-cc + +ENV PATH="/opt/musl/bin:${PATH}" +ENV CC="musl-gcc" +ENV C_INCLUDE_PATH="/opt/musl/include" +ENV LIBRARY_PATH="/opt/musl/lib" + +# ---------- 静态 zlib ---------- +ARG ZLIB_VERSION=1.3.1 +WORKDIR /tmp +RUN wget -q https://zlib.net/zlib-${ZLIB_VERSION}.tar.gz \ + && tar -xzf zlib-${ZLIB_VERSION}.tar.gz \ + && cd zlib-${ZLIB_VERSION} \ + && CC=musl-gcc ./configure --static --prefix=/opt/musl \ + && make -j"$(nproc)" \ + && make install \ + && cd / && rm -rf /tmp/zlib-${ZLIB_VERSION}* + +# ---------- Maven 缓存优化 ---------- +WORKDIR /app +COPY pom.xml ./ + +# 先拉依赖并缓存 +RUN mvn -B -P native-linux dependency:go-offline + +# ---------- 复制源码 ---------- +COPY . /app + +# ---------- 编译 native image ---------- +RUN mvn -P native-linux -DskipTests clean package + +# ------------------------------------------------------------ +# Stage 2: 输出产物镜像(可以直接 cp 出二进制) +# ------------------------------------------------------------ +FROM busybox AS export +WORKDIR /export +COPY --from=builder /app/org.jcnc.snow.cli.SnowCLI /export/Snow \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..21bca91 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +services: + snow-export: + image: my-snow-export + build: + context: . + command: [ "/bin/sh", "-c", "cp /export/Snow /output/Snow" ] + volumes: + - ./target:/output diff --git a/pom.xml b/pom.xml index 6b8b27d..e4b48da 100644 --- a/pom.xml +++ b/pom.xml @@ -70,11 +70,7 @@ - + native-linux @@ -82,25 +78,39 @@ unix + org.graalvm.buildtools native-maven-plugin ${native.maven.plugin.version} - true + + + org.jcnc.snow.cli.SnowCLI + Snow + ${project.build.directory} + + --static + --libc=musl + --emit=build-report + -O2 + -H:Class=org.jcnc.snow.cli.SnowCLI + -H:CCompilerPath=/opt/musl/bin/musl-gcc + -H:CLibraryPath=/opt/musl/lib + + + - build-native - compile-no-fork package - + test-native @@ -109,24 +119,6 @@ test - - - - --static - - --libc=musl - - --emit build-report - - -O2 - - - - /opt/musl-1.2.5/bin:${env.PATH} - /opt/musl-1.2.5/include - /opt/musl-1.2.5/lib - -