Exemplo n.º 1
0
func GetContainers() ([]Container, error) {
	listOpts := docker.ListContainersOptions{}
	listOpts.All = true
	listOpts.Limit = 1000
	listOpts.Size = true

	apiContainers, err := client.ListContainers(listOpts)

	if err != nil {
		return nil, err
	}

	containers := make([]Container, 0, len(apiContainers))
	for _, c := range apiContainers {
		ports := make([]Port, 0, 5)
		for _, p := range c.Ports {
			port := Port{
				Private: uint64(p.PrivatePort),
				Public:  uint64(p.PublicPort),
				Type:    p.Type,
				IP:      p.IP,
			}
			ports = append(ports, port)
		}

		labels := make([]Label, 0, 5)
		for k, v := range c.Labels {
			labels = append(labels, Label{k, v})
		}

		c := Container{
			Command:    c.Command,
			Id:         c.ID,
			Created:    c.Created,
			Image:      c.Image,
			Name:       c.Names[0][1:],
			Ports:      ports,
			Labels:     labels,
			RootFsSize: c.SizeRootFs,
			RwSize:     c.SizeRw,
			Status:     c.Status,
		}

		containers = append(containers, c)
	}

	return containers, nil
}