Skip to content

Instantly share code, notes, and snippets.

@tiesmaster
Last active December 2, 2023 12:22
Show Gist options
  • Save tiesmaster/820fa6ed9255472167b9cad2efc788a6 to your computer and use it in GitHub Desktop.
Save tiesmaster/820fa6ed9255472167b9cad2efc788a6 to your computer and use it in GitHub Desktop.
Reproduce Docker "default context" bug
module example/hello
go 1.21.4
require (
github.com/GoogleCloudPlatform/cloudsql-proxy v1.33.14
github.com/Xuanwo/go-locale v1.1.0
github.com/blang/semver v3.5.1+incompatible
github.com/docker/cli v24.0.7+incompatible
github.com/docker/go-connections v0.4.0
github.com/google/go-github/v56 v56.0.0
github.com/jmoiron/sqlx v1.3.5
github.com/juju/clock v1.0.3
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b
github.com/juju/mutex/v2 v2.0.0
github.com/moby/patternmatcher v0.6.0
github.com/opencontainers/runc v1.1.10
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
)
require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.0 // indirect
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fvbommel/sortorder v1.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/miekg/pkcs11 v1.0.2 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.1.0 // indirect
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect
github.com/prometheus/common v0.6.0 // indirect
github.com/prometheus/procfs v0.0.3 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/theupdateframework/notary v0.7.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
package main
import (
"errors"
"fmt"
"os"
dcommand "github.com/docker/cli/cli/command"
dconfig "github.com/docker/cli/cli/config"
configfile "github.com/docker/cli/cli/config/configfile"
ddocker "github.com/docker/cli/cli/context/docker"
dstore "github.com/docker/cli/cli/context/store"
dflags "github.com/docker/cli/cli/flags"
)
func main() {
cli, err := dcommand.NewDockerCli()
if err != nil {
fmt.Println(err)
}
version := cli.CurrentVersion()
fmt.Println(version)
context := cli.CurrentContext()
fmt.Println(context)
cli.Initialize(dflags.NewClientOptions())
store := cli.ContextStore()
m, err3 := store.GetMetadata("default")
if err3 != nil {
fmt.Println(err3)
}
fmt.Println(m)
}
func main2() {
var cf *configfile.ConfigFile
currentContext := os.Getenv("DOCKER_CONTEXT")
// currentContext := "desktop-linux"
if currentContext == "" {
dockerConfigDir := dconfig.Dir()
if _, err := os.Stat(dockerConfigDir); err != nil {
if !errors.Is(err, os.ErrNotExist) {
fmt.Println(err)
}
return
}
var err error
cf, err = dconfig.Load(dockerConfigDir)
if err != nil {
fmt.Printf("Unable to load the current Docker config from %q\n", dockerConfigDir)
return
}
currentContext = cf.CurrentContext
}
if currentContext == "" {
fmt.Println("currentContext in the docker config (from dconfig \"github.com/docker/cli/cli/config\") is \"\", normally minikube would exit the function, and not do the other logic")
fmt.Println("cf: ", cf)
fmt.Println("cf.CurrentContext: ", cf.CurrentContext)
}
storeConfig := dstore.NewConfig(
func() interface{} { return &ddocker.EndpointMeta{} },
dstore.EndpointTypeGetter(ddocker.DockerEndpoint, func() interface{} { return &ddocker.EndpointMeta{} }),
)
st := dstore.New(dconfig.ContextStoreDir(), storeConfig)
_, err := st.GetMetadata(currentContext)
if err != nil {
fmt.Printf("Unable to resolve the current Docker CLI context %q: %v\n", currentContext, err)
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment