zeabur / zbpack

Build your project into static assets, serverless function or container image with magic, no Dockerfile needed!
Mozilla Public License 2.0
277 stars 44 forks source link

zbpack image build v2 #359

Closed pan93412 closed 3 weeks ago

pan93412 commented 1 month ago

Description (required)

I rewrote the image build logic.

First, I decoupled "injection" and "push" in the "build" process:

graph LR;
  subgraph Build
    direction LR
    buildkit[Use buildkit to build a Docker image] -- If serverless --> serverless[Transform to serverless]
    buildkit -- If not serverless --> tar[Output as Docker TAR]
  end

  S((Start)) --> IEV[Inject environment variable]
  IEV --> Build
  tar -.->|If push| skopeo[Upload to registry with skopeo]

Second, I leveraged ARG, target, and the combination of Docker images to simplify the Dockerfile construction. By combining the Docker images, we accelerate the build process.

FROM zeabur/zbpack-dart-flutter-base AS build
ARG build

WORKDIR /app
COPY . .
RUN flutter clean
RUN flutter pub get
RUN ${build}

FROM scratch AS target-static
LABEL com.zeabur.image-type="static"

COPY --from=build /app/build/web /

FROM docker.io/library/caddy AS target-containerized
LABEL com.zeabur.image-type="containerized"

COPY --from=build /app/build/web /usr/share/caddy
╔══════════════════════════════ Build Plan ═════════════════════════════╗
║ provider         │ dart                                               ║
║───────────────────────────────────────────────────────────────────────║
║ framework        │ flutter                                            ║
║───────────────────────────────────────────────────────────────────────║
║ build            │ flutter build web                                  ║
║───────────────────────────────────────────────────────────────────────║
║ zeaburImage      │ dart-flutter                                       ║
║───────────────────────────────────────────────────────────────────────║
║ zeaburImageStage │ target-containerized                               ║
╚═══════════════════════════════════════════════════════════════════════╝
Build successful

The Docker image has been saved in /var/folders/qj/62r8d09n5hn3nm_bdzf0dcpr0000gn/T/zbpack-docker-tar-3950225412/artifact.tar
Build successful                                                                                                                                           

The compiled serverless function has been saved in /tmp/flutter-template/.zeabur

Third, I utilized the LABEL in the Dockerfile to select the transformer for serverless functions. For example:

FROM scratch AS target-serverless
LABEL com.zeabur.image-type="serverless"
LABEL com.zeabur.serverless-transformer="dart-serverless"

COPY --from=build /app/build/web /

Finally, I leverage dockerfile/parser to parse the Dockerfile, ensuring more reliable parsing.

Demo:

docker run -d -p 5000:5000 --restart always --name registry registry:2

# build base images
cd internal/dockerfiles
REGISTRY="localhost:5000" bash build.sh

# copy caddy
docker pull caddy
docker tag caddy localhost:5000/library/caddy
docker push localhost:5000/library/caddy

# build our projects
BUILDKIT_HOST=tcp://0.0.0.0:1234 REGISTRY=registry.orb.local zbpack flutter-template

WIPs:

Related issues & labels (optional)