Help us shape the future of Go! Take the 2026 Go Developer Survey

container

package
v0.37.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DockerRunCmd

func DockerRunCmd(image, cmdFile, name string, mounts, env []string) string

DockerRunCmd returns a `docker run` command line that will:

* Pull the given image if it is missing. * Create a container with the given name.

  • That will mount the current working directory inside the container and use it as the workdir.
  • That will also mount any given disk locations, in the format "/local/path:/inside/container/path" (the colon and inside path being optional if the same as local path).
  • That will set the given environment variables inside the container to their values outside the container.
  • That will run the command in the given file (by piping the file contents to /bin/sh); use PrepareCmdFile() to create one.

* Automatically remove the container when it exits.

func PrepareCmdFile

func PrepareCmdFile(ctx context.Context, cmd string) (string, func(), error)

PrepareCmdFile creates a temporary file containing the given command and returns its path, as well as a method you can defer that will delete the file.

File deletion errors in the returned method are logged using clog and the given ctx.

func SingularityRunCmd

func SingularityRunCmd(image, cmdFile string, mounts []string) string

SingularityRunCmd returns a `singularity shell` command line that will:

  • Pull the given image if it is missing, creating a sif image if it's a docker image.
  • Create a container.
  • That will run the command in the given file (by piping the file contents to the container's shell); use PrepareCmdFile() to create one.
  • That will mount the given disk locations, in the format "/local/path:/inside/container/path" (the colon and inside path being optional if the same as local path). The CWD is always mounted at / in container.
  • That will have all environment variables outside the container replicated inside the container.
  • Automatically remove the container when it exits.

Types

type Container

type Container struct {
	ID    string
	Names []string
	// contains filtered or unexported fields
}

Container struct represents a container type with specific properties.

func (*Container) HasName

func (c *Container) HasName(name string) bool

HasName checks if a given name is one of this container's names. Make sure to call the TrimNamePrefixes on the container before calling this function.

func (*Container) Stats

func (c *Container) Stats(ctx context.Context) (*Stats, error)

Stats returns the current memory usage (RSS, in MB) and total CPU usage (in seconds) of this container.

func (*Container) TrimNamePrefixes

func (c *Container) TrimNamePrefixes()

TrimNamePrefixes trims the '/' from this container's names. You must call this after adding names that might contain the prefix.

type Interactor

type Interactor interface {
	ContainerList(ctx context.Context) ([]*Container, error)
	ContainerStats(ctx context.Context, containerID string) (*Stats, error)
	ContainerKill(ctx context.Context, containerID string) error
}

Interactor defines some methods to query containers.

type OperationErr

type OperationErr string

OperationErr is supplied to OperatorErr to define the reasons for the failed operations.

const (
	ErrContainerList  OperationErr = "Could not list the containers"
	ErrContainerStats OperationErr = "Could not get the container stats"
	ErrContainerKill  OperationErr = "Container could not be killed"
)

ErrContainer* are the reasons for the failed operations.

type Operator

type Operator struct {
	// contains filtered or unexported fields
}

Operator offers some methods for working with containers. You must use the NewOperator() method to make one, or the methods won't work.

func NewOperator

func NewOperator(cntrInteractor Interactor) *Operator

NewOperator creates a new Operator, working on containers using the supplied Interactor. eg. to create an operator that can work with Docker containers:

import (
	"github.com/moby/moby/client"

	"github.com/VertebrateResequencing/wr/container/docker"
)
 ....
 {
	 cli, err := client.NewClientWithOpts(client.FromEnv)
	 dockerInterator := docker.NewInteractor(cli)
	 dockerOperator := NewOperator(dockerInterator)
	 cntrList, err := dockerOperator.GetCurrentContainers(ctx)
 }
 ...

func (*Operator) GetContainerByID

func (o *Operator) GetContainerByID(ctx context.Context, id string) (*Container, error)

GetContainerByID checks if the given id is the ID of a current container. If so, returns that container.

func (*Operator) GetContainerByPath

func (o *Operator) GetContainerByPath(ctx context.Context, path string, dir string) (*Container, error)

GetContainerByPath checks the file at the given path, and if the first line contains the ID of a container, that container is returned.

The path can contain globs: `?` to match any non-separator character and `*` to match any number of them. The first file to match the glob and also contain a valid id is the one used.

In case the name is a relative file path, the second argument is the absolute path to the working directory where your container was created.

This method is useful if container IDs are written to file, as they are eg. in the case of docker when using the --cidfile argument.

func (*Operator) GetCurrentContainers

func (o *Operator) GetCurrentContainers(ctx context.Context) ([]*Container, error)

GetCurrentContainers returns current containers.

func (*Operator) GetNewContainerByName

func (o *Operator) GetNewContainerByName(ctx context.Context, name string) (*Container, error)

GetNewContainerByName returns a container with given name. Only new containers (those not remembered by the last RememberCurrentContainers() call) are considered when searching for a container with that name.

func (*Operator) GetNewContainers

func (o *Operator) GetNewContainers(ctx context.Context) ([]*Container, error)

GetNewContainers returns the list of all the new containers that now exists and weren't previously remembered by a RememberCurrentContainers() call.

func (*Operator) KillContainer

func (o *Operator) KillContainer(ctx context.Context, containerID string) error

KillContainer kills the container with the given ID.

func (*Operator) RememberCurrentContainers

func (o *Operator) RememberCurrentContainers(ctx context.Context) error

RememberCurrentContainers calls GetCurrentContainers() and stores the results, for the benefit of a future GetNewContainers() call.

type OperatorError

type OperatorError struct {
	Type OperationErr // one of our OperationErr constants
	Err  error
}

OperatorError records an error and a reason that caused it.

func (*OperatorError) Error

func (et *OperatorError) Error() string

Error returns an error with the reason for a failed operation.

func (*OperatorError) Unwrap

func (et *OperatorError) Unwrap() error

Unwrap method returns contained error of OperatorErr.

type Stats

type Stats struct {
	MemoryMB int
	CPUSec   int
}

Stats struct represents a container stats type with memory and cpu properties.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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