Documentation
¶
Index ¶
- type Client
- func (c *Client) ContainerCreate(ctx context.Context, containerName string, containerConfig *container.Config, ...) error
- func (c *Client) ContainerRemove(ctx context.Context, containerIDorName string, force bool) error
- func (c *Client) ContainerStop(ctx context.Context, containerID string, timeout int) error
- func (c *Client) ImageTag(ctx context.Context, imageID, imageTag string) error
- func (c *Client) LoadImage(ctx context.Context, imagePath string) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type 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 (*Client) ContainerStop ¶
Click to show internal directories.
Click to hide internal directories.