Skip to content

Instantly share code, notes, and snippets.

@tbvinh
Last active November 4, 2025 06:24
Show Gist options
  • Select an option

  • Save tbvinh/0fea071e1b6856cf90610e2d2cec5e9e to your computer and use it in GitHub Desktop.

Select an option

Save tbvinh/0fea071e1b6856cf90610e2d2cec5e9e to your computer and use it in GitHub Desktop.

Create springboot - docker - graalvm

docker build -f  Dockerfile-native-image -t native-builder .

docker create --name temp-native native-builder
docker cp temp-native:/app/target/native-demo ./target/native-demo
docker rm temp-native

docker build -f Dockerfile-bookworm -t bookworm-runtime .
docker run --rm bookworm-runtime

build dockerfile thành image -t=native-builder

docker build -f  Dockerfile-native-image-mini -t native-builder .

Chạy springboot trong docker với thư mục user/tmp

docker run --rm native-builder -Djava.io.tmpdir=/home/user/tmp

chạy bash trong dockerd, để xem

docker run --rm -it --entrypoint sh native-builder

đổi port ngoài 8880, port nội bộ của docker là 8080

docker run --rm -p 8880:8080  native-builder

Dockerfile-native-image

FROM ghcr.io/graalvm/native-image-community:latest

# Cài thư viện để build static

RUN microdnf install -y \
    gcc \
    gcc-c++ \
    zlib-devel \
    maven \
    bash \
    && microdnf clean all

WORKDIR /app
COPY . .



RUN mvn clean package -Pnative

Dockerfile-native-image-mini
# ----------------------------------------------------------------------
# STAGE 1: BUILDER - Biên dịch ứng dụng thành Native Executable
# SỬ DỤNG JAVA 25: Đã cập nhật tag GraalVM Community Edition lên `:25`
FROM ghcr.io/graalvm/native-image-community:latest AS builder

# Đặt thư mục làm việc và sao chép mã nguồn
WORKDIR /app
COPY . .

# Cài đặt các thư viện C/C++ cần thiết để GraalVM thực hiện liên kết (linking)
# ĐÃ THÊM LẠI MAVEN: Cần thiết để chạy lệnh 'mvn'
RUN microdnf install -y \
        gcc \
        gcc-c++ \
        zlib-devel \
        maven \
        bash \
    && microdnf clean all \
    && rm -rf /var/cache/microdnf

# Bước xây dựng: Biên dịch ứng dụng thành executable
# Kết quả executable sẽ nằm trong thư mục target/
RUN mvn clean package -Pnative

# ----------------------------------------------------------------------
# STAGE 2: RUNNER - Image cuối cùng siêu nhỏ gọn (Super-Minimal)
# Sử dụng base image 'scratch' (image rỗng) để đạt kích thước nhỏ nhất
# CHỈ những gì được COPY từ stage 'builder' mới tồn tại trong image cuối cùng.
FROM alpine:latest

# Tải các thư viện C cần thiết từ builder image (glibc, libstdc++)
COPY --from=builder /usr/lib64/libz.so.1 /usr/lib64/
COPY --from=builder /usr/lib64/libstdc++.so.6 /usr/lib64/
# Copy các thư viện C cơ sở quan trọng nhất (như glibc)
COPY --from=builder /usr/lib64/libc.so.6 /usr/lib64/
COPY --from=builder /usr/lib64/libdl.so.2 /usr/lib64/
COPY --from=builder /usr/lib64/libm.so.6 /usr/lib64/
COPY --from=builder /usr/lib64/libpthread.so.0 /usr/lib64/
COPY --from=builder /usr/lib64/librt.so.1 /usr/lib64/
COPY --from=builder /lib64/ld-linux-x86-64.so.2 /lib64/

# Đặt thư mục làm việc và sao chép executable
WORKDIR /app

# Sao chép executable đã được biên dịch từ Stage 1 (builder)
COPY --from=builder /app/target/ /app/target/

RUN mkdir -p /home/user/tmp && chmod 777 /home/user/tmp

# Lấy executable và đổi tên thành 'application'
# ĐÃ CẬP NHẬT: Sử dụng tên artifact "native-demo-runner"
COPY --from=builder /app/target/native-demo /app/application

RUN mkdir -p /home/user/tmp

# Cấu hình cổng mặc định (nếu ứng dụng là web)
EXPOSE 8080

# Điểm vào (ENTRYPOINT) để chạy ứng dụng Native Image
# Cần set LD_LIBRARY_PATH để runner tìm thấy các thư viện C vừa copy
# ĐÃ SỬA CẢNH BÁO: Thay thế cú pháp nối chuỗi để tránh cảnh báo UndefinedVar

# 4. Cấu hình Runtime
RUN adduser -D appuser
USER appuser
ENV JAVA_TOOL_OPTIONS="-Djava.io.tmpdir=/home/user/tmp"


ENV LD_LIBRARY_PATH=/usr/lib64
ENTRYPOINT ["/app/application"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment