Skip to content
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

Fix various issues with the "make buildx" target #40089

Merged
merged 8 commits into from Oct 22, 2019
21 changes: 17 additions & 4 deletions Dockerfile.buildx
@@ -1,14 +1,27 @@
ARG GO_VERSION=1.12.10
FROM golang:${GO_VERSION}-stretch
ARG BUILDX_COMMIT=v0.3.1
ARG BUILDX_REPO=https://github.com/docker/buildx.git

FROM golang:${GO_VERSION}-stretch AS build
ARG BUILDX_REPO
RUN git clone "${BUILDX_REPO}" /buildx
WORKDIR /buildx
ARG BUILDX_COMMIT=master
ARG BUILDX_COMMIT
kolyshkin marked this conversation as resolved.
Show resolved Hide resolved
RUN git fetch origin "${BUILDX_COMMIT}":build && git checkout build
RUN go mod download
ARG GOOS
ARG GOARCH
# Keep these essentially no-op var settings for debug purposes.
# It allows us to see what the GOOS/GOARCH that's being built for is.
RUN GOOS=${GOOS} GOARCH=${GOARCH} go build -ldflags '-X github.com/docker/buildx/version.Version=${BUILDX_COMMIT} -X github.com/docker/buildx/version.Revision=${BUILDX_COMMIT} -X github.com/docker/buildx/version.Package=github.com/docker/buildx' -o /usr/bin/buildx ./cmd/buildx
ENTRYPOINT ["/usr/bin/buildx"]
RUN GOOS="${GOOS}" GOARCH="${GOARCH}" BUILDX_COMMIT="${BUILDX_COMMIT}"; \
pkg="github.com/docker/buildx"; \
ldflags="\
-X \"${pkg}/version.Version=$(git describe --tags)\" \
-X \"${pkg}/version.Revision=$(git rev-parse --short HEAD)\" \
-X \"${pkg}/version.Package=buildx\" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm. shouldn't it be -X \"${pkg}/version.Package=${pkg}\" \?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #40089 (comment)

This way, it looks like

buildx version
buildx v0.3.0 c967f1d

instead of

buildx version
github.com/docker/buildx v0.3.0 c967f1d

"; \
go build -ldflags "${ldflags}" -o /usr/bin/buildx ./cmd/buildx

FROM golang:${GO_VERSION}-stretch
COPY --from=build /usr/bin/buildx /usr/bin/buildx
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! 💡 I know why it failed in this branch; before this, there was no COPY in the Dockerfile, and because the COPY was added, it started downloading the docker.io/tonistiigi/copy:v0.1.3 image. Apparently, that one is only available for amd64 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup; that one is only amd64, and armv7

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning for doing this second image?
This only produces a runnable image if the specified GOOS/GOARCH == the daemon's GOOS/GOARCH

In effect this is creating a 2nd image and taking even more space since buildx is duplicated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first stage won't be tagged, so can be garbage collected, and only the second one is preserved; the final image is roughly 870MB whereas without that stage, we're tagging an image that's 1.71GB; see the commit message 39f1a4a

ENTRYPOINT ["/usr/bin/buildx"]
32 changes: 24 additions & 8 deletions Jenkinsfile
Expand Up @@ -375,8 +375,12 @@ pipeline {
expression { params.s390x }
}
agent { label 's390x-ubuntu-1604' }
// s390x machines run on Docker 18.06, and buildkit has some bugs on that version
environment { DOCKER_BUILDKIT = '0' }
// s390x machines run on Docker 18.06, and buildkit has some
// bugs on that version. Build and use buildx instead.
environment {
USE_BUILDX = '1'
DOCKER_BUILDKIT = '0'
}

stages {
stage("Print info") {
Expand Down Expand Up @@ -484,8 +488,12 @@ pipeline {
expression { params.s390x }
}
agent { label 's390x-ubuntu-1604' }
// s390x machines run on Docker 18.06, and buildkit has some bugs on that version
environment { DOCKER_BUILDKIT = '0' }
// s390x machines run on Docker 18.06, and buildkit has some
// bugs on that version. Build and use buildx instead.
environment {
USE_BUILDX = '1'
DOCKER_BUILDKIT = '0'
}

stages {
stage("Print info") {
Expand Down Expand Up @@ -569,8 +577,12 @@ pipeline {
expression { params.ppc64le }
}
agent { label 'ppc64le-ubuntu-1604' }
// ppc64le machines run on Docker 18.06, and buildkit has some bugs on that version
environment { DOCKER_BUILDKIT = '0' }
// ppc64le machines run on Docker 18.06, and buildkit has some
// bugs on that version. Build and use buildx instead.
environment {
USE_BUILDX = '1'
DOCKER_BUILDKIT = '0'
}

stages {
stage("Print info") {
Expand Down Expand Up @@ -678,8 +690,12 @@ pipeline {
expression { params.ppc64le }
}
agent { label 'ppc64le-ubuntu-1604' }
// ppc64le machines run on Docker 18.06, and buildkit has some bugs on that version
environment { DOCKER_BUILDKIT = '0' }
// ppc64le machines run on Docker 18.06, and buildkit has some
// bugs on that version. Build and use buildx instead.
environment {
USE_BUILDX = '1'
DOCKER_BUILDKIT = '0'
}

stages {
stage("Print info") {
Expand Down
26 changes: 15 additions & 11 deletions Makefile
Expand Up @@ -247,21 +247,25 @@ swagger-docs: ## preview the API documentation

.PHONY: buildx
ifeq ($(BUILDX), bundles/buildx)
buildx: bundles/buildx # build buildx cli tool
else
buildx:
buildx: bundles/buildx ## build buildx cli tool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #40091 buildx is a required pre-req for all builds.
What happens if we don't define buildx:?

Copy link
Member Author

@thaJeztah thaJeztah Oct 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be covered by the .PHONY buildx

mkdir foo && cd foo

echo ".PHONY: buildx" > Makefile

make buildx
# make: Nothing to be done for `buildx'.

echo $?
# 0

make nonexistingtarget
# make: *** No rule to make target `nonexistingtarget'.  Stop.

echo $?
# 2

endif

bundles/buildx: BUILDX_DOCKERFILE ?= Dockerfile.buildx
bundles/buildx: BUILDX_COMMIT ?= v0.3.0
# This intentionally is not using the `--output` flag from the docker CLI, which
# is a buildkit option. The idea here being that if buildx is being used, it's
# because buildkit is not supported natively
bundles/buildx: bundles ## build buildx CLI tool
# This intetionally is not using the `--output` flag from the docker CLI which is a buildkit option
# The idea here being that if buildx is being used, it's because buildkit is not supported natively
docker build -f $(BUILDX_DOCKERFILE) -t "moby-buildx:$(BUILDX_COMMIT)" \
docker build -f $${BUILDX_DOCKERFILE:-Dockerfile.buildx} -t "moby-buildx:$${BUILDX_COMMIT:-latest}" \
--build-arg BUILDX_COMMIT \
kolyshkin marked this conversation as resolved.
Show resolved Hide resolved
--build-arg BUILDX_REPO \
--build-arg GOOS=$$(if [ -n "$(GOOS)" ]; then echo $(GOOS); else go env GOHOSTOS || uname | awk '{print tolower($$0)}' || true; fi) \
--build-arg GOARCH=$$(if [ -n "$(GOARCH)" ]; then echo $(GOARCH); else go env GOHOSTARCH || true; fi) \
thaJeztah marked this conversation as resolved.
Show resolved Hide resolved
. && \
id=$$(docker create moby-buildx:$(BUILDX_COMMIT)); \
if [ -n "$${id}" ]; then docker cp $${id}:/usr/bin/buildx $@ && touch $@; docker rm -f $${id}; fi
.

id=$$(docker create moby-buildx:$${BUILDX_COMMIT:-latest}); \
if [ -n "$${id}" ]; then \
docker cp $${id}:/usr/bin/buildx $@ \
&& touch $@; \
docker rm -f $${id}; \
fi

$@ version