Documentation
¶
Overview ¶
Package dockerhost models the Docker daemons a program talks to: the local socket, a remote daemon over SSH, or a remote daemon over TCP with TLS client certificates. Every Docker operation is built from a Docker handle resolved for one named host, so nothing ever silently assumes the local daemon.
It does not create `docker context` entries: those live in the service user's ~/.docker and are shared, mutable global state. It passes the equivalent connection flags (-H, --tlsverify, ...) on every call instead, which is exactly what a context expands to and keeps each host's settings in the calling program's own database and credential store.
Product names do not appear in messages, and DefaultCredentialName takes the product prefix as a parameter.
Index ¶
- Constants
- Variables
- func DefaultCredentialName(product, host string) string
- func Hint(err error, stderr []byte) string
- func PickComposeFiles(present []string) []string
- func TLSFiles(credential string) (ca, cert, key string)
- type ComposeContainer
- type ComposeProject
- type Credentials
- type Docker
- type File
- type Host
- type Kind
- type Resolver
- type TestResult
- type Volume
Constants ¶
const ( // ComposeSourceLabels marks a project found from container labels — // authoritative: its paths are exactly what Compose used. ComposeSourceLabels = "labels" // ComposeSourceScan marks a defined-but-not-running project found by // scanning a configured folder with Compose's default filenames. ComposeSourceScan = "scan" // ComposeSourceRegistered marks a project from a folder the user // registered explicitly (a stack that was never started by label). ComposeSourceRegistered = "registered" )
Sources of a discovered ComposeProject.
const DefaultTLSPort = "2376"
DefaultTLSPort is the Docker daemon's conventional TLS port.
const LocalName = "local"
LocalName is the name of the built-in local host. The caller should make it always exist; it is what a service uses when no host is chosen.
const TestTimeout = 45 * time.Second
TestTimeout bounds a connection test: long enough for a slow SSH handshake, short enough that a firewalled host fails promptly.
Variables ¶
var ComposeBaseFiles = []string{"compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"}
ComposeBaseFiles are the default compose filenames in Docker's documented lookup order: the first one present is the base file.
var ErrNoCredentials = errors.New("no credentials given")
ErrNoCredentials is returned by RenderCredentials when c is empty.
Functions ¶
func DefaultCredentialName ¶
DefaultCredentialName is the credential base name a host gets when its credentials are saved through the program: product names the program, e.g. "myapp" gives "myapp-dockerhost.<host>".
func Hint ¶
Hint returns a plain-English explanation for a failed docker command against a host, or "" when nothing specific is recognized.
func PickComposeFiles ¶
PickComposeFiles chooses, from the filenames present in one folder, the files `docker compose up` would use there: the first base file in lookup order, then its override files in merge order. It returns nil when the folder has no base file.
Types ¶
type ComposeContainer ¶
type ComposeContainer struct {
// Name is the container name
Name string `json:"name"`
// Service is the Compose service the container belongs to
Service string `json:"service"`
// Image is the image the container runs
Image string `json:"image"`
// Labels holds the container labels, as reported by Docker
Labels map[string]string `json:"labels,omitempty"`
}
ComposeContainer is one container belonging to a ComposeProject.
type ComposeProject ¶
type ComposeProject struct {
// Name is the project name (the `com.docker.compose.project` label).
Name string `json:"name"`
// WorkingDir is the project's working directory, as Compose itself
// recorded it — where docker-compose.yml/.env live.
WorkingDir string `json:"working_dir"`
// ConfigFiles are the absolute paths of the compose files the
// project was last started from.
ConfigFiles []string `json:"config_files,omitempty"`
// Services are the compose service names running in the project
// (e.g. "db", "web"), sorted and deduplicated.
Services []string `json:"services,omitempty"`
// Source says how the project was found: ComposeSourceLabels,
// ComposeSourceScan or ComposeSourceRegistered.
Source string `json:"source,omitempty"`
// Containers are the project's containers, for the compose
// strategy's labels snapshot. Deliberately excludes environment
// variables (docker inspect's Config.Env commonly carries secrets
// such as database passwords) — only name, image and labels, none
// of which callers should treat as secret.
Containers []ComposeContainer `json:"containers,omitempty"`
}
ComposeProject is one Docker Compose project discovered on a host, for a project picker or deployment tool: enough to find its config files and describe its containers without ever reading the host filesystem directly.
func ListComposeProjects ¶
ListComposeProjects lists every Docker Compose project on host d — found by grouping every container (running or stopped: a stopped project's config is still worth snapshotting) by its com.docker.compose.project label. A container with no such label belongs to no project and is skipped. Listing projects is a best-effort convenience like ListVolumes: the caller falls back to a plain text box when this fails.
func MergeComposeProjects ¶
func MergeComposeProjects(groups ...[]ComposeProject) []ComposeProject
MergeComposeProjects combines discovery results in priority order: label-found projects first (authoritative), then registered folders, then scanned ones. A later project is dropped when an earlier one already has the same name or the same working directory, so a running stack is never listed twice.
func ScanComposeDirs ¶
func ScanComposeDirs(ctx context.Context, r *runner.Runner, d Docker, helperImage string, dirs []string, scanChildren bool, source string) ([]ComposeProject, error)
ScanComposeDirs looks for defined-but-not-running compose projects under the given folders on host d. A folder counts if it holds a compose file itself (the "project" kind) or, with scanChildren, if any of its immediate sub-folders does. Files are read through a throwaway read-only helper container (no new SSH file access), with folder paths and filenames passed as argv, never into the shell text. Project names default to the folder name, as Compose does. source is stamped on every result (ComposeSourceScan or ComposeSourceRegistered).
type Credentials ¶
type Credentials struct {
// SSHPrivateKey is a remote-ssh host's private key (unencrypted).
SSHPrivateKey string `json:"ssh_private_key,omitempty"`
// CACert, ClientCert and ClientKey are a remote-tls host's PEM
// files (ca.pem, cert.pem, key.pem in Docker's documentation).
CACert string `json:"ca_cert,omitempty"`
// ClientCert is the PEM client certificate for TLS hosts
ClientCert string `json:"client_cert,omitempty"`
// ClientKey is the PEM private key for ClientCert
ClientKey string `json:"client_key,omitempty"`
}
Credentials is what a user pastes into a Docker host's credential form. It is write-only: it only ever reaches the credential files RenderCredentials produces, never the database or an API response.
func (Credentials) Empty ¶
func (c Credentials) Empty() bool
Empty reports whether no credential field was filled in.
type Docker ¶
type Docker struct {
// HostName names the host, for error messages ("" = local).
HostName string
// contains filtered or unexported fields
}
Docker is a ready-to-run docker CLI handle for one host: every command built from it carries that host's connection flags and environment. The zero value is the local daemon with the program's ambient Docker settings and "docker" from $PATH — what every caller got before Docker hosts existed.
func NewLocal ¶
NewLocal returns the local-host handle using bin ("" = "docker"). Tests use it to point commands at a stub.
type File ¶
type File struct {
// Name is the file name inside the credentials directory
Name string
// Data is the file contents
Data []byte
}
File is one credential file to write: a plain file name and its contents.
func RenderCredentials ¶
RenderCredentials validates c for a host of kind k whose credential base name is credential, and returns the files to write plus the secret values the caller must register for redaction first.
type Host ¶
type Host struct {
// Name is the short name the host is known by
Name string `json:"name"`
// Kind says how the host is reached
Kind Kind `json:"kind"`
// Address is where the daemon is. local: empty (Docker's default
// socket, honouring DOCKER_HOST in the program's environment) or a
// unix:// socket path; remote-ssh: ssh://user@host[:port];
// remote-tls: tcp://host:port. Normalize fills in the canonical
// form from shorter spellings (user@host, host, host:port).
Address string `json:"address"`
// Credential is the base name of the host's credential file(s).
// remote-ssh: the private key file (optional — without one, the
// service user's own SSH keys and agent are used).
// remote-tls: required; the files are Credential+".ca.pem",
// ".cert.pem" and ".key.pem" (see TLSFiles).
Credential string `json:"credential,omitempty"`
}
Host is one configured Docker host. It holds no secrets: an SSH key or TLS client key lives in root-only credential files, referenced by name (Credential) only.
type Kind ¶
type Kind string
Kind is how the program reaches a Docker daemon.
const ( // Local is the Docker daemon on the machine the program runs on, // through its Unix socket. Local Kind = "local" // RemoteSSH is a daemon on another machine, reached with // `ssh://user@host` (the docker CLI runs `docker system // dial-stdio` over SSH). Preferred for homelabs: no certificates. RemoteSSH Kind = "remote-ssh" // RemoteTLS is a daemon listening on TCP (normally port 2376) // that requires TLS client certificates. RemoteTLS Kind = "remote-tls" )
The supported host kinds.
type Resolver ¶
type Resolver struct {
// CredDirs are searched in order for each credential file.
CredDirs []string
// DockerBin overrides the docker binary (tests); "" = "docker".
DockerBin string
// SSHBin is the real ssh program remote-ssh hosts use; "" resolves
// "ssh" against $PATH when a remote-ssh host is resolved.
SSHBin string
// StateDir holds generated per-host ssh wrappers
// (<StateDir>/<host>/ssh). Required for remote-ssh hosts.
StateDir string
// KnownHostsFile is where remote-ssh hosts' SSH host keys are
// remembered (trust on first use) — the same file SFTP
// destinations use. Required for remote-ssh hosts.
KnownHostsFile string
// Redact receives the contents of each secret credential file, so
// nothing a host's key contains can reach a log or error.
Redact *secret.Set
}
Resolver turns configured Hosts into Docker handles, resolving credential files against the credential directories. The pipeline, the API and the CLI all resolve hosts through one Resolver so a host behaves the same everywhere.
type TestResult ¶
type TestResult struct {
// ServerVersion is the remote Docker Engine's version.
ServerVersion string `json:"server_version"`
}
TestResult is a successful connection test's findings.
type Volume ¶
type Volume struct {
// Name is the volume's name, what a container's mount source would
// reference.
Name string `json:"name"`
// Driver is the volume driver ("local" for almost every homelab
// setup).
Driver string `json:"driver"`
// ComposeProject is the Compose project the volume belongs to
// (from the com.docker.compose.project label), empty for a volume
// not created by Compose.
ComposeProject string `json:"compose_project,omitempty"`
}
Volume is one Docker volume, as much as the volume picker needs to know about it: enough to show it in a list and let an operator pick it by name.
func ListVolumes ¶
ListVolumes lists every Docker volume on host d, sorted by name, for a volume picker. An error here (commonly: Docker not installed, or the daemon not reachable) is the caller's to report — the picker always falls back to a plain text box, so listing volumes is a best-effort convenience, never a hard requirement.