config

package module
v0.1.0-alpha012 Latest Latest
Warning

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

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

README

Docker Config

This package provides a simple API to load docker CLI configs, auths, etc. with minimal deps.

This library is a fork of github.com/cpuguy83/dockercfg. Read the NOTICE file for more details.

Installation

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

Usage

Docker Config
Directory

It returns the current Docker config directory.

dir, err := config.Dir()
if err != nil {
    log.Fatalf("failed to get current docker config directory: %v", err)
}

fmt.Printf("current docker config directory: %s", dir)
Filepath

It returns the path to the Docker config file.

filepath, err := config.Filepath()
if err != nil {
    log.Fatalf("failed to get current docker config file path: %v", err)
}

fmt.Printf("current docker config file path: %s", filepath)
Load

It returns the Docker config.

cfg, err := config.Load()
if err != nil {
    log.Fatalf("failed to load docker config: %v", err)
}

fmt.Printf("docker config: %+v", cfg)
Save

Once you have loaded a config, you can save it back to the file system.

if err := cfg.Save(); err != nil {
    log.Fatalf("failed to save docker config: %v", err)
}
Auth
AuthConfigs

It returns a maps of the registry credentials for the given Docker images, indexed by the registry hostname.

authConfigs, err := config.AuthConfigs("nginx:latest")
if err != nil {
    log.Fatalf("failed to get registry credentials: %v", err)
}

fmt.Printf("registry credentials: %+v", authConfigs)
Auth Configs For Hostname

It returns the registry credentials for the given Docker registry.

authConfig, err := config.AuthConfigForHostname("https://index.docker.io/v1/")
if err != nil {
    log.Fatalf("failed to get registry credentials: %v", err)
}

fmt.Printf("registry credentials: %+v", authConfig)

Documentation

Index

Examples

Constants

View Source
const (
	// EnvOverrideDir is the name of the environment variable that can be
	// used to override the location of the client configuration files (~/.docker).
	//
	// It takes priority over the default.
	EnvOverrideDir = "DOCKER_CONFIG"

	// configFileName is the name of the client configuration file inside the
	// config-directory.
	FileName = "config.json"
)

Variables

View Source
var (
	ErrCredentialsNotFound         = errors.New("credentials not found in native keychain")
	ErrCredentialsMissingServerURL = errors.New("no credentials server URL")
)

Errors from credential helpers.

Functions

func AuthConfigForHostname

func AuthConfigForHostname(hostname string) (registry.AuthConfig, error)

AuthConfigForHostname gets registry credentials for the passed in registry host.

This will use Load to read registry auth details from the config. If the config doesn't exist, it will attempt to load registry credentials using the default credential helper for the platform.

Example
package main

import (
	"fmt"

	"github.com/docker/go-sdk/config"
	"github.com/docker/go-sdk/config/auth"
)

func main() {
	authConfig, err := config.AuthConfigForHostname(auth.IndexDockerIO)
	fmt.Println(err)
	fmt.Println(authConfig.Username != "")

}
Output:

<nil>
true

func AuthConfigs

func AuthConfigs(images ...string) (map[string]registry.AuthConfig, error)

AuthConfigs returns the auth configs for the given images. The images slice must contain images that are used in a Dockerfile. The returned map is keyed by the registry registry hostname for each image.

Example
package main

import (
	"fmt"

	"github.com/docker/go-sdk/config"
	"github.com/docker/go-sdk/config/auth"
)

func main() {
	authConfigs, err := config.AuthConfigs("nginx:latest")
	fmt.Println(err)
	fmt.Println(len(authConfigs))
	fmt.Println(authConfigs[auth.DockerRegistry].ServerAddress)

}
Output:

<nil>
1
docker.io

func Dir

func Dir() (string, error)

Dir returns the directory the configuration file is stored in, checking if the directory exists.

Example
package main

import (
	"fmt"
	"strings"

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

func main() {
	dir, err := config.Dir()
	fmt.Println(err)
	fmt.Println(strings.HasSuffix(dir, ".docker"))

}
Output:

<nil>
true

func EncodeBase64

func EncodeBase64(authConfig registry.AuthConfig) (string, error)

EncodeBase64 encodes an AuthConfig into base64.

func Filepath

func Filepath() (string, error)

Filepath returns the path to the docker cli config file, checking if the file exists.

Example
package main

import (
	"fmt"
	"log"
	"strings"

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

func main() {
	filepath, err := config.Filepath()
	if err != nil {
		log.Printf("error getting config filepath: %s", err)
		return
	}

	fmt.Println(strings.HasSuffix(filepath, "config.json"))

}
Output:

true

func Version

func Version() string

Version returns the version of the config package.

Types

type AuthConfig

type AuthConfig registry.AuthConfig

AuthConfig contains authorization information for connecting to a Registry. Deprecated: prefer use of registry.AuthConfig

type Config

type Config struct {
	AuthConfigs          map[string]registry.AuthConfig `json:"auths"`
	HTTPHeaders          map[string]string              `json:"HttpHeaders,omitempty"`
	PsFormat             string                         `json:"psFormat,omitempty"`
	ImagesFormat         string                         `json:"imagesFormat,omitempty"`
	NetworksFormat       string                         `json:"networksFormat,omitempty"`
	PluginsFormat        string                         `json:"pluginsFormat,omitempty"`
	VolumesFormat        string                         `json:"volumesFormat,omitempty"`
	StatsFormat          string                         `json:"statsFormat,omitempty"`
	DetachKeys           string                         `json:"detachKeys,omitempty"`
	CredentialsStore     string                         `json:"credsStore,omitempty"`
	CredentialHelpers    map[string]string              `json:"credHelpers,omitempty"`
	Filename             string                         `json:"-"` // Note: for internal use only.
	ServiceInspectFormat string                         `json:"serviceInspectFormat,omitempty"`
	ServicesFormat       string                         `json:"servicesFormat,omitempty"`
	TasksFormat          string                         `json:"tasksFormat,omitempty"`
	SecretFormat         string                         `json:"secretFormat,omitempty"`
	ConfigFormat         string                         `json:"configFormat,omitempty"`
	NodesFormat          string                         `json:"nodesFormat,omitempty"`
	PruneFilters         []string                       `json:"pruneFilters,omitempty"`
	Proxies              map[string]ProxyConfig         `json:"proxies,omitempty"`
	Experimental         string                         `json:"experimental,omitempty"`
	StackOrchestrator    string                         `json:"stackOrchestrator,omitempty"`
	Kubernetes           *KubernetesConfig              `json:"kubernetes,omitempty"`
	CurrentContext       string                         `json:"currentContext,omitempty"`
	CLIPluginsExtraDirs  []string                       `json:"cliPluginsExtraDirs,omitempty"`
	Aliases              map[string]string              `json:"aliases,omitempty"`
	// contains filtered or unexported fields
}

Config represents the on disk format of the docker CLI's config file.

func Load

func Load() (Config, error)

Load returns the docker config file. It will internally check, in this particular order: 1. the DOCKER_AUTH_CONFIG environment variable, unmarshalling it into a Config 2. the DOCKER_CONFIG environment variable, as the path to the config file 3. else it will load the default config file, which is ~/.docker/config.json

Example
package main

import (
	"fmt"

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

func main() {
	cfg, err := config.Load()
	fmt.Println(err)
	fmt.Println(len(cfg.AuthConfigs) > 0)

}
Output:

<nil>
true

func (*Config) AuthConfigForHostname

func (c *Config) AuthConfigForHostname(hostname string) (registry.AuthConfig, error)

AuthConfigForHostname returns the auth config for the given hostname with caching

func (*Config) AuthConfigForImage

func (c *Config) AuthConfigForImage(image string) (string, registry.AuthConfig, error)

AuthConfigForImage returns the auth config for a single image

func (*Config) AuthConfigsForImages

func (c *Config) AuthConfigsForImages(images []string) (map[string]registry.AuthConfig, error)

AuthConfigsForImages returns auth configs for multiple images with caching

func (*Config) ParseProxyConfig

func (c *Config) ParseProxyConfig(host string, runOpts map[string]*string) map[string]*string

ParseProxyConfig computes proxy configuration by retrieving the config for the provided host and then checking this against any environment variables provided to the container

func (*Config) Save

func (c *Config) Save() error

Save saves the config to the file system

type KubernetesConfig

type KubernetesConfig struct {
	AllNamespaces string `json:"allNamespaces,omitempty"`
}

KubernetesConfig contains Kubernetes orchestrator settings.

type ProxyConfig

type ProxyConfig struct {
	HTTPProxy  string `json:"httpProxy,omitempty"`
	HTTPSProxy string `json:"httpsProxy,omitempty"`
	NoProxy    string `json:"noProxy,omitempty"`
	FTPProxy   string `json:"ftpProxy,omitempty"`
	AllProxy   string `json:"allProxy,omitempty"`
}

ProxyConfig contains proxy configuration settings.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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