[Java]制作Java21 Docker镜像 基于Debian和SDKMan 作为编译Graavl二进制程序的基础镜像

1 制作镜像

FROM debian:stable-slim

# aliyun 替换apt 更新源为国内阿里云镜像站
# RUN sed -i s/archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && \
# sed -i s/security.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && \
# sed -i s/cn.archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && \
# sed -i s/ports.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && \
# sed -i s/deb.debian.org/mirrors.aliyun.com/g /etc/apt/sources.list

RUN apt update 
RUN apt install -y libssl-dev \
    autoconf \
    re2c \
    bison \
    libsqlite3-dev \
    libpq-dev \
    libonig-dev \
    libfcgi-dev \
    libfcgi0ldbl \
    libjpeg-dev \
    libpng-dev \
    libssl-dev \
    libxml2-dev \
    libcurl4-openssl-dev \
    libxpm-dev \
    libgd-dev \
    libmariadb-dev \
    libfreetype6-dev \
    libxslt1-dev \
    libpspell-dev \
    libzip-dev \
    libgccjit-10-dev \
    zlib1g-dev libz-dev lib32z1-dev \
    zip unzip curl wget \
    build-essential && \
	rm -rf /var/lib/apt/lists/* && \
	rm -rf /tmp/*

# 安装SDKMan
RUN curl -s "https://get.sdkman.io" | bash

# nik是bellsoft基于GraavlVM 的发行版,有gu执行文件
# 主义是java21不是java23 运行 sdkman list java 看看
ENV SDK_MAN=/root/.sdkman
ENV SDK_MAN_CAN=${SDK_MAN}/candidates
ARG JAVA_VERSION="23.1.1.r21-nik"

# 安装jdk gradle和maven
RUN bash -c "source ${SDK_MAN}/bin/sdkman-init.sh && \
    yes | sdk install java $JAVA_VERSION &&  \
    yes | sdk use java $JAVA_VERSION && \
    yes | sdk default java $JAVA_VERSION && \
    yes | sdk install maven && \
    yes | sdk install gradle && \
    rm -rf ${SDK_MAN}/archives/* && \
    rm -rf ${SDK_MAN}/tmp/*"
    
ENV JAVA_HOME=${SDK_MAN_CAN}/java/current

# 设置环境变量
ENV PATH="${PATH}:${SDK_MAN_CAN}/gradle/current/bin/"
ENV PATH="${PATH}:${SDK_MAN_CAN}/maven/current/bin/"

# Create appuser
ENV USER=app
ENV UID=10001

# 不用root权限运行
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    "${USER}"

2 利用Github Actions 制作Docker Image推送到DockerHub

记得在Github 里 仓库的镜像里设置 DOCKERHUB_TOKEN

touch .github/workflows/main.yml

# on: [push]
name: build-docker-image

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      # -
      #   name: Build and push base
      #   uses: docker/build-push-action@v3
      #   with:
      #     context: ./
      #     file: ./DockerfileBase
      #     push: true
      #     tags: ${{ secrets.DOCKERHUB_USERNAME }}/spring_base:latest
      -
        name: Build and push build
        uses: docker/build-push-action@v3
        with:
          context: ./
          file: ./DockerfileBuild
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/spring_build:latest