context

package module
v0.1.0-alpha004 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2025 License: Apache-2.0 Imports: 7 Imported by: 3

README

Docker Contexts

This package provides a simple API to discover Docker contexts.

Installation

go get github.com/docker/go-sdk/context

Usage

Current Context

It will return the current Docker context name.

current, err := context.Current()
if err != nil {
    log.Fatalf("failed to get current docker context: %v", err)
}

fmt.Printf("current docker context: %s", current)
Current Docker Host

It will return the Docker host that the current context is configured to use.

dockerHost, err := context.CurrentDockerHost()
if err != nil {
    log.Fatalf("failed to get current docker host: %v", err)
}
fmt.Printf("current docker host: %s", dockerHost)
Docker Host From Context

It will return the Docker host that the given context is configured to use.

dockerHost, err := context.DockerHostFromContext("desktop-linux")
if err != nil {
    log.Printf("error getting docker host from context: %s", err)
    return
}

fmt.Printf("docker host from context: %s", dockerHost)

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultContextName is the name reserved for the default context (config & env based)
	DefaultContextName = "default"

	// EnvOverrideContext is the name of the environment variable that can be
	// used to override the context to use. If set, it overrides the context
	// that's set in the CLI's configuration file, but takes no effect if the
	// "DOCKER_HOST" env-var is set (which takes precedence.
	EnvOverrideContext = "DOCKER_CONTEXT"

	// EnvOverrideHost is the name of the environment variable that can be used
	// to override the default host to connect to (DefaultDockerHost).
	//
	// This env-var is read by FromEnv and WithHostFromEnv and when set to a
	// non-empty value, takes precedence over the default host (which is platform
	// specific), or any host already set.
	EnvOverrideHost = "DOCKER_HOST"
)

Variables

View Source
var (
	// DefaultDockerHost is the default host to connect to the Docker socket.
	// The actual value is platform-specific and defined in host_linux.go and host_windows.go.
	DefaultDockerHost = ""

	// ErrDockerHostNotSet is the error returned when the Docker host is not set in the Docker context
	ErrDockerHostNotSet = internal.ErrDockerHostNotSet

	// ErrDockerContextNotFound is the error returned when the Docker context is not found.
	ErrDockerContextNotFound = internal.ErrDockerContextNotFound
)

Functions

func Current

func Current() (string, error)

Current returns the current context name, based on environment variables and the cli configuration file. It does not validate if the given context exists or if it's valid.

If the current context is not found, it returns the default context name.

Example
package main

import (
	"fmt"

	"github.com/docker/go-sdk/context"
)

func main() {
	ctx, err := context.Current()
	fmt.Println(err)
	fmt.Println(ctx != "")

}
Output:
<nil>
true

func CurrentDockerHost

func CurrentDockerHost() (string, error)

CurrentDockerHost returns the Docker host from the current Docker context. For that, it traverses the directory structure of the Docker configuration directory, looking for the current context and its Docker endpoint.

If the current context is the default context, it returns the value of the DOCKER_HOST environment variable.

Example
package main

import (
	"fmt"

	"github.com/docker/go-sdk/context"
)

func main() {
	host, err := context.CurrentDockerHost()
	fmt.Println(err)
	fmt.Println(host != "")

}
Output:
<nil>
true

func DockerHostFromContext

func DockerHostFromContext(ctx string) (string, error)

DockerHostFromContext returns the Docker host from the given context.

Example
package main

import (
	"fmt"
	"log"

	"github.com/docker/go-sdk/context"
)

func main() {
	host, err := context.DockerHostFromContext("desktop-linux")
	if err != nil {
		log.Printf("error getting docker host from context: %s", err)
		return
	}

	fmt.Println(host)

	// Intentionally not printing the output, as the context could not exist in the CI environment
}

func SetupTestDockerContexts

func SetupTestDockerContexts(tb testing.TB, currentContextIndex int, contextsCount int)

SetupTestDockerContexts creates a temporary directory structure for testing the Docker context functions. It creates the following structure, where $i is the index of the context, starting from 1: - $HOME/.docker

  • config.json
  • contexts
  • meta
  • context$i
  • meta.json

The config.json file contains the current context, and the meta.json files contain the metadata for each context. It generates the specified number of contexts, setting the current context to the one specified by currentContextIndex. The docker host for each context is "tcp://127.0.0.1:$i". Finally it always adds a context with an empty host, to validate the behavior when the host is not set. This empty context can be used setting the currentContextIndex to a number greater than contextsCount.

func Version

func Version() string

Version returns the version of the context package.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL