Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListContainers fails listing all containers #85

Closed
DimShadoWWW opened this issue Apr 4, 2014 · 4 comments
Closed

ListContainers fails listing all containers #85

DimShadoWWW opened this issue Apr 4, 2014 · 4 comments
Labels

Comments

@DimShadoWWW
Copy link

ListContainers with "docker.ListContainersOptions{All: true}" returns an empty list
while "docker.ListContainersOptions{All: false}" or "docker.ListContainersOptions{}"
returns the list of containers running

I'm using docker version 0.9.0.

my code:

package main
import (
    "fmt"
    "github.com/fsouza/go-dockerclient"
)
func main() {
    client, _ := docker.NewClient("unix:///var/run/docker.sock")
    conts, _ := client.ListContainers(docker.ListContainersOptions{All: true})
    for _, cont := range conts {
        fmt.Println("ID: ", cont.ID)
    }
}
@fsouza fsouza added the bug label Apr 5, 2014
@DimShadoWWW
Copy link
Author

it fails in line 63 of container.go

err = json.Unmarshal(body, &containers)

I had to connect directly to docker, reusing the code of the client API and http

@fsouza
Copy link
Owner

fsouza commented Apr 7, 2014

@DimShadoWWW looks like Docker has changed the API, I will have a look.

It seems that you managed to get it working, what did you do? Would you be willing to send a patch?

Thanks!

@DimShadoWWW
Copy link
Author

@fsouza .. this is the code of my function (I hasn't clean or organized yet):

func (l *Lib) GetContainers(all bool) ([]APIContainers, error) {
    query := "0"
    if all {
        query = "1"
    }
    req, err := http.NewRequest("GET", "/containers/json?all="+query, nil)
    if err != nil {
        fmt.Errorf("ERROR: ", err)
        return nil, err
    }
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("User-Agent", "mine")
    var resp *http.Response
    dial, err := net.Dial("unix", "/var/run/docker.sock")
    if err != nil {
        fmt.Errorf("ERROR: ", err)
        return nil, err
    }
    clientconn := httputil.NewClientConn(dial, nil)
    resp, err = clientconn.Do(req)
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Errorf("ERROR: ", err)
        return nil, err
    }
    outs := engine.NewTable("Created", 0)
    if _, err := outs.ReadListFrom(body); err != nil {
        fmt.Errorf("ERROR: ", err)
        return nil, err
    }
    var containers []APIContainers
    for _, out := range outs.Data {
        var c APIContainers
        c.ID = out.Get("Id")
        c.Names = out.GetList("Names")
        c.Image = out.Get("Image")
        c.Command = out.Get("Command")
        c.Created = out.GetInt64("Created")
        c.Status = out.Get("Status")
        c.SizeRw = out.GetInt64("SizeRw")
        c.SizeRootFs = out.GetInt64("SizeRootFs")
        ports := engine.NewTable("", 0)
        ports.ReadListFrom([]byte(out.Get("Ports")))
        ports.SetKey("PublicPort")
        ports.Sort()
        for _, port := range ports.Data {
            var p APIPort
            p.IP = port.Get("IP")
            p.PrivatePort = port.GetInt64("PrivatePort")
            p.PublicPort = port.GetInt64("PublicPort")
            p.Type = port.Get("Type")
            c.Ports = append(c.Ports, p)
        }
        containers = append(containers, c)
    }
    return containers, nil
}

@fsouza
Copy link
Owner

fsouza commented Jul 9, 2014

It's working with the latest version of Docker.

I've tried Docker 1.0 and 1.1. I'm closing this issue.

@DimShadoWWW please let me know if you find this or any other issue in the future! :)

@fsouza fsouza closed this as completed Jul 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants