Dockerfile: use our own Go toolchain, not the golang Docker image

The golang:N-alpine Docker image lags behind Go minor releases by a
day or two. When we bump go.mod to a new minor version before the
image catches up, the required "Build Docker image" CI check fails
with "go.mod requires go >= 1.27.1 (running go 1.27.0;
GOTOOLCHAIN=local)" and blocks the toolchain bump from merging.

Instead, base the build stage on plain alpine and download the
Tailscale Go toolchain release for the revision in go.toolchain.rev,
matching how everything else in this repo is built.

Fixes #21072

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I1ccb77fa3c7a49532ea87dbdbf9e3340880ec94e
This commit is contained in:
Brad Fitzpatrick authored and Brad Fitzpatrick committed 2026-09-01 15:47:33 -07:00
1 parent 0188b38666
commit c3736ae41a
4 files changed
+31 -28

No files matched your search

+25 -1
View File
@@ -36,10 +36,34 @@
# $ docker exec tailscaled tailscale status
FROM golang:1.27-alpine AS build-env
# Use Tailscale's Go toolchain (a fork of Go) as specified by the
# go.toolchain.rev file, matching how everything else in this repo is
# built, rather than the golang Docker image. This avoids this
# Dockerfile breaking for a day or two after each Go minor version
# bump, when go.mod requires a Go version that the official golang
# Docker image doesn't yet ship. See
# https://github.com/tailscale/tailscale/issues/21072
FROM alpine:3.22 AS build-env
WORKDIR /go/src/tailscale
RUN apk add --no-cache curl tar
COPY go.toolchain.rev ./
RUN read -r REV <go.toolchain.rev && \
case "$(apk --print-arch)" in \
x86_64) ARCH=amd64 ;; \
aarch64) ARCH=arm64 ;; \
*) echo "unsupported architecture" >&2; exit 1 ;; \
esac && \
curl --retry 3 -f -L -o /tmp/go.tar.gz "https://github.com/tailscale/go/releases/download/build-${REV}/linux-${ARCH}.tar.gz" && \
mkdir -p /usr/local/go && \
tar -C /usr/local/go --strip-components=1 -xzf /tmp/go.tar.gz && \
rm /tmp/go.tar.gz
ENV GOPATH=/go
ENV PATH=/usr/local/go/bin:$GOPATH/bin:$PATH
COPY go.mod go.sum ./
RUN go mod download
-1
View File
@@ -26,7 +26,6 @@ and past bump commits (e.g. "go.toolchain.branch: switch to Go 1.26",
## Version strings elsewhere
- [ ] `Dockerfile`: `FROM golang:1.NN-alpine` in the build stage.
- [ ] `README.md`: "We always require the latest Go release, currently Go 1.NN".
- [ ] `.github/workflows/`: grep for hardcoded Go versions. As of Go
1.27 the workflows use `go-version-file: go.mod` and need no
+3 -1
View File
@@ -38,7 +38,9 @@ func readAlpineTag(t *testing.T, file string) string {
if !ok {
continue
}
return string(suf)
// Drop anything after the tag, such as an "AS stage-name" suffix.
tag, _, _ := bytes.Cut(suf, []byte(" "))
return string(tag)
}
return ""
}
+3 -25
View File
@@ -4,7 +4,6 @@
package tailscaleroot
import (
"fmt"
"os"
"os/exec"
"runtime"
@@ -14,19 +13,6 @@ import (
"golang.org/x/mod/modfile"
)
func TestDockerfileVersion(t *testing.T) {
goVersion := mustGetGoModVersion(t, false)
dockerFile, err := os.ReadFile("Dockerfile")
if err != nil {
t.Fatal(err)
}
wantSub := fmt.Sprintf("FROM golang:%s-alpine AS build-env", goVersion)
if !strings.Contains(string(dockerFile), wantSub) {
t.Errorf("didn't find %q in Dockerfile", wantSub)
}
}
// TestGoVersion tests that the Go version specified in go.mod matches ./tool/go version.
func TestGoVersion(t *testing.T) {
// We could special-case ./tool/go path for Windows, but really there is no
@@ -34,7 +20,7 @@ func TestGoVersion(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping test on Windows")
}
goModVersion := mustGetGoModVersion(t, true)
goModVersion := mustGetGoModVersion(t)
goToolCmd := exec.Command("./tool/go", "version")
goToolOutput, err := goToolCmd.Output()
@@ -56,7 +42,7 @@ func TestGoVersion(t *testing.T) {
}
}
func mustGetGoModVersion(t *testing.T, includePatchVersion bool) string {
func mustGetGoModVersion(t *testing.T) string {
t.Helper()
goModBytes, err := os.ReadFile("go.mod")
@@ -73,13 +59,5 @@ func mustGetGoModVersion(t *testing.T, includePatchVersion bool) string {
t.Fatal("no Go version found in go.mod")
}
version := modFile.Go.Version
parts := strings.Split(version, ".")
if !includePatchVersion {
if len(parts) >= 2 {
version = parts[0] + "." + parts[1]
}
}
return version
return modFile.Go.Version
}