Optimize Docker image size and split into three base-line image (#895)

Co-authored-by: Jerry8块 <i@jerryzone.cn>
This commit is contained in:
Jerry8块 2025-03-29 21:28:58 +08:00 committed by GitHub
parent 41c90a96fc
commit 8a4c202679
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 226 additions and 14 deletions

View File

@ -8,8 +8,8 @@ on:
- "v[0-9]+.[0-9]+.[0-9]+-*"
jobs:
# Build and push Docker image
docker:
# Build and push ubuntu-based latest image
docker-latest:
runs-on: ubuntu-latest
permissions:
contents: read
@ -41,8 +41,87 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: |
ghcr.io/${{ github.repository }}:latest
ghcr.io/${{ github.repository }}:${{ env.VERSION }}
# Build and push alpine-based dev image
docker-dev:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Extract version from tag
id: get_version
run: |
# Remove 'v' prefix from tag
VERSION=${GITHUB_REF#refs/tags/v}
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64,linux/arm/v7
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.DAGU_GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.dev
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: |
ghcr.io/${{ github.repository }}:dev
ghcr.io/${{ github.repository }}:${{ env.VERSION }}-dev
# Build and push ubuntu-based dev image
docker-alpine:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Extract version from tag
id: get_version
run: |
# Remove 'v' prefix from tag
VERSION=${GITHUB_REF#refs/tags/v}
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64,linux/arm/v7
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.DAGU_GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.alpine
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: |
ghcr.io/${{ github.repository }}:alpine
ghcr.io/${{ github.repository }}:${{ env.VERSION }}-alpine

View File

@ -29,20 +29,8 @@ ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
apt-get install -y \
sudo \
git \
curl \
wget \
zip \
unzip \
sudo \
tzdata \
build-essential \
jq \
python3 \
python3-pip \
openjdk-11-jdk \
nodejs \
npm \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

64
Dockerfile.alpine Normal file
View File

@ -0,0 +1,64 @@
# syntax=docker/dockerfile:1.4
# Stage 1: UI Builder
FROM --platform=$BUILDPLATFORM node:18-alpine as ui-builder
WORKDIR /app
COPY ui/ ./
RUN rm -rf node_modules; \
yarn install --frozen-lockfile --non-interactive; \
yarn build
# Stage 2: Go Builder
FROM --platform=$TARGETPLATFORM golang:1.23-alpine as go-builder
ARG LDFLAGS
ARG TARGETOS
ARG TARGETARCH
WORKDIR /app
COPY . .
RUN go mod download && rm -rf frontend/assets
COPY --from=ui-builder /app/dist/ ./internal/frontend/assets/
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags="${LDFLAGS}" -o ./bin/dagu ./cmd
# Stage 3: Final Image
FROM --platform=$TARGETPLATFORM alpine:3.21.3
ARG USER="dagu"
ARG USER_UID=1000
ARG USER_GID=$USER_UID
# Install common tools
RUN apk update && \
apk add jq tzdata sudo shadow bash && \
rm -rf /var/cache/apk/*
COPY --from=go-builder /app/bin/dagu /usr/local/bin/
COPY ./entrypoint.sh /entrypoint.sh
RUN set -eux; \
# Try to create group with specified GID, fallback if GID in use
(groupadd -g "${USER_GID}" "${USER}" || groupadd "${USER}") && \
# Try to create user with specified UID, fallback if UID in use
(useradd -m -d /config \
-u "${USER_UID}" \
-g "$(getent group "${USER}" | cut -d: -f3)" \
-s /bin/bash \
"${USER}" \
|| useradd -m -d /config \
-g "$(getent group "${USER}" | cut -d: -f3)" \
-s /bin/bash \
"${USER}") && \
chown -R "${USER}:${USER}" /config && \
chmod +x /entrypoint.sh
# Create user and set permissions
RUN echo "dagu ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/99-dagu \
&& chmod 440 /etc/sudoers.d/99-dagu
WORKDIR /config
ENV DAGU_HOST=0.0.0.0
ENV DAGU_PORT=8080
ENV DAGU_TZ="Etc/UTC"
ENV PUID=${USER_UID}
ENV PGID=${USER_GID}
ENV DOCKER_GID=-1
EXPOSE 8080
ENTRYPOINT ["/entrypoint.sh"]
CMD ["dagu", "start-all"]

81
Dockerfile.dev Normal file
View File

@ -0,0 +1,81 @@
# syntax=docker/dockerfile:1.4
# Stage 1: UI Builder
FROM --platform=$BUILDPLATFORM node:18-alpine as ui-builder
WORKDIR /app
COPY ui/ ./
RUN rm -rf node_modules; \
yarn install --frozen-lockfile --non-interactive; \
yarn build
# Stage 2: Go Builder
FROM --platform=$TARGETPLATFORM golang:1.23-alpine as go-builder
ARG LDFLAGS
ARG TARGETOS
ARG TARGETARCH
WORKDIR /app
COPY . .
RUN go mod download && rm -rf frontend/assets
COPY --from=ui-builder /app/dist/ ./internal/frontend/assets/
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags="${LDFLAGS}" -o ./bin/dagu ./cmd
# Stage 3: Final Image
FROM --platform=$TARGETPLATFORM ubuntu:24.04
ARG USER="dagu"
ARG USER_UID=1000
ARG USER_GID=$USER_UID
# Install common tools
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
apt-get install -y \
sudo \
git \
curl \
wget \
zip \
unzip \
sudo \
tzdata \
build-essential \
jq \
python3 \
python3-pip \
openjdk-11-jdk \
nodejs \
npm \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY --from=go-builder /app/bin/dagu /usr/local/bin/
COPY ./entrypoint.sh /entrypoint.sh
RUN set -eux; \
# Try to create group with specified GID, fallback if GID in use
(groupadd -g "${USER_GID}" "${USER}" || groupadd "${USER}") && \
# Try to create user with specified UID, fallback if UID in use
(useradd -m -d /config \
-u "${USER_UID}" \
-g "$(getent group "${USER}" | cut -d: -f3)" \
-s /bin/bash \
"${USER}" \
|| useradd -m -d /config \
-g "$(getent group "${USER}" | cut -d: -f3)" \
-s /bin/bash \
"${USER}") && \
chown -R "${USER}:${USER}" /config && \
chmod +x /entrypoint.sh
# Create user and set permissions
RUN echo "dagu ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/99-dagu \
&& chmod 440 /etc/sudoers.d/99-dagu
WORKDIR /config
ENV DAGU_HOST=0.0.0.0
ENV DAGU_PORT=8080
ENV DAGU_TZ="Etc/UTC"
ENV PUID=${USER_UID}
ENV PGID=${USER_GID}
ENV DOCKER_GID=-1
EXPOSE 8080
ENTRYPOINT ["/entrypoint.sh"]
CMD ["dagu", "start-all"]