Created
July 5, 2016 18:13
-
-
Save clinta/3f57efba0e482c209adf3ad442cb527c to your computer and use it in GitHub Desktop.
docker error connecting bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
dockerclient "github.com/docker/engine-api/client" | |
dockerTypes "github.com/docker/engine-api/types" | |
dockerCTypes "github.com/docker/engine-api/types/container" | |
dockerNTypes "github.com/docker/engine-api/types/network" | |
"golang.org/x/net/context" | |
) | |
func main() { | |
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"} | |
dc, err := dockerclient.NewClient("unix:///var/run/docker.sock", "v1.23", nil, defaultHeaders) | |
if err != nil { | |
panic(err) | |
} | |
bg := context.Background() | |
i := 0 | |
s := 0 | |
nr, err := dc.NetworkCreate(bg, "t2", dockerTypes.NetworkCreate{}) | |
if err != nil { | |
panic(err) | |
} | |
defer func(id string) { | |
err := dc.NetworkRemove(bg, id) | |
if err != nil { | |
panic(err) | |
} | |
}(nr.ID) | |
for { | |
err := func() error { | |
i++ | |
r, err := dc.ContainerCreate(bg, | |
&dockerCTypes.Config{ | |
Image: "alpine", | |
Entrypoint: []string{"/bin/sleep", "600"}, | |
}, | |
&dockerCTypes.HostConfig{}, | |
&dockerNTypes.NetworkingConfig{ | |
EndpointsConfig: map[string]*dockerNTypes.EndpointSettings{ | |
// Using nr.ID fails most of the time | |
nr.ID: {}, | |
// Using the network name succeeds always | |
//"t2": {}, | |
}, | |
}, | |
"") | |
if err != nil { | |
panic(err) | |
} | |
err = dc.ContainerStart(bg, r.ID, dockerTypes.ContainerStartOptions{}) | |
if err != nil { | |
return err | |
} | |
err = dc.ContainerKill(bg, r.ID, "") | |
if err != nil { | |
panic(err) | |
} | |
err = dc.ContainerRemove(bg, r.ID, dockerTypes.ContainerRemoveOptions{}) | |
if err != nil { | |
panic(err) | |
} | |
return nil | |
}() | |
if err == nil { | |
s++ | |
} | |
fmt.Printf("%v of %v succeeded\n", s, i) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment