# zidane/extensions/docker

Import: `import { … } from 'zidane/extensions/docker'`. Source: [src/extensions/docker.ts](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts).

## Functions

### `containerStatus`

Function.

The container's status line, read without changing anything ([`readContainer`](https://zidane.si/docs/reference/extensions-docker.md#readContainer), then [`containerStatusOf`](https://zidane.si/docs/reference/extensions-docker.md#containerStatusOf)).

```ts
function containerStatus(
  container: LocalContainer,
  deps?: ContainerDeps,
): Promise<ActionSettingStatus | undefined>
```

**Parameters**

- `container` (`LocalContainer`): The container.
- `deps?` (`ContainerDeps`, default `{}`): CLI and probe.

**Returns** `Promise<ActionSettingStatus | undefined>`

Source: [src/extensions/docker.ts:300](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L300)

### `containerStatusOf`

Function.

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.

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

**Parameters**

- `container` (`Pick<LocalContainer, 'healthPath'>`): The container.
- `state` (`ContainerState`): Its state ([`readContainer`](https://zidane.si/docs/reference/extensions-docker.md#readContainer)).
- `probe?` (`(url: string) => Promise<boolean>`, default `defaultProbe`): GETs a URL.

**Returns** `Promise<ActionSettingStatus | undefined>`

Source: [src/extensions/docker.ts:277](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L277)

### `defaultProbe`

Function.

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

```ts
function defaultProbe(url: string): Promise<boolean>
```

**Parameters**

- `url` (`string`): The URL to probe.

**Returns** `Promise<boolean>`

Source: [src/extensions/docker.ts:110](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L110)

### `dockerDetail`

Function.

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.

```ts
function dockerDetail(res: ExecResult): string
```

**Parameters**

- `res` (`ExecResult`): The failed call.

**Returns** `string`

Source: [src/extensions/docker.ts:92](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L92)

### `isPortConflict`

Function.

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

```ts
function isPortConflict(res: ExecResult): boolean
```

**Parameters**

- `res` (`ExecResult`): The failed call.

**Returns** `boolean`

Source: [src/extensions/docker.ts:81](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L81)

### `readContainer`

Function.

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

```ts
function readContainer(
  container: Pick<LocalContainer, 'name' | 'containerPort'>,
  exec?: Exec,
): Promise<ContainerState>
```

**Parameters**

- `container` (`Pick<LocalContainer, 'name' | 'containerPort'>`): The container.
- `exec?` (`Exec`, default `defaultExec`): Runs the CLI.

**Returns** `Promise<ContainerState>`

Source: [src/extensions/docker.ts:253](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L253)

### `requireDocker`

Function.

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

```ts
function requireDocker(exec?: Exec, notFound?: string): Promise<void>
```

**Parameters**

- `exec?` (`Exec`, default `defaultExec`): Runs the CLI.
- `notFound?` (`string`, default `DOCKER_NOT_FOUND`): The message when the CLI is missing.

**Returns** `Promise<void>`

Source: [src/extensions/docker.ts:130](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L130)

### `resolveHostPort`

Function.

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

```ts
function resolveHostPort(value: unknown, fallback: number): number
```

**Parameters**

- `value` (`unknown`): The setting's value (a number, or a numeric string).
- `fallback` (`number`): The port to use otherwise.

**Returns** `number`

Source: [src/extensions/docker.ts:71](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L71)

### `startContainer`

Function.

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.

```ts
function startContainer(
  container: LocalContainer,
  options: StartContainerOptions,
  deps?: ContainerDeps,
): Promise<StartedContainer>
```

**Parameters**

- `container` (`LocalContainer`): The container.
- `options` (`StartContainerOptions`): Port, extra run arguments and the missing-CLI message.
- `deps?` (`ContainerDeps`, default `{}`): CLI, probe and sleep.

**Returns** `Promise<StartedContainer>`

Source: [src/extensions/docker.ts:172](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L172)

### `stopContainer`

Function.

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

```ts
function stopContainer(
  name: string,
  deps?: Pick<ContainerDeps, 'exec'>,
): Promise<'absent' | 'already-stopped' | 'stopped'>
```

**Parameters**

- `name` (`string`): The container's name.
- `deps?` (`Pick<ContainerDeps, 'exec'>`, default `{}`): CLI.

**Returns** `Promise<'absent' | 'already-stopped' | 'stopped'>`

Source: [src/extensions/docker.ts:311](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L311)

## Constants

### `defaultExec`

Constant.

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

```ts
const defaultExec: Exec
```

Source: [src/extensions/docker.ts:99](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L99)

## Interfaces

### `ContainerDeps`

Interface.

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

```ts
interface ContainerDeps
```

**Properties**

- `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.

Source: [src/extensions/docker.ts:36](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L36)

### `ExecResult`

Interface.

What a CLI call answered.

```ts
interface ExecResult
```

**Properties**

- `code` (`number`): Exit code; `1` for a call that could not start.
- `error?` (`string`): Spawn-level failure (`ENOENT` when docker isn't installed).
- `stderr` (`string`): Its error output: where `docker` says why it failed.
- `stdout` (`string`): What the call printed.

Source: [src/extensions/docker.ts:21](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L21)

### `LocalContainer`

Interface.

An extension's container.

```ts
interface LocalContainer
```

**Properties**

- `command?` (`readonly string[]`): The command and its arguments after the image (`npx -y pxpipe-proxy`); absent: the image's own.
- `containerPort` (`number`): The server's port inside the container.
- `healthPath` (`string`): The path probed on the published URL to tell the server answers (`/stats`; `''`: the URL itself).
- `image` (`string`): The image it runs.
- `name` (`string`): The container's name (`zidane-headroom`).

Source: [src/extensions/docker.ts:46](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L46)

### `StartContainerOptions`

Interface.

Options of [`startContainer`](https://zidane.si/docs/reference/extensions-docker.md#startContainer).

```ts
interface StartContainerOptions
```

**Properties**

- `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.
- `port` (`number`): 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.

Source: [src/extensions/docker.ts:139](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L139)

### `StartedContainer`

Interface.

What [`startContainer`](https://zidane.si/docs/reference/extensions-docker.md#startContainer) did.

```ts
interface StartedContainer
```

**Properties**

- `baseUrl` (`string`): The server's URL on loopback, read back from docker.
- `created` (`boolean`): The container was created now (else an existing one was reused or started).
- `healthy` (`boolean`): The server answered its health path before the wait ran out.

Source: [src/extensions/docker.ts:153](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L153)

## Types

### `ContainerState`

Type.

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).

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

Source: [src/extensions/docker.ts:240](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L240)

### `Exec`

Type.

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

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

Source: [src/extensions/docker.ts:33](https://github.com/Tahul/zidane/blob/main/src/extensions/docker.ts#L33)
