mirror of https://github.com/1Panel-dev/1Panel
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.7 KiB
76 lines
1.7 KiB
package docker
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/docker/docker/client"
|
|
)
|
|
|
|
type Client struct {
|
|
cli *client.Client
|
|
}
|
|
|
|
func NewClient() (Client, error) {
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
if err != nil {
|
|
return Client{}, err
|
|
}
|
|
|
|
return Client{
|
|
cli: cli,
|
|
}, nil
|
|
}
|
|
|
|
func NewDockerClient() (*client.Client, error) {
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cli, nil
|
|
}
|
|
|
|
func (c Client) ListAllContainers() ([]types.Container, error) {
|
|
var options types.ContainerListOptions
|
|
containers, err := c.cli.ContainerList(context.Background(), options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return containers, nil
|
|
}
|
|
|
|
func (c Client) ListContainersByName(names []string) ([]types.Container, error) {
|
|
var options types.ContainerListOptions
|
|
options.All = true
|
|
if len(names) > 0 {
|
|
var array []filters.KeyValuePair
|
|
for _, n := range names {
|
|
array = append(array, filters.Arg("name", n))
|
|
}
|
|
options.Filters = filters.NewArgs(array...)
|
|
}
|
|
containers, err := c.cli.ContainerList(context.Background(), options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return containers, nil
|
|
}
|
|
|
|
func (c Client) CreateNetwork(name string) error {
|
|
_, err := c.cli.NetworkCreate(context.Background(), name, types.NetworkCreate{
|
|
Driver: "bridge",
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (c Client) NetworkExist(name string) bool {
|
|
var options types.NetworkListOptions
|
|
options.Filters = filters.NewArgs(filters.Arg("name", name))
|
|
networks, err := c.cli.NetworkList(context.Background(), options)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return len(networks) > 0
|
|
}
|