Documentation
¶
Overview ¶
Package exec provides functionality for executing commands in Docker containers.
Index ¶
- Variables
- func Create(ctx context.Context, client client.APIClient, containerID string, ...) (string, error)
- func CreateAndWait(ctx context.Context, client client.APIClient, containerID string, ...) (int, []byte, []byte, error)
- func CreateMultiple(ctx context.Context, client client.APIClient, containerID string, ...) ([]string, error)
- func GetRunningExecsCount(ctx context.Context, client client.APIClient, containerID string, ...) (int, error)
- func InspectMultiple(ctx context.Context, client client.APIClient, execIDs []string, ...) (map[string]*Info, error)
- func SanitizeCommand(cmd []string) ([]string, error)
- func Start(ctx context.Context, client client.APIClient, execID string, ...) (io.ReadCloser, error)
- func StartAndWait(ctx context.Context, client client.APIClient, execID string, ...) (int, []byte, []byte, error)
- func StartWithStdCopy(ctx context.Context, client client.APIClient, execID string, ...) error
- func ValidateUser(user string) bool
- func WaitForExecToComplete(ctx context.Context, client client.APIClient, execID string, ...) (int, error)
- type CreateOptions
- type ExecConfig
- type Info
- type InspectOptions
- type OutputWriter
- type StartOptions
- type StdWriter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrContainerNotFound indicates the container was not found ErrContainerNotFound = errors.New("container not found") // ErrContainerNotRunning indicates the container is not running ErrContainerNotRunning = errors.New("container not running") // ErrInvalidCommand indicates an invalid command ErrInvalidCommand = errors.New("invalid command") // ErrExecNotFound indicates the exec instance was not found ErrExecNotFound = errors.New("exec instance not found") // ErrExecCreateFailed indicates exec creation failed ErrExecCreateFailed = errors.New("exec creation failed") )
Common errors
var DefaultSecurityValidator = func(config ExecConfig) error { dangerousCommands := []string{ "rm -rf /", "mkfs", "dd", "reboot", "shutdown", "halt", "poweroff", "init", "chmod -R 777", "chown -R", "iptables", "route", } cmdStr := strings.Join(config.Cmd, " ") for _, dc := range dangerousCommands { if strings.Contains(cmdStr, dc) { return fmt.Errorf("potentially dangerous command detected: %s", dc) } } if config.Privileged { return fmt.Errorf("privileged mode not allowed") } return nil }
DefaultSecurityValidator is the default security validator
Functions ¶
func Create ¶
func Create(ctx context.Context, client client.APIClient, containerID string, config ExecConfig, options CreateOptions) (string, error)
Create creates an exec instance in a container
func CreateAndWait ¶
func CreateAndWait(ctx context.Context, client client.APIClient, containerID string, config ExecConfig, options CreateOptions) (int, []byte, []byte, error)
CreateAndWait creates an exec instance and waits for it to complete
func CreateMultiple ¶
func CreateMultiple(ctx context.Context, client client.APIClient, containerID string, configs []ExecConfig, options CreateOptions) ([]string, error)
CreateMultiple creates multiple exec instances in a container
func GetRunningExecsCount ¶
func GetRunningExecsCount(ctx context.Context, client client.APIClient, containerID string, options InspectOptions) (int, error)
GetRunningExecsCount gets the number of running execs for a container (UNRELIABLE)
func InspectMultiple ¶
func InspectMultiple(ctx context.Context, client client.APIClient, execIDs []string, options InspectOptions) (map[string]*Info, error)
InspectMultiple inspects multiple exec instances
func SanitizeCommand ¶
SanitizeCommand sanitizes a command for exec
func Start ¶
func Start(ctx context.Context, client client.APIClient, execID string, options StartOptions) (io.ReadCloser, error)
Start starts an exec instance and returns an io.ReadCloser for the output stream
func StartAndWait ¶
func StartAndWait(ctx context.Context, client client.APIClient, execID string, options StartOptions) (int, []byte, []byte, error)
StartAndWait starts an exec instance and waits for it to complete
func StartWithStdCopy ¶
func StartWithStdCopy(ctx context.Context, client client.APIClient, execID string, options StartOptions, output OutputWriter) error
StartWithStdCopy starts an exec instance and copies its output to the provided writers
Types ¶
type CreateOptions ¶
type CreateOptions struct {
// Timeout is the timeout for the operation
Timeout time.Duration
// Logger for logging
Logger *logrus.Logger
// SecurityValidator validates security settings
SecurityValidator func(config ExecConfig) error
}
CreateOptions defines options for creating an exec instance
type ExecConfig ¶
type ExecConfig struct {
// Command to execute in the container
Cmd []string
// User that will run the command
User string
// AttachStdin indicates whether to attach to stdin
AttachStdin bool
// AttachStdout indicates whether to attach to stdout
AttachStdout bool
// AttachStderr indicates whether to attach to stderr
AttachStderr bool
// Tty indicates whether to allocate a TTY
Tty bool
// DetachKeys is the key sequence used to detach from the container
DetachKeys string
// Env are additional environment variables
Env []string
// WorkingDir is the working directory
WorkingDir string
// Privileged indicates whether to run the command with extended privileges
Privileged bool
// SecurityOpts are security options
SecurityOpts []string
}
ExecConfig defines configuration for creating an exec instance
type Info ¶
type Info struct {
ID string `json:"id"`
ContainerID string `json:"container_id"`
Command []string `json:"command"`
User string `json:"user"`
Running bool `json:"running"`
ExitCode int `json:"exit_code"`
Pid int `json:"pid"`
StartedAt time.Time `json:"started_at,omitempty"`
FinishedAt time.Time `json:"finished_at,omitempty"`
Privileged bool `json:"privileged"`
OpenStdin bool `json:"open_stdin"`
OpenStdout bool `json:"open_stdout"`
OpenStderr bool `json:"open_stderr"`
}
Info represents information about an exec instance
func Inspect ¶
func Inspect(ctx context.Context, client client.APIClient, execID string, options InspectOptions) (*Info, error)
Inspect inspects an exec instance
type InspectOptions ¶
InspectOptions defines options for inspecting an exec instance
type OutputWriter ¶
type OutputWriter interface {
Write(p []byte) (n int, err error)
WriteErr(p []byte) (n int, err error)
Close() error
}
OutputWriter defines an interface for writing to stdout and stderr
type StartOptions ¶
type StartOptions struct {
Timeout time.Duration
Input io.Reader
RawOutput bool
DetachKeys string
Logger *logrus.Logger
// Fields from ExecConfig needed for start/attach logic
AttachStdin bool
AttachStdout bool
AttachStderr bool
Tty bool
}
StartOptions defines options for starting an exec instance