-
Notifications
You must be signed in to change notification settings - Fork 2.1k
dockerfile based binary building #2993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6423da8
dockerfile based binary building
tonistiigi 8b822c9
update windows resources generation
tonistiigi a2a1de5
add windows/arm64 target
tonistiigi bd3e853
update circleci cross target
tonistiigi 706e857
remove unused targets
tonistiigi b099c9c
update readme with new examples
tonistiigi 26b633d
set default version from git
tonistiigi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| .circleci | ||
| .dockerignore | ||
| .git | ||
| .github | ||
| .gitignore | ||
| appveyor.yml | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #syntax=docker/dockerfile:1.2 | ||
|
|
||
| ARG BASE_VARIANT=alpine | ||
| ARG GO_VERSION=1.13.15 | ||
|
|
||
| FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} AS gostable | ||
| FROM --platform=$BUILDPLATFORM golang:1.16-${BASE_VARIANT} AS golatest | ||
|
|
||
| FROM gostable AS go-linux | ||
| FROM golatest AS go-darwin | ||
| FROM golatest AS go-windows-amd64 | ||
| FROM golatest AS go-windows-386 | ||
| FROM golatest AS go-windows-arm | ||
| FROM --platform=$BUILDPLATFORM tonistiigi/golang:497feff1-${BASE_VARIANT} AS go-windows-arm64 | ||
| FROM go-windows-${TARGETARCH} AS go-windows | ||
|
|
||
| FROM --platform=$BUILDPLATFORM tonistiigi/xx@sha256:620d36a9d7f1e3b102a5c7e8eff12081ac363828b3a44390f24fa8da2d49383d AS xx | ||
|
|
||
| FROM go-${TARGETOS} AS build-base-alpine | ||
| COPY --from=xx / / | ||
| RUN apk add --no-cache clang lld llvm file git | ||
| WORKDIR /go/src/github.com/docker/cli | ||
|
|
||
| FROM build-base-alpine AS build-alpine | ||
| ARG TARGETPLATFORM | ||
| # gcc is installed for libgcc only | ||
| RUN xx-apk add --no-cache musl-dev gcc | ||
|
|
||
| FROM go-${TARGETOS} AS build-base-buster | ||
| COPY --from=xx / / | ||
| RUN apt-get update && apt-get install --no-install-recommends -y clang lld file | ||
| WORKDIR /go/src/github.com/docker/cli | ||
|
|
||
| FROM build-base-buster AS build-buster | ||
| ARG TARGETPLATFORM | ||
| RUN xx-apt install --no-install-recommends -y libc6-dev libgcc-8-dev | ||
|
|
||
| FROM build-${BASE_VARIANT} AS build | ||
| # GO_LINKMODE defines if static or dynamic binary should be produced | ||
| ARG GO_LINKMODE=static | ||
| # GO_BUILDTAGS defines additional build tags | ||
| ARG GO_BUILDTAGS | ||
| # GO_STRIP strips debugging symbols if set | ||
| ARG GO_STRIP | ||
| # CGO_ENABLED manually sets if cgo is used | ||
| ARG CGO_ENABLED | ||
| # VERSION sets the version for the produced binary | ||
| ARG VERSION | ||
| RUN --mount=ro --mount=type=cache,target=/root/.cache \ | ||
| --mount=from=dockercore/golang-cross:xx-sdk-extras,target=/xx-sdk,src=/xx-sdk \ | ||
| --mount=type=tmpfs,target=cli/winresources \ | ||
| xx-go --wrap && \ | ||
| # export GOCACHE=$(go env GOCACHE)/$(xx-info)$([ -f /etc/alpine-release ] && echo "alpine") && \ | ||
| TARGET=/out ./scripts/build/binary && \ | ||
| xx-verify $([ "$GO_LINKMODE" = "static" ] && echo "--static") /out/docker | ||
|
|
||
| FROM build-base-${BASE_VARIANT} AS dev | ||
| COPY . . | ||
|
|
||
| FROM scratch AS binary | ||
| COPY --from=build /out . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| variable "VERSION" { | ||
| default = "" | ||
| } | ||
|
|
||
| variable "USE_GLIBC" { | ||
| default = "" | ||
| } | ||
|
|
||
| variable "STRIP_TARGET" { | ||
| default = "" | ||
| } | ||
|
|
||
| group "default" { | ||
| targets = ["binary"] | ||
| } | ||
|
|
||
| target "binary" { | ||
| target = "binary" | ||
| platforms = ["local"] | ||
| output = ["build"] | ||
| args = { | ||
| BASE_VARIANT = USE_GLIBC != "" ? "buster" : "alpine" | ||
| VERSION = VERSION | ||
| GO_STRIP = STRIP_TARGET | ||
| } | ||
| } | ||
|
|
||
| target "dynbinary" { | ||
| inherits = ["binary"] | ||
| args = { | ||
| GO_LINKMODE = "dynamic" | ||
| } | ||
| } | ||
|
|
||
| variable "GROUP_TOTAL" { | ||
| default = "1" | ||
| } | ||
|
|
||
| variable "GROUP_INDEX" { | ||
| default = "0" | ||
| } | ||
|
|
||
| function "platforms" { | ||
| params = [USE_GLIBC] | ||
| result = concat(["linux/amd64", "linux/386", "linux/arm64", "linux/arm", "linux/ppc64le", "linux/s390x", "darwin/amd64", "darwin/arm64", "windows/amd64", "windows/arm", "windows/386"], USE_GLIBC!=""?[]:["windows/arm64"]) | ||
| } | ||
|
|
||
| function "glen" { | ||
| params = [platforms, GROUP_TOTAL] | ||
| result = ceil(length(platforms)/GROUP_TOTAL) | ||
| } | ||
|
|
||
| target "_all_platforms" { | ||
| platforms = slice(platforms(USE_GLIBC), GROUP_INDEX*glen(platforms(USE_GLIBC), GROUP_TOTAL),min(length(platforms(USE_GLIBC)), (GROUP_INDEX+1)*glen(platforms(USE_GLIBC), GROUP_TOTAL))) | ||
| } | ||
|
|
||
| target "cross" { | ||
| inherits = ["binary", "_all_platforms"] | ||
| } | ||
|
|
||
| target "dynbinary-cross" { | ||
| inherits = ["dynbinary", "_all_platforms"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
2>/dev/nullwas intended to be ongit describehere, notsed, ala:Otherwise we get (the otherwise harmless)
fatal: not a git repository (or any of the parent directories): .gitoutput on non-Git builds.(I found this PR because I've got a job that's building CLI in a container, and it broke today with
make: docker: Command not foundso I got to go digging to figure out why, and found I now have to use./scripts/build/binarydirectly with the variables that are defined in that file but documented in theDockerfile-- I'm sure this is going to affect others who try to build the CLI in restricted environments too.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, and worse, the
||only takes effect if thesedfails, so I'd suggest something like this instead: