mirror of
https://github.com/docker/cli.git
synced 2026-08-28 02:24:15 -05:00
commit 880ef756b7 fixed static builds with
CGO, which included setting the `netgo` build-tag for static builds.
Starting with go1.19, the Go runtime on Windows now supports the `netgo` build-
flag to use a native Go DNS resolver. Prior to that version, the build-flag
only had an effect on non-Windows platforms. From the go1.19 release notes:
https://go.dev/doc/go1.19#net
> Resolver.PreferGo is now implemented on Windows and Plan 9. It previously
> only worked on Unix platforms. Combined with Dialer.Resolver and Resolver.Dial,
> it's now possible to write portable programs and be in control of all DNS name
> lookups when dialing.
>
> The net package now has initial support for the netgo build tag on Windows.
> When used, the package uses the Go DNS client (as used by Resolver.PreferGo)
> instead of asking Windows for DNS results. The upstream DNS server it discovers
> from Windows may not yet be correct with complex system network configurations,
> however.
This originally caused issues in the daemon, because the pure-go implementation
did not respect file-based resolution (`C:\Windows\System32\Drivers\etc\hosts`),
resulting in `localhost` not being resolvable, and custom entries in `.etc/hosts`
not being used.
That specific problem was resolved in go1.22 (through [golang/go@33d4a51]), but
other limitations may still apply, and resolver ordering may not respect VPN
adaptors (such as Twingate) and queries sent through the local network adapter
instead of the VPN tunnel, resulting in DNS resolution failures;
Get "https://example.com:2376/v1.52/containers/json": dial tcp: lookup example.com: i/o timeout
This patch unsets the `netgo` option when (cross-)compiling for Windows, similar
to the patch used for the daemon (see [moby/moby@53d1b12]).
[golang/go@33d4a51]: https://github.com/golang/go/commit/33d4a5105cf2b2d549922e909e9239a48b8cefcc
[moby/moby@53d1b12]: https://github.com/moby/moby/commit/53d1b12bc014b4243e9439fc2610eb4ef863659f
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
126 lines
4.3 KiB
Bash
Executable File
126 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
set -eu
|
|
|
|
: "${CGO_ENABLED=}"
|
|
: "${GO_LINKMODE=static}"
|
|
: "${GO_BUILDMODE=}"
|
|
: "${GO_BUILDTAGS=}"
|
|
: "${GO_STRIP=}"
|
|
|
|
TARGET=${TARGET:-"build"}
|
|
|
|
PLATFORM=${PLATFORM:-}
|
|
VERSION=${VERSION:-$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags | sed 's/^v//' 2>/dev/null || echo "unknown-version" )}
|
|
GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)}
|
|
|
|
if [ "$(uname)" = "Darwin" ]; then
|
|
# Using BSD date (macOS), which doesn't suppoort the --date option
|
|
# date -jf "<input format>" "<input value>" +"<output format>" (https://unix.stackexchange.com/a/86510)
|
|
BUILDTIME=${BUILDTIME:-$(TZ=UTC date -jf "%s" "${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")}
|
|
else
|
|
# Using GNU date (Linux)
|
|
BUILDTIME=${BUILDTIME:-$(TZ=UTC date -u --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")}
|
|
fi
|
|
|
|
case "$VERSION" in
|
|
refs/tags/v*) VERSION=${VERSION#refs/tags/v} ;;
|
|
refs/tags/*) VERSION=${VERSION#refs/tags/} ;;
|
|
refs/heads/*) VERSION=$(echo "${VERSION#refs/heads/}" | sed -r 's#/+#-#g') ;;
|
|
refs/pull/*) VERSION=pr-$(echo "$VERSION" | grep -o '[0-9]\+') ;;
|
|
esac
|
|
|
|
GOOS="$(go env GOOS)"
|
|
GOARCH="$(go env GOARCH)"
|
|
if [ "${GOARCH}" = "arm" ]; then
|
|
GOARM="$(go env GOARM)"
|
|
fi
|
|
|
|
TARGET="$TARGET/docker-${GOOS}-${GOARCH}"
|
|
if [ "${GOARCH}" = "arm" ] && [ -n "${GOARM}" ]; then
|
|
TARGET="${TARGET}-v${GOARM}"
|
|
fi
|
|
if [ "${GOOS}" = "windows" ]; then
|
|
TARGET="${TARGET}.exe"
|
|
fi
|
|
export TARGET
|
|
|
|
# No CGO when cross building to darwin
|
|
if [ "$(go env GOOS)" = "darwin" ] && [ "$(uname)" != "Darwin" ]; then
|
|
if [ -z "$CGO_ENABLED" ]; then
|
|
CGO_ENABLED=0
|
|
elif [ "$CGO_ENABLED" = "1" ]; then
|
|
echo "CGO_ENABLED=1 not supported when cross-compiling for Darwin"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
if [ -z "$CGO_ENABLED" ]; then
|
|
case "$(go env GOOS)" in
|
|
linux)
|
|
case "$(go env GOARCH)" in
|
|
amd64|arm64|arm|s390x|riscv64|loong64)
|
|
CGO_ENABLED=1
|
|
;;
|
|
*)
|
|
CGO_ENABLED=0
|
|
;;
|
|
esac
|
|
;;
|
|
darwin|windows)
|
|
CGO_ENABLED=1
|
|
;;
|
|
*)
|
|
CGO_ENABLED=0
|
|
;;
|
|
esac
|
|
fi
|
|
export CGO_ENABLED
|
|
|
|
if [ "$CGO_ENABLED" = "1" ] && [ "$(go env GOOS)" != "windows" ]; then
|
|
case "$(go env GOARCH)" in
|
|
mips*|ppc64)
|
|
# pie build mode is not supported on mips architectures
|
|
;;
|
|
*)
|
|
GO_BUILDMODE="-buildmode=pie"
|
|
;;
|
|
esac
|
|
fi
|
|
export GO_BUILDMODE
|
|
|
|
GO_LDFLAGS="${GO_LDFLAGS:-}"
|
|
GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.GitCommit=${GITCOMMIT}\""
|
|
GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.BuildTime=${BUILDTIME}\""
|
|
GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.Version=${VERSION}\""
|
|
if test -n "${PLATFORM}"; then
|
|
GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.PlatformName=${PLATFORM}\""
|
|
fi
|
|
if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ] && [ "$(go env GOOS)" = "linux" ]; then
|
|
GO_LDFLAGS="$GO_LDFLAGS -linkmode external -extldflags -static"
|
|
fi
|
|
if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ]; then
|
|
# compiling statically with CGO enabled requires osusergo and netgo to be set.
|
|
GO_BUILDTAGS="$GO_BUILDTAGS osusergo netgo"
|
|
fi
|
|
# XXX: Disable netgo on Windows and use Windows system resolver instead.
|
|
#
|
|
# go1.19 and newer added support for netgo on Windows (https://go.dev/doc/go1.19#net),
|
|
# which may not respect VPN adaptors (such as Twingate) due to resolver ordering,
|
|
# resulting in queries being sent through the local network adapter instead of the
|
|
# VPN tunnel. See https://github.com/docker/cli/issues/6665
|
|
if [ "$(go env GOOS)" = "windows" ]; then
|
|
GO_BUILDTAGS=$(echo "$GO_BUILDTAGS" | sed 's/\(^\| \)netgo\( \|$\)/\1/g')
|
|
fi
|
|
if [ -n "$GO_STRIP" ]; then
|
|
# if stripping enabled and building with llvm < 12 against darwin/amd64
|
|
# platform, it will fail with:
|
|
# # github.com/docker/cli/cmd/docker
|
|
# /usr/local/go/pkg/tool/linux_amd64/link: /usr/local/go/pkg/tool/linux_amd64/link: running strip failed: exit status 1
|
|
# llvm-strip: error: unsupported load command (cmd=0x5)
|
|
# more info: https://github.com/docker/cli/pull/3717
|
|
GO_LDFLAGS="$GO_LDFLAGS -s -w"
|
|
fi
|
|
export GO_LDFLAGS="$GO_LDFLAGS" # https://github.com/koalaman/shellcheck/issues/2064
|
|
|
|
export SOURCE="github.com/docker/cli/cmd/docker"
|