docker

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package docker is the docker behavior: the Docker containers of the host. It reads them through gohai's docker collector, and changes them with the docker CLI, on each platform where docker answers.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"docker", "The Docker containers of the host",
	behavior.Title("Container"),
	behavior.Node,
	behavior.API,
	behavior.Area(area.Containers),
	behavior.CLI,
	behavior.Platform(behavior.PlatformLinux, newCLI),
	behavior.Platform(behavior.PlatformDarwin, newCLI),
	behavior.Sudo("docker"),
	behavior.Runs("docker"),
	behavior.Read("list", List).
		Describe("List the containers of the host").
		Explain("Reads each container through gohai's docker collector, which runs docker ps "+
			"and docker inspect. state limits the list to the running or the stopped ones. "+
			"A host whose docker does not answer reports skipped. The environment of a "+
			"container shows only the names of its variables: osapi never reads their values.").
		Example("A host with a container", ListInput{}, []Entry{web},
			answers, running, details,
		).
		Example("Only the stopped ones", ListInput{State: "stopped"}, []Entry{},
			answers, running, details,
		).
		Fails("A state that is not one", ListInput{State: "paused"}, "enum"),
	behavior.Read("get", Get).Key("name").
		Describe("Get one container").
		Explain("Reads one container by its name, its ID or the start of its ID. A container "+
			"that the host does not have is not found.").
		Example("A container", GetInput{Name: name}, web, answers, running, details,
			behavior.NotLive("the container is the one that the docker write examples make, "+
				"and their reads back read it")).
		Fails("A container that the host does not have", GetInput{Name: "nothere"}, "not found").
		Fails("A name that is an option", GetInput{Name: "-f"}, "container_id"),
	behavior.Write("create", Create).
		Describe("Create a container").
		Explain("Runs docker create, which pulls the image when the host does not have it. "+
			"The environment goes on docker's standard input, not in its arguments, which "+
			"each user of the host can read. auto_start starts the container once it is made. "+
			"A container of the name that the host has already is not made again, and "+
			"reports changed: false.").
		Example("Create a container", toCreate, Result{Name: name, ID: id, Changed: true},
			answers, made, created, details,
			behavior.Ran("docker", "create", "--name", name, image, "sleep", "300"),
			behavior.Reads(GetInput{Name: "osapitest"}, Entry{Name: "osapitest", Image: "alpine:3.20"}),
		).
		Example("A container that the host has", toCreate, Result{Name: name, ID: id},
			answers, made, created, details,
			behavior.After(toCreate),
			behavior.Reads(GetInput{Name: "osapitest"}, Entry{Name: "osapitest", Image: "alpine:3.20"}),
		).
		Fails("A name that is an option", CreateInput{Name: "-f", Image: image}, "container_id").
		Fails("No image", CreateInput{Name: name}, "required").
		Fails("An image that is an option", CreateInput{Name: name, Image: "--privileged"}, "image_ref").
		Fails("A hostname that is an option",
			CreateInput{Name: name, Image: image, HostName: "-x"}, "dns_name").
		Fails("A hostname longer than 253 characters",
			CreateInput{Name: name, Image: image, HostName: strings.Repeat("a", 254)}, "max").
		Fails("A DNS server that is not an address",
			CreateInput{Name: name, Image: image, DNS: []string{"nameserver"}}, "ip").
		Fails("A port that is not host:container",
			CreateInput{Name: name, Image: image, Ports: []string{"80"}}, "port_mapping").
		Fails("A bind mount that is not two absolute paths",
			CreateInput{
				Name:    name,
				Image:   image,
				Volumes: []string{"data:/data"},
			}, "volume_mapping").
		Fails("No name", CreateInput{Image: image}, "required"),
	behavior.Write("delete", Delete).Key("name").
		Describe("Remove a container").
		Explain("Runs docker rm -f, which also removes a container that runs: a delete "+
			"leaves no container of the name. A container that the host does not have "+
			"reports changed: false.").
		Example("Remove a container", DeleteInput{Name: name}, Result{Name: name, Changed: true},
			answers, made, created, removed, details,
			behavior.After(toCreate),
			behavior.Ran("docker", "rm", "-f", name),
			behavior.ReadsNone(GetInput{Name: "osapitest"}, Entry{Name: "osapitest"}, "name"),
		).
		Example("A container that the host does not have", DeleteInput{Name: name},
			Result{Name: name}, answers,
			behavior.ReadsNone(GetInput{Name: "osapitest"}, Entry{Name: "osapitest"}, "name"),
		).
		Fails("A name that is an option", DeleteInput{Name: "-f"}, "container_id"),
	behavior.Action("start", Start).Key("name").
		Describe("Start a container").
		Explain("Runs docker start. A container that runs already reports changed: false, and "+
			"one that the host does not have is not found.").
		Example("Start a container", StartInput{Name: name}, Result{Name: name, Changed: true},
			answers, made, created, started, details,
			behavior.After(toCreate),
			behavior.Ran("docker", "start", name),
			behavior.Reads(GetInput{Name: "osapitest"}, Entry{Name: "osapitest", State: "running"}),
		).
		Example("A container that runs already", StartInput{Name: name}, Result{Name: name},
			answers, made, created, started, details,
			behavior.After(toRun),
			behavior.Reads(GetInput{Name: "osapitest"}, Entry{Name: "osapitest", State: "running"}),
		).
		Fails("A container that the host does not have", StartInput{Name: "nothere"}, "not found").
		Fails("A name that is an option", StartInput{Name: "-f"}, "container_id"),
	behavior.Action("stop", Stop).Key("name").
		Describe("Stop a container").
		Explain("Runs docker stop, which waits timeout seconds before docker kills the "+
			"container, or docker's default of 10 when it is 0. A container that does not "+
			"run reports changed: false.").
		Example("Stop a container", StopInput{Name: name, Timeout: 1}, Result{Name: name, Changed: true},
			answers, made, created, started, stopped, details,
			behavior.After(toRun),
			behavior.Ran("docker", "stop", "-t", "1", name),
			behavior.ReadsNone(GetInput{Name: "osapitest"}, Entry{Name: "osapitest", State: "running"}, "name", "state"),
		).
		Example("A container that does not run", StopInput{Name: name, Timeout: 1}, Result{Name: name},
			answers, made, created, details,
			behavior.After(toCreate),
			behavior.ReadsNone(GetInput{Name: "osapitest"}, Entry{Name: "osapitest", State: "running"}, "name", "state"),
		).
		Fails("A container that the host does not have", StopInput{Name: "nothere"}, "not found").
		Fails("A name that is an option", StopInput{Name: "-f"}, "container_id").
		Fails("A timeout longer than 300 seconds", StopInput{Name: name, Timeout: 301}, "max").
		Fails("A negative timeout", StopInput{Name: name, Timeout: -1}, "min"),
	behavior.Action("exec", Exec).Key("name").
		ReadsNothing("the output of the command in the container is its result, which the call answers").
		PermitAs(types.VerbExec).
		Always().
		Describe("Run a command in a container").
		Explain("Runs docker exec, and answers what the command wrote to standard output and "+
			"to standard error, and its exit code. A command that exits with a code other "+
			"than 0 is an answer, not a failure. It changes what the command changes, which "+
			"osapi cannot tell, so it reports changed: true. Its permission is docker:exec, "+
			"which only the admin role gets, apart from the docker:execute of start and stop. An agent with agent.command.enabled false refuses it, as it "+
			"refuses a command.").
		Example("Run a command", ExecInput{Name: name, Command: []string{"echo", "hello"}},
			ExecResult{Stdout: "hello\n", Changed: true},
			answers, made, created, started, details,
			behavior.Prints("docker exec osapitest echo hello", "hello\n"),
			behavior.After(toRun),
			behavior.Ran("docker", "exec", name, "echo", "hello"),
		).
		Fails("An empty command", ExecInput{Name: name, Command: []string{}}, "min").
		Fails("No command", ExecInput{Name: name}, "required").
		Fails("A name that is an option", ExecInput{Name: "-f", Command: []string{"true"}}, "container_id").
		Fails("A working directory that is not absolute",
			ExecInput{Name: name, Command: []string{"pwd"}, WorkingDir: "tmp"}, "startswith"),
))

Behavior is the docker definition.

Functions

This section is empty.

Types

type CreateInput

type CreateInput struct {
	Name      string                  `` /* 185-byte string literal not displayed */
	Image     string                  `` /* 168-byte string literal not displayed */
	HostName  string                  `json:"container_hostname,omitempty" validate:"omitempty,dns_name,max=253"    doc:"The hostname inside the container"`
	DNS       []string                `json:"dns,omitempty"                validate:"omitempty,dive,ip"             doc:"The DNS servers of the container"`
	Command   []string                `json:"command,omitempty"            validate:"omitempty"                     doc:"The command, in place of the image's own"`
	Env       map[string]secret.Value `json:"env,omitempty"                validate:"omitempty"                     doc:"The environment variables of the container"`
	Ports     []string                `` /* 140-byte string literal not displayed */
	Volumes   []string                `` /* 144-byte string literal not displayed */
	AutoStart bool                    `json:"auto_start,omitempty"         validate:"omitempty"                     doc:"Start the container once it is created"`
}

CreateInput is create's input.

type DeleteInput

type DeleteInput struct {
	Name string `json:"name" validate:"required,container_id" doc:"The container's name or ID"`
}

DeleteInput is delete's input.

type Entry

type Entry struct {
	ID       string   `json:"id"                  doc:"The container's full ID"`
	Name     string   `json:"name"                doc:"The container's name"`
	Image    string   `json:"image"               doc:"The image it runs"`
	State    string   `` /* 129-byte string literal not displayed */
	Summary  string   `` /* 139-byte string literal not displayed */
	Created  string   `` /* 127-byte string literal not displayed */
	Health   string   `` /* 139-byte string literal not displayed */
	Ports    []string `` /* 139-byte string literal not displayed */
	Mounts   []string `` /* 139-byte string literal not displayed */
	Networks []string `` /* 139-byte string literal not displayed */
	EnvNames []string `` /* 139-byte string literal not displayed */
}

Entry is one container, as list and get answer it.

func Get

func Get(
	ctx context.Context,
	p Provider,
	in GetInput,
) (Entry, error)

Get reads one container.

func List

func List(
	ctx context.Context,
	p Provider,
	in ListInput,
) ([]Entry, error)

List reads the containers of the host.

type ExecInput

type ExecInput struct {
	Name       string                  `json:"name"                  validate:"required,container_id"  doc:"The container's name or ID"`
	Command    []string                `json:"command"               validate:"required,min=1"         doc:"The command and its arguments"`
	Env        map[string]secret.Value `json:"env,omitempty"         validate:"omitempty"              doc:"Environment variables for the command"`
	WorkingDir string                  `json:"working_dir,omitempty" validate:"omitempty,startswith=/" doc:"The directory the command runs in"`
}

ExecInput is exec's input.

type ExecResult

type ExecResult struct {
	Stdout   string `json:"stdout"    doc:"What the command wrote to standard output"`
	Stderr   string `json:"stderr"    doc:"What the command wrote to standard error"`
	ExitCode int    `json:"exit_code" doc:"The command's exit code"`
	Changed  bool   `json:"changed"   doc:"Always true: osapi cannot tell what the command changed"`
}

ExecResult is exec's output.

func Exec

func Exec(
	ctx context.Context,
	p Provider,
	in ExecInput,
) (ExecResult, error)

Exec runs a command in a container.

type GetInput

type GetInput struct {
	Name string `json:"name" validate:"required,container_id" doc:"The container's name or ID"`
}

GetInput is get's input: the container, by its name or its ID.

type ListInput

type ListInput struct {
	State types.ContainerState `` /* 126-byte string literal not displayed */
}

ListInput is list's input.

type Provider

type Provider interface {
	List(ctx context.Context, state types.ContainerState) ([]Entry, error)
	Get(ctx context.Context, name string) (Entry, error)
	Create(ctx context.Context, in CreateInput) (Result, error)
	Delete(ctx context.Context, name string) (bool, error)
	Start(ctx context.Context, name string) (bool, error)
	Stop(ctx context.Context, name string, timeout int) (bool, error)
	Exec(ctx context.Context, in ExecInput) (ExecResult, error)
}

Provider reads the containers of the host and changes them. host.go is the one implementation: it reads through gohai's docker collector and changes through the docker CLI, the same on each platform.

type Result

type Result struct {
	Name    string `json:"name"         doc:"The container"`
	ID      string `json:"id,omitempty" doc:"The container's full ID, for create"`
	Changed bool   `json:"changed"      doc:"Whether the host changed"`
}

Result is the output of create, delete, start and stop.

func Create

func Create(
	ctx context.Context,
	p Provider,
	in CreateInput,
) (Result, error)

Create makes a container.

func Delete

func Delete(
	ctx context.Context,
	p Provider,
	in DeleteInput,
) (Result, error)

Delete removes a container.

func Start

func Start(
	ctx context.Context,
	p Provider,
	in StartInput,
) (Result, error)

Start starts a container.

func Stop

func Stop(
	ctx context.Context,
	p Provider,
	in StopInput,
) (Result, error)

Stop stops a container.

type StartInput

type StartInput struct {
	Name string `json:"name" validate:"required,container_id" doc:"The container's name or ID"`
}

StartInput is start's input.

type StopInput

type StopInput struct {
	Name    string `json:"name"              validate:"required,container_id"   doc:"The container's name or ID"`
	Timeout int    `` /* 129-byte string literal not displayed */
}

StopInput is stop's input.

Directories

Path Synopsis
Package mocks holds the generated double of the docker provider, for the methods' tests.
Package mocks holds the generated double of the docker provider, for the methods' tests.

Jump to

Keyboard shortcuts

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