/
index.go
58 lines (46 loc) 路 1.21 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package dockercontrol
import (
"context"
"fmt"
"github.com/applinh/elephant/utils"
u "github.com/applinh/elephant/utils"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
// Vol struct defines a docker-container volume
type Vol struct {
Labels map[string]string
}
// ListContainers list running containers
func ListContainers() ([]types.Container, error) {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
if len(containers) > 0 {
return containers, nil
}
return nil, &u.ErrorString{S: "NO_STACK"}
}
// StopContainer stop a container
func StopContainer(containerID string) error {
cli, err := client.NewEnvClient()
if err != nil {
return err
}
fmt.Println("Stopping " + containerID + " ...")
go utils.Loading()
return cli.ContainerStop(context.Background(), containerID, nil)
}
// InspectContainer retrieves container informations
func InspectContainer(containerID string) (types.ContainerJSON, error) {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
return cli.ContainerInspect(context.Background(), containerID)
}