Skip to content

zidane/extensions/docker

import { … } from 'zidane/extensions/docker'src/extensions/docker.ts

Functions#

containerStatusFunction#

The container's status line, read without changing anything (readContainer, then containerStatusOf).

TypeScript
function containerStatus(
  container: LocalContainer,
  deps?: ContainerDeps,
): Promise<ActionSettingStatus | undefined>
Parameters
NameTypeDefaultDescription
containerLocalContainer

The container.

deps?ContainerDeps{}

CLI and probe.

Promise<ActionSettingStatus | undefined>

src/extensions/docker.ts:300

containerStatusOfFunction#

A "Run container" row's status line for a container's state, probing the server when it runs. undefined (no line) while docker can't be asked: a user without docker sees no noise on a row they never used.

TypeScript
function containerStatusOf(
  container: Pick<LocalContainer, 'healthPath'>,
  state: ContainerState,
  probe?: (url: string) => Promise<boolean>,
): Promise<ActionSettingStatus | undefined>
Parameters
NameTypeDefaultDescription
containerPick<LocalContainer, 'healthPath'>

The container.

stateContainerState

Its state (readContainer).

probe?(url: string) => Promise<boolean>defaultProbe

GETs a URL.

Promise<ActionSettingStatus | undefined>

src/extensions/docker.ts:277

defaultProbeFunction#

Whether a GET of url answers 2xx within 2 s.

TypeScript
function defaultProbe(url: string): Promise<boolean>
Parameters
NameTypeDescription
urlstring

The URL to probe.

Promise<boolean>

src/extensions/docker.ts:110

dockerDetailFunction#

The last non-empty line of stderr (else stdout), clipped. Docker errors are verbose, and docker run starts with a pull banner ("Unable to find image … locally"): the last line is the cause.

TypeScript
function dockerDetail(res: ExecResult): string
Parameters
NameTypeDescription
resExecResult

The failed call.

string

src/extensions/docker.ts:92

isPortConflictFunction#

Whether a docker run / docker start failure is a host port another process holds.

TypeScript
function isPortConflict(res: ExecResult): boolean
Parameters
NameTypeDescription
resExecResult

The failed call.

boolean

src/extensions/docker.ts:81

readContainerFunction#

Reads the container's state in one docker inspect, with a short timeout so a hung daemon fits a status line's budget.

TypeScript
function readContainer(
  container: Pick<LocalContainer, 'name' | 'containerPort'>,
  exec?: Exec,
): Promise<ContainerState>
Parameters
NameTypeDefaultDescription
containerPick<LocalContainer, 'name' | 'containerPort'>

The container.

exec?ExecdefaultExec

Runs the CLI.

src/extensions/docker.ts:253

requireDockerFunction#

Throws, with a message for the user, unless the docker CLI is installed and its daemon answers.

TypeScript
function requireDocker(exec?: Exec, notFound?: string): Promise<void>
Parameters
NameTypeDefaultDescription
exec?ExecdefaultExec

Runs the CLI.

notFound?stringDOCKER_NOT_FOUND

The message when the CLI is missing.

Promise<void>

src/extensions/docker.ts:130

resolveHostPortFunction#

A config value as a host port: an integer from 1 to 65535, else fallback.

TypeScript
function resolveHostPort(value: unknown, fallback: number): number
Parameters
NameTypeDescription
valueunknown

The setting's value (a number, or a numeric string).

fallbacknumber

The port to use otherwise.

number

src/extensions/docker.ts:71

startContainerFunction#

Starts the container, or reuses it, and waits until its server answers. Not answering in time is not a failure: a first start may download for minutes, so healthy says so and the URL is returned anyway. Throws an error whose message is for the user on every failure.

TypeScript
function startContainer(
  container: LocalContainer,
  options: StartContainerOptions,
  deps?: ContainerDeps,
): Promise<StartedContainer>
Parameters
NameTypeDefaultDescription
containerLocalContainer

The container.

optionsStartContainerOptions

Port, extra run arguments and the missing-CLI message.

deps?ContainerDeps{}

CLI, probe and sleep.

src/extensions/docker.ts:172

stopContainerFunction#

Stops the container when it runs. absent and already-stopped are not failures; only a failed docker stop (or no docker) throws.

TypeScript
function stopContainer(
  name: string,
  deps?: Pick<ContainerDeps, 'exec'>,
): Promise<'absent' | 'already-stopped' | 'stopped'>
Parameters
NameTypeDefaultDescription
namestring

The container's name.

deps?Pick<ContainerDeps, 'exec'>{}

CLI.

Promise<'absent' | 'already-stopped' | 'stopped'>

src/extensions/docker.ts:311

Constants#

defaultExecConstant#

The CLI, bounded by timeoutMs. Never rejects: a call that cannot start resolves with its error code (ENOENT).

TypeScript
const defaultExec: Exec

src/extensions/docker.ts:99

Interfaces#

ContainerDepsInterface#

How a container is driven: the CLI, the health probe and the wait between probes. Each defaults to the real one.

TypeScript
interface ContainerDeps
ContainerDeps properties
NameTypeDescription
exec?Exec

Runs a docker CLI call.

probe?(url: string) => Promise<boolean>

GET url and report whether the server answered.

sleep?(ms: number) => Promise<void>

Waits between two health probes; a test passes one that resolves at once.

src/extensions/docker.ts:36

ExecResultInterface#

What a CLI call answered.

TypeScript
interface ExecResult
ExecResult properties
NameTypeDescription
codenumber

Exit code; 1 for a call that could not start.

error?string

Spawn-level failure (ENOENT when docker isn't installed).

stderrstring

Its error output: where docker says why it failed.

stdoutstring

What the call printed.

src/extensions/docker.ts:21

LocalContainerInterface#

An extension's container.

TypeScript
interface LocalContainer
LocalContainer properties
NameTypeDescription
command?readonly string[]

The command and its arguments after the image (npx -y pxpipe-proxy); absent: the image's own.

containerPortnumber

The server's port inside the container.

healthPathstring

The path probed on the published URL to tell the server answers (/stats; '': the URL itself).

imagestring

The image it runs.

namestring

The container's name (zidane-headroom).

src/extensions/docker.ts:46

StartContainerOptionsInterface#

Options of startContainer.

TypeScript
interface StartContainerOptions
StartContainerOptions properties
NameTypeDescription
healthWaitMs?number

How long to probe the server before reporting it is still starting, in ms. Default 15 s.

notFound?string

The message when the docker CLI is missing.

portnumber

The loopback port to publish the server on.

portSetting?string

The label of the setting that picks port, named in the "port in use" error. Default Host port.

runArgs?() => readonly string[] | Promise<readonly string[]>

Extra docker run arguments before the image (env forwards, volumes, GPU flags), asked only when the container is created.

src/extensions/docker.ts:139

StartedContainerInterface#

What startContainer did.

TypeScript
interface StartedContainer
StartedContainer properties
NameTypeDescription
baseUrlstring

The server's URL on loopback, read back from docker.

createdboolean

The container was created now (else an existing one was reused or started).

healthyboolean

The server answered its health path before the wait ran out.

src/extensions/docker.ts:153

Types#

ContainerStateType#

What docker says about a container: unknown when it can't be asked (the CLI is missing, the daemon is down), else absent, stopped, or running with its published host port (absent when it publishes none).

TypeScript
type ContainerState =
  | { state: 'unknown' }
  | { state: 'absent' }
  | { state: 'stopped' }
  | { hostPort?: number; state: 'running' }

src/extensions/docker.ts:240

ExecType#

Runs argv (the program first), giving up after timeoutMs. Never rejects.

TypeScript
type Exec = (argv: readonly string[], timeoutMs: number) => Promise<ExecResult>

src/extensions/docker.ts:33