docker

package
v0.0.1-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Cli *client.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(host, ca, cert, key string) *Client

func (*Client) ContainerCreate

func (c *Client) ContainerCreate(ctx context.Context, containerName string, containerConfig *container.Config, hostConfig *container.HostConfig) error

ContainerCreate 1. 配置要启动的容器

containerConfig := &container.Config{
	Image: "docker@latest",                    // 指定要使用的镜像
	Cmd:   []string{"echo", "Hello, Docker!"}, // 指定容器启动时要执行的命令
	Env:   []string{"LOCAL_USER_ID=" + "id -u $USER"},	//
}

2. 配置容器自动重启 网络 磁盘挂载等

hostConfig := &container.HostConfig{
	RestartPolicy: container.RestartPolicy{
		Name: "always", // 设置重启策略为"always",容器将总是自动重启
		// 可选的重启策略:
		// - "no":无重启策略
		// - "always":容器总是自动重启
		// - "on-failure":容器在非零退出状态时重启(默认最多重启3次)
		// - "unless-stopped":除非手动停止,否则容器总是自动重启
	},
	NetworkMode: "host", // 设置主机网络模式,
	// 可选的策略:
	// - "none"
	// - "default"
	// - "host":与主机共享网络 无需配置端口映射
	Privileged: true,
	CapAdd:     []string{"SYS_ADMIN", "IPC_LOCK"},
	//PortBindings: nat.PortMap{
	//	"80/tcp": []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: "8080"}}, // 将容器的80端口映射到宿主机的8080端口
	//},
	Mounts: []mount.Mount{
		{
			Type:     mount.TypeBind,
			Source:   "/opt", // 宿主机上要挂载的文件夹路径
			Target:   "/opt", // 容器内挂载的路径
			ReadOnly: false,  // 是否只读
		},
	},
}

func (*Client) ContainerRemove

func (c *Client) ContainerRemove(ctx context.Context, containerIDorName string, force bool) error

func (*Client) ContainerStop

func (c *Client) ContainerStop(ctx context.Context, containerID string, timeout int) error

func (*Client) ImageTag

func (c *Client) ImageTag(ctx context.Context, imageID, imageTag string) error

ImageTag imageID => image@latest

func (*Client) LoadImage

func (c *Client) LoadImage(ctx context.Context, imagePath string) (string, error)

LoadImage

imagePath: /opt/raw.tar
return: sha256

Jump to

Keyboard shortcuts

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