|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | + |
| 3 | +# Copyright 2025 Docker Packaging authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +ARG XX_VERSION="1.6.1" |
| 18 | +ARG DEBIAN_FRONTEND="noninteractive" |
| 19 | + |
| 20 | +ARG DISTRO_NAME="debian12" |
| 21 | +ARG DISTRO_TYPE="deb" |
| 22 | +ARG DISTRO_RELEASE="debian" |
| 23 | +ARG DISTRO_SUITE="bookworm" |
| 24 | +ARG DISTRO_ID="12" |
| 25 | +ARG DISTRO_IMAGE="debian:bookworm" |
| 26 | + |
| 27 | +ARG PKG_NAME="docker-model-plugin" |
| 28 | +ARG PKG_REPO="https://github.com/docker/model-cli.git" |
| 29 | +ARG PKG_REF="main" |
| 30 | + |
| 31 | +ARG GO_IMAGE="golang" |
| 32 | +ARG GO_VERSION="1.24.4" |
| 33 | +ARG GO_IMAGE_VARIANT="bookworm" |
| 34 | + |
| 35 | +# stage used as named context that mounts hack/scripts |
| 36 | +# see pkg target in docker-bake.hcl |
| 37 | +FROM scratch AS scripts |
| 38 | + |
| 39 | +# cross compilation helper |
| 40 | +FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx |
| 41 | + |
| 42 | +# osxcross contains the MacOSX SDK for xx |
| 43 | +FROM dockercore/golang-cross:xx-sdk-extras AS osxsdk |
| 44 | + |
| 45 | +# go base image to retrieve /usr/local/go |
| 46 | +FROM ${GO_IMAGE}:${GO_VERSION}-${GO_IMAGE_VARIANT} AS go |
| 47 | +FROM --platform=$BUILDPLATFORM ${GO_IMAGE}:${GO_VERSION}-${GO_IMAGE_VARIANT} AS gocross |
| 48 | + |
| 49 | +# src |
| 50 | +FROM --platform=$BUILDPLATFORM alpine AS src-base |
| 51 | +RUN apk add --no-cache bash curl file git zip tar |
| 52 | + |
| 53 | +FROM src-base AS src |
| 54 | +WORKDIR /src |
| 55 | +ARG PKG_REPO |
| 56 | +RUN git init . && git remote add origin "${PKG_REPO}" |
| 57 | +ARG PKG_REF |
| 58 | +RUN git fetch origin "${PKG_REF}" +refs/heads/*:refs/remotes/origin/* +refs/tags/*:refs/tags/* && git checkout -q FETCH_HEAD |
| 59 | + |
| 60 | +FROM src-base AS src-tgz |
| 61 | +RUN --mount=from=src,source=/src,target=/model \ |
| 62 | + mkdir /out && tar -C / -zcf /out/model.tgz --exclude .git model |
| 63 | + |
| 64 | +# metadata |
| 65 | +FROM src-base AS metadata-builder |
| 66 | +ARG PKG_REPO |
| 67 | +ARG PKG_REF |
| 68 | +ARG NIGHTLY_BUILD |
| 69 | +RUN --mount=type=bind,from=scripts,source=gen-ver.sh,target=/usr/local/bin/gen-ver \ |
| 70 | + --mount=type=bind,from=src,source=/src,target=/src <<EOT |
| 71 | + set -e |
| 72 | + mkdir -p /out |
| 73 | + for l in $(gen-ver "/src"); do |
| 74 | + export "${l?}" |
| 75 | + done |
| 76 | + cat > "/out/metadata.env" <<-EOF |
| 77 | +REPO=${PKG_REPO%.*} |
| 78 | +REF=${PKG_REF} |
| 79 | +VERSION=${GENVER_VERSION} |
| 80 | +COMMIT=${GENVER_COMMIT} |
| 81 | +EOF |
| 82 | +EOT |
| 83 | + |
| 84 | +FROM scratch AS metadata |
| 85 | +COPY --from=metadata-builder /out / |
| 86 | + |
| 87 | +# deb |
| 88 | +FROM ${DISTRO_IMAGE} AS builder-deb |
| 89 | +COPY --from=xx / / |
| 90 | +ARG DEBIAN_FRONTEND |
| 91 | +ENV GOPROXY="https://proxy.golang.org|direct" |
| 92 | +ENV GOPATH="/go" |
| 93 | +ENV PATH="$PATH:/usr/local/go/bin:$GOPATH/bin" |
| 94 | +ENV GOTOOLCHAIN="local" |
| 95 | +ENV GO111MODULE="on" |
| 96 | +ENV CGO_ENABLED="0" |
| 97 | +ARG DISTRO_NAME |
| 98 | +RUN apt-get update && apt-get install -y --no-install-recommends apt-utils bash ca-certificates curl devscripts equivs git |
| 99 | +COPY deb /root/package/debian |
| 100 | +ARG TARGETPLATFORM |
| 101 | +RUN mk-build-deps -t "xx-apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y" -i /root/package/debian/control |
| 102 | +WORKDIR /root/package |
| 103 | +ARG NIGHTLY_BUILD |
| 104 | +ARG DISTRO_RELEASE |
| 105 | +ARG DISTRO_ID |
| 106 | +ARG DISTRO_SUITE |
| 107 | +ARG PKG_NAME |
| 108 | +ARG PKG_REF |
| 109 | +ARG PKG_PACKAGER |
| 110 | +ARG PKG_DEB_BUILDFLAGS |
| 111 | +ARG PKG_DEB_REVISION |
| 112 | +ARG PKG_DEB_EPOCH |
| 113 | +ARG SOURCE_DATE_EPOCH |
| 114 | +RUN --mount=type=bind,source=scripts/pkg-deb-build.sh,target=/usr/local/bin/pkg-deb-build \ |
| 115 | + --mount=type=bind,from=scripts,source=gen-ver.sh,target=/usr/local/bin/gen-ver \ |
| 116 | + --mount=type=bind,from=scripts,source=gen-deb-changelog.sh,target=/usr/local/bin/gen-deb-changelog \ |
| 117 | + --mount=type=bind,from=scripts,source=fix-cc.sh,target=/usr/local/bin/fix-cc \ |
| 118 | + --mount=type=bind,from=src,source=/src,target=/root/package/model,rw \ |
| 119 | + --mount=type=bind,from=go,source=/usr/local/go,target=/usr/local/go \ |
| 120 | + OUTDIR=/out SRCDIR=./model pkg-deb-build |
| 121 | + |
| 122 | +# rpm |
| 123 | +FROM ${DISTRO_IMAGE} AS builder-rpm |
| 124 | +COPY --from=xx / / |
| 125 | +ENV GOPROXY="https://proxy.golang.org|direct" |
| 126 | +ENV GOPATH="/go" |
| 127 | +ENV PATH="$PATH:/usr/local/go/bin:$GOPATH/bin" |
| 128 | +ENV GOTOOLCHAIN="local" |
| 129 | +ENV GO111MODULE="on" |
| 130 | +ENV CGO_ENABLED="0" |
| 131 | +ARG DISTRO_NAME |
| 132 | +RUN --mount=type=bind,from=scripts,source=rpm-init.sh,target=/usr/local/bin/rpm-init \ |
| 133 | + rpm-init $DISTRO_NAME |
| 134 | +COPY rpm /root/rpmbuild/SPECS |
| 135 | +ARG TARGETPLATFORM |
| 136 | +RUN --mount=type=bind,from=scripts,source=rpm-builddep.sh,target=/usr/local/bin/rpm-builddep \ |
| 137 | + rpm-builddep $(xx-info rhel-arch) /root/rpmbuild/SPECS |
| 138 | +WORKDIR /root/rpmbuild |
| 139 | +ARG NIGHTLY_BUILD |
| 140 | +ARG DISTRO_RELEASE |
| 141 | +ARG DISTRO_ID |
| 142 | +ARG DISTRO_SUITE |
| 143 | +ARG PKG_NAME |
| 144 | +ARG PKG_REF |
| 145 | +ARG PKG_PACKAGER |
| 146 | +ARG PKG_RPM_BUILDFLAGS |
| 147 | +ARG PKG_RPM_RELEASE |
| 148 | +ARG SOURCE_DATE_EPOCH |
| 149 | +RUN --mount=type=bind,source=scripts/pkg-rpm-build.sh,target=/usr/local/bin/pkg-rpm-build \ |
| 150 | + --mount=type=bind,from=scripts,source=gen-ver.sh,target=/usr/local/bin/gen-ver \ |
| 151 | + --mount=type=bind,from=scripts,source=fix-cc.sh,target=/usr/local/bin/fix-cc \ |
| 152 | + --mount=type=bind,from=src-tgz,source=/out/model.tgz,target=/root/rpmbuild/SOURCES/model.tgz \ |
| 153 | + --mount=type=bind,from=src,source=/src,target=/usr/local/src/model \ |
| 154 | + --mount=type=bind,from=go,source=/usr/local/go,target=/usr/local/go \ |
| 155 | + OUTDIR=/out SRCDIR=/usr/local/src/model pkg-rpm-build |
| 156 | + |
| 157 | +# static |
| 158 | +FROM --platform=$BUILDPLATFORM ${DISTRO_IMAGE} AS builder-static-nosdk |
| 159 | +COPY --from=xx / / |
| 160 | +ARG DEBIAN_FRONTEND |
| 161 | +RUN apt-get update && apt-get install -y --no-install-recommends bash ca-certificates file git zip tar \ |
| 162 | + dpkg-dev clang lld llvm make pkg-config |
| 163 | +ENV GOPROXY="https://proxy.golang.org|direct" |
| 164 | +ENV GOPATH="/go" |
| 165 | +ENV PATH="$PATH:/usr/local/go/bin:$GOPATH/bin" |
| 166 | +ENV GOTOOLCHAIN="local" |
| 167 | +ENV GO111MODULE="on" |
| 168 | +ENV CGO_ENABLED="0" |
| 169 | +ARG PKG_NAME |
| 170 | +ARG PKG_REF |
| 171 | +ARG NIGHTLY_BUILD |
| 172 | +WORKDIR /build |
| 173 | +# download dependencies before "ARG TARGETPLATFORM" otherwise it will be cached |
| 174 | +# for each platform and that would take a lot of disk space |
| 175 | +RUN --mount=type=bind,from=src,source=/src/go.mod,target=/build/go.mod \ |
| 176 | + --mount=type=bind,from=src,source=/src/go.sum,target=/build/go.sum,rw \ |
| 177 | + --mount=type=bind,from=gocross,source=/usr/local/go,target=/usr/local/go,rw \ |
| 178 | + go mod download -x |
| 179 | +ARG TARGETPLATFORM |
| 180 | +RUN xx-apt-get install -y gcc libc6-dev |
| 181 | +RUN --mount=type=bind,source=scripts/pkg-static-build.sh,target=/usr/local/bin/pkg-static-build \ |
| 182 | + --mount=type=bind,from=scripts,source=gen-ver.sh,target=/usr/local/bin/gen-ver \ |
| 183 | + --mount=type=bind,from=scripts,source=fix-cc.sh,target=/usr/local/bin/fix-cc \ |
| 184 | + --mount=type=bind,from=src,source=/src,target=/usr/local/src/model \ |
| 185 | + --mount=type=bind,from=gocross,source=/usr/local/go,target=/usr/local/go,rw \ |
| 186 | + OUTDIR=/out BUILDDIR=/build SRCDIR=/usr/local/src/model pkg-static-build |
| 187 | +FROM builder-static-nosdk AS builder-static-linux |
| 188 | +FROM builder-static-nosdk AS builder-static-windows |
| 189 | + |
| 190 | +FROM --platform=$BUILDPLATFORM ${DISTRO_IMAGE} AS builder-static-darwin |
| 191 | +COPY --from=xx / / |
| 192 | +ARG DEBIAN_FRONTEND |
| 193 | +RUN apt-get update && apt-get install -y --no-install-recommends bash ca-certificates file git zip tar \ |
| 194 | + dpkg-dev clang lld llvm make pkg-config |
| 195 | +ENV GOPROXY="https://proxy.golang.org|direct" |
| 196 | +ENV GOPATH="/go" |
| 197 | +ENV PATH="$PATH:/usr/local/go/bin:$GOPATH/bin" |
| 198 | +ENV GOTOOLCHAIN="local" |
| 199 | +ENV GO111MODULE="on" |
| 200 | +ENV CGO_ENABLED="0" |
| 201 | +ARG PKG_NAME |
| 202 | +ARG PKG_REF |
| 203 | +ARG NIGHTLY_BUILD |
| 204 | +WORKDIR /build |
| 205 | +# download dependencies before "ARG TARGETPLATFORM" otherwise it will be cached |
| 206 | +# for each platform and that would take a lot of disk space |
| 207 | +RUN --mount=type=bind,from=src,source=/src/go.mod,target=/build/go.mod \ |
| 208 | + --mount=type=bind,from=src,source=/src/go.sum,target=/build/go.sum,rw \ |
| 209 | + --mount=type=bind,from=gocross,source=/usr/local/go,target=/usr/local/go,rw \ |
| 210 | + go mod download -x |
| 211 | +ARG TARGETPLATFORM |
| 212 | +RUN xx-apt-get install -y gcc libc6-dev |
| 213 | +RUN --mount=type=bind,source=scripts/pkg-static-build.sh,target=/usr/local/bin/pkg-static-build \ |
| 214 | + --mount=type=bind,from=scripts,source=gen-ver.sh,target=/usr/local/bin/gen-ver \ |
| 215 | + --mount=type=bind,from=scripts,source=fix-cc.sh,target=/usr/local/bin/fix-cc \ |
| 216 | + --mount=type=bind,from=src,source=/src,target=/usr/local/src/model \ |
| 217 | + --mount=type=bind,from=gocross,source=/usr/local/go,target=/usr/local/go,rw \ |
| 218 | + --mount=from=osxsdk,target=/xx-sdk,src=/xx-sdk \ |
| 219 | + OUTDIR=/out BUILDDIR=/build SRCDIR=/usr/local/src/model pkg-static-build |
| 220 | + |
| 221 | +FROM builder-static-$TARGETOS AS builder-static |
| 222 | + |
| 223 | +FROM builder-${DISTRO_TYPE} AS build-pkg |
| 224 | +ARG BUILDKIT_SBOM_SCAN_STAGE=true |
| 225 | + |
| 226 | +FROM scratch AS pkg |
| 227 | +COPY --from=build-pkg /out / |
0 commit comments