Add a predefined name to containers

This commit is contained in:
nemunaire 2021-05-09 00:55:45 +02:00
commit 5aa2d72da0
3 changed files with 54 additions and 1 deletions

View file

@ -16,6 +16,31 @@ func newCli() (*client.Client, error) {
return client.NewClientWithOpts(client.FromEnv)
}
func Ps() ([]types.Container, error) {
cli, err := newCli()
if err != nil {
return nil, err
}
return cli.ContainerList(context.Background(), types.ContainerListOptions{})
}
func PsName() (ret map[string]string, err error) {
containers, err := Ps()
if err != nil {
return nil, err
}
ret = map[string]string{}
for _, container := range containers {
for _, name := range container.Names {
ret[name] = container.ID
}
}
return ret, err
}
func Run(image string, cmd []string, mounts []mount.Mount) (string, error) {
cli, err := newCli()
if err != nil {
@ -38,7 +63,7 @@ func Run(image string, cmd []string, mounts []mount.Mount) (string, error) {
Architecture: "amd64",
OS: "linux",
},
"",
genContainerName(image),
)
if err != nil {
return "", err