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).
function containerStatus(
container: LocalContainer,
deps?: ContainerDeps,
): Promise<ActionSettingStatus | undefined>Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| container | LocalContainer | The container. | |
| deps? | ContainerDeps | {} | CLI and probe. |
Returns
Promise<ActionSettingStatus | undefined>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.
function containerStatusOf(
container: Pick<LocalContainer, 'healthPath'>,
state: ContainerState,
probe?: (url: string) => Promise<boolean>,
): Promise<ActionSettingStatus | undefined>Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| container | Pick<LocalContainer, 'healthPath'> | The container. | |
| state | ContainerState | Its state ( | |
| probe? | (url: string) => Promise<boolean> | defaultProbe | GETs a URL. |
Returns
Promise<ActionSettingStatus | undefined>defaultProbeFunction#
Whether a GET of url answers 2xx within 2 s.
function defaultProbe(url: string): Promise<boolean>Parameters
| Name | Type | Description |
|---|---|---|
| url | string | The URL to probe. |
Returns
Promise<boolean>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.
function dockerDetail(res: ExecResult): stringParameters
| Name | Type | Description |
|---|---|---|
| res | ExecResult | The failed call. |
Returns
stringisPortConflictFunction#
Whether a docker run / docker start failure is a host port another process holds.
function isPortConflict(res: ExecResult): booleanParameters
| Name | Type | Description |
|---|---|---|
| res | ExecResult | The failed call. |
Returns
booleanreadContainerFunction#
Reads the container's state in one docker inspect, with a short timeout
so a hung daemon fits a status line's budget.
function readContainer(
container: Pick<LocalContainer, 'name' | 'containerPort'>,
exec?: Exec,
): Promise<ContainerState>Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| container | Pick<LocalContainer, 'name' | 'containerPort'> | The container. | |
| exec? | Exec | defaultExec | Runs the CLI. |
Returns
Promise<ContainerState>requireDockerFunction#
Throws, with a message for the user, unless the docker CLI is installed and its daemon answers.
resolveHostPortFunction#
A config value as a host port: an integer from 1 to 65535, else fallback.
function resolveHostPort(value: unknown, fallback: number): numberParameters
| Name | Type | Description |
|---|---|---|
| value | unknown | The setting's value (a number, or a numeric string). |
| fallback | number | The port to use otherwise. |
Returns
numberstartContainerFunction#
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.
function startContainer(
container: LocalContainer,
options: StartContainerOptions,
deps?: ContainerDeps,
): Promise<StartedContainer>Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| container | LocalContainer | The container. | |
| options | StartContainerOptions | Port, extra run arguments and the missing-CLI message. | |
| deps? | ContainerDeps | {} | CLI, probe and sleep. |
Returns
Promise<StartedContainer>stopContainerFunction#
Stops the container when it runs. absent and already-stopped are not
failures; only a failed docker stop (or no docker) throws.
function stopContainer(
name: string,
deps?: Pick<ContainerDeps, 'exec'>,
): Promise<'absent' | 'already-stopped' | 'stopped'>Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| name | string | The container's name. | |
| deps? | Pick<ContainerDeps, 'exec'> | {} | CLI. |
Returns
Promise<'absent' | 'already-stopped' | 'stopped'>Constants#
defaultExecConstant#
The CLI, bounded by timeoutMs. Never rejects: a call that cannot start resolves with its error code (ENOENT).
const defaultExec: ExecInterfaces#
ContainerDepsInterface#
How a container is driven: the CLI, the health probe and the wait between probes. Each defaults to the real one.
interface ContainerDepsProperties
| Name | Type | Description |
|---|---|---|
| exec? | Exec | Runs a |
| probe? | (url: string) => Promise<boolean> | GET |
| sleep? | (ms: number) => Promise<void> | Waits between two health probes; a test passes one that resolves at once. |
ExecResultInterface#
What a CLI call answered.
interface ExecResultProperties
| Name | Type | Description |
|---|---|---|
| code | number | Exit code; |
| error? | string | Spawn-level failure ( |
| stderr | string | Its error output: where |
| stdout | string | What the call printed. |
LocalContainerInterface#
An extension's container.
interface LocalContainerProperties
| Name | Type | Description |
|---|---|---|
| command? | readonly string[] | The command and its arguments after the image ( |
| containerPort | number | The server's port inside the container. |
| healthPath | string | The path probed on the published URL to tell the server answers ( |
| image | string | The image it runs. |
| name | string | The container's name ( |
StartContainerOptionsInterface#
Options of startContainer.
interface StartContainerOptionsProperties
| Name | Type | Description |
|---|---|---|
| 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 |
| runArgs? | () => readonly string[] | Promise<readonly string[]> | Extra |
StartedContainerInterface#
What startContainer did.
interface StartedContainerProperties
| Name | Type | Description |
|---|---|---|
| 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. |
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).
type ContainerState =
| { state: 'unknown' }
| { state: 'absent' }
| { state: 'stopped' }
| { hostPort?: number; state: 'running' }ExecType#
Runs argv (the program first), giving up after timeoutMs. Never rejects.
type Exec = (argv: readonly string[], timeoutMs: number) => Promise<ExecResult>