MCPcopy Create free account
hub / github.com/ddev/ddev / getDockerManagerInstance

Function getDockerManagerInstance

pkg/dockerutil/docker_manager.go:49–103  ·  view source on GitHub ↗

getDockerManagerInstance returns the singleton instance, initializing it if needed

()

Source from the content-addressed store, hash-verified

47
48// getDockerManagerInstance returns the singleton instance, initializing it if needed
49func getDockerManagerInstance() (*dockerManager, error) {
50 sDockerManagerOnce.Do(func() {
51 sDockerManager = &dockerManager{}
52 // Suppress any output (stdout, stderr) from docker/cli
53 sDockerManager.cli, sDockerManagerErr = command.NewDockerCli(
54 command.WithCombinedStreams(io.Discard),
55 )
56 if sDockerManagerErr != nil {
57 return
58 }
59 // InstallFlags and SetDefaultOptions are necessary to match
60 // the plugin mode behavior to handle env vars such as
61 // DOCKER_TLS and DOCKER_TLS_VERIFY.
62 // See https://github.com/docker/cli/blob/master/cmd/docker-trust/trust/commands.go
63 flagSet := pflag.NewFlagSet("ddev", pflag.ContinueOnError)
64 opts := flags.NewClientOptions()
65 opts.InstallFlags(flagSet)
66 opts.SetDefaultOptions(flagSet)
67 sDockerManagerErr = sDockerManager.cli.Initialize(opts)
68 if sDockerManagerErr != nil {
69 return
70 }
71 sDockerManager.dockerContextName = sDockerManager.cli.CurrentContext()
72 sDockerManager.host = sDockerManager.cli.DockerEndpoint().Host
73 util.Verbose("getDockerManagerInstance(): dockerContextName=%s, host=%s", sDockerManager.dockerContextName, sDockerManager.host)
74 sDockerManager.hostIP, sDockerManager.hostIPErr = getDockerIPFromDockerHost(sDockerManager.host)
75 sDockerManager.goContext = context.Background()
76 // Set the Docker CLI version for User-Agent header
77 version.Version = "ddev-" + versionconstants.DdevVersion
78 // We can't use sDockerManager.cli.Client(), see https://github.com/docker/cli/issues/4489
79 // That's why we create a new client from flags to catch errors
80 sDockerManager.apiClient, sDockerManagerErr = command.NewAPIClientFromFlags(
81 opts,
82 sDockerManager.cli.ConfigFile(),
83 )
84 if sDockerManagerErr != nil {
85 return
86 }
87 sDockerManager.serverVersion, sDockerManagerErr = sDockerManager.apiClient.ServerVersion(sDockerManager.goContext, client.ServerVersionOptions{})
88 if sDockerManagerErr != nil {
89 return
90 }
91 info, infoErr := sDockerManager.apiClient.Info(sDockerManager.goContext, client.InfoOptions{})
92 if infoErr != nil {
93 sDockerManagerErr = infoErr
94 return
95 }
96 sDockerManager.info = info.Info
97 // A minimal cobra.Command is sufficient since we only need plugin
98 // metadata, not builtin-command conflict detection.
99 rootCmd := &cobra.Command{Use: "ddev"}
100 sDockerManager.cliPlugins, sDockerManager.cliPluginsErr = manager.ListPlugins(sDockerManager.cli, rootCmd)
101 })
102 return sDockerManager, sDockerManagerErr
103}
104
105// GetDockerClient returns the Go context and the Docker API client
106func GetDockerClient() (context.Context, client.APIClient, error) {

Callers 9

GetDockerClientFunction · 0.85
GetDockerClientInfoFunction · 0.85
GetDockerIPFunction · 0.85
ResetDockerHostFunction · 0.85
GetServerVersionFunction · 0.85
GetDockerVersionFunction · 0.85
GetDockerAPIVersionFunction · 0.85
GetCLIPluginsFunction · 0.85

Calls 2

VerboseFunction · 0.92

Tested by

no test coverage detected