本文整理匯總了Golang中github.com/fsouza/go-dockerclient.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestCreateContainerForward
func (s *S) TestCreateContainerForward(c *gocheck.C) {
cmutex.Lock()
oldClusterNodes := clusterNodes
clusterNodes = map[string]string{
"server": "http://localhost:8081",
}
cmutex.Unlock()
defer func() {
cmutex.Lock()
clusterNodes = oldClusterNodes
cmutex.Unlock()
}()
err := newImage("tsuru/python", s.server.URL())
c.Assert(err, gocheck.IsNil)
client, err := dockerClient.NewClient(s.server.URL())
c.Assert(err, gocheck.IsNil)
images, err := client.ListImages(true)
c.Assert(err, gocheck.IsNil)
cmds := []string{"ps", "-ef"}
app := testing.NewFakeApp("myapp", "python", 1)
cont := container{Name: "myName", AppName: app.GetName(), Type: app.GetPlatform(), Status: "created"}
context := action.FWContext{Previous: cont, Params: []interface{}{app, images[0].ID, cmds}}
r, err := createContainer.Forward(context)
c.Assert(err, gocheck.IsNil)
cont = r.(container)
defer cont.remove()
c.Assert(cont, gocheck.FitsTypeOf, container{})
c.Assert(cont.ID, gocheck.Not(gocheck.Equals), "")
c.Assert(cont.HostAddr, gocheck.Equals, "localhost")
dcli, err := dockerClient.NewClient(s.server.URL())
c.Assert(err, gocheck.IsNil)
cc, err := dcli.InspectContainer(cont.ID)
c.Assert(err, gocheck.IsNil)
c.Assert(cc.State.Running, gocheck.Equals, false)
}
示例2: TestSchedulerSchedule
func (s *SchedulerSuite) TestSchedulerSchedule(c *gocheck.C) {
server0, err := testing.NewServer(nil)
c.Assert(err, gocheck.IsNil)
defer server0.Stop()
server1, err := testing.NewServer(nil)
c.Assert(err, gocheck.IsNil)
defer server1.Stop()
server2, err := testing.NewServer(nil)
c.Assert(err, gocheck.IsNil)
defer server2.Stop()
var buf bytes.Buffer
client, _ := dcli.NewClient(server0.URL())
client.PullImage(dcli.PullImageOptions{Repository: "tsuru/python"}, &buf)
client, _ = dcli.NewClient(server1.URL())
client.PullImage(dcli.PullImageOptions{Repository: "tsuru/python"}, &buf)
client, _ = dcli.NewClient(server2.URL())
client.PullImage(dcli.PullImageOptions{Repository: "tsuru/python"}, &buf)
a1 := app.App{Name: "impius", Teams: []string{"tsuruteam", "nodockerforme"}}
a2 := app.App{Name: "mirror", Teams: []string{"tsuruteam"}}
a3 := app.App{Name: "dedication", Teams: []string{"nodockerforme"}}
cont1 := container{ID: "1", Name: "impius1", AppName: a1.Name}
cont2 := container{ID: "2", Name: "mirror1", AppName: a2.Name}
cont3 := container{ID: "3", Name: "dedication1", AppName: a3.Name}
err = s.storage.Apps().Insert(a1, a2, a3)
c.Assert(err, gocheck.IsNil)
defer s.storage.Apps().RemoveAll(bson.M{"name": bson.M{"$in": []string{a1.Name, a2.Name, a3.Name}}})
coll := s.storage.Collection(schedulerCollection)
err = coll.Insert(
node{ID: "server0", Address: server0.URL(), Teams: []string{"tsuruteam"}},
node{ID: "server1", Address: server1.URL(), Teams: []string{"tsuruteam"}},
node{ID: "server2", Address: server2.URL()},
)
c.Assert(err, gocheck.IsNil)
defer coll.RemoveAll(bson.M{"_id": bson.M{"$in": []string{"server0", "server1", "server2"}}})
contColl := collection()
err = contColl.Insert(
cont1, cont2, cont3,
)
c.Assert(err, gocheck.IsNil)
defer contColl.RemoveAll(bson.M{"name": bson.M{"$in": []string{cont1.Name, cont2.Name, cont3.Name}}})
var scheduler segregatedScheduler
config := docker.Config{Cmd: []string{"/usr/sbin/sshd", "-D"}, Image: "tsuru/python"}
opts := dcli.CreateContainerOptions{Name: cont1.Name}
node, _, err := scheduler.Schedule(opts, &config)
c.Assert(err, gocheck.IsNil)
c.Check(node, gocheck.Equals, "server1")
config = docker.Config{Cmd: []string{"/usr/sbin/sshd", "-D"}, Image: "tsuru/python"}
opts = dcli.CreateContainerOptions{Name: cont2.Name}
node, _, err = scheduler.Schedule(opts, &config)
c.Assert(err, gocheck.IsNil)
c.Check(node == "server0" || node == "server1", gocheck.Equals, true)
config = docker.Config{Cmd: []string{"/usr/sbin/sshd", "-D"}, Image: "tsuru/python"}
opts = dcli.CreateContainerOptions{Name: cont3.Name}
node, _, err = scheduler.Schedule(opts, &config)
c.Assert(err, gocheck.IsNil)
c.Check(node, gocheck.Equals, "server2")
}
示例3: NewDockerClient
func NewDockerClient(endpoint string) (*docker.Client, error) {
if strings.HasPrefix(endpoint, "unix:") {
return docker.NewClient(endpoint)
} else if tlsVerify || tlsEnabled() {
if tlsVerify {
if e, err := pathExists(tlsCaCert); !e || err != nil {
return nil, errors.New("TLS verification was requested, but CA cert does not exist")
}
}
return docker.NewTLSClient(endpoint, tlsCert, tlsKey, tlsCaCert)
}
return docker.NewClient(endpoint)
}
示例4: TestNewContainer
func (s *S) TestNewContainer(c *gocheck.C) {
oldClusterNodes := clusterNodes
clusterNodes = map[string]string{"server": s.server.URL()}
defer func() { clusterNodes = oldClusterNodes }()
err := newImage("tsuru/python", s.server.URL())
c.Assert(err, gocheck.IsNil)
app := testing.NewFakeApp("app-name", "python", 1)
rtesting.FakeRouter.AddBackend(app.GetName())
defer rtesting.FakeRouter.RemoveBackend(app.GetName())
cont, err := newContainer(app, getImage(app), []string{"docker", "run"})
c.Assert(err, gocheck.IsNil)
defer s.removeTestContainer(&cont)
c.Assert(cont.ID, gocheck.Not(gocheck.Equals), "")
c.Assert(cont, gocheck.FitsTypeOf, container{})
c.Assert(cont.AppName, gocheck.Equals, app.GetName())
c.Assert(cont.Type, gocheck.Equals, app.GetPlatform())
u, _ := url.Parse(s.server.URL())
host, _, _ := net.SplitHostPort(u.Host)
c.Assert(cont.HostAddr, gocheck.Equals, host)
port, err := getPort()
c.Assert(err, gocheck.IsNil)
c.Assert(cont.Port, gocheck.Equals, port)
user, err := config.GetString("docker:ssh:user")
c.Assert(err, gocheck.IsNil)
dcli, _ := dockerClient.NewClient(s.server.URL())
container, err := dcli.InspectContainer(cont.ID)
c.Assert(err, gocheck.IsNil)
c.Assert(container.Config.User, gocheck.Equals, user)
}
示例5: Open
func (d *DockerDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
cleanupContainer, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.container", "true"))
if err != nil {
return nil, fmt.Errorf("Unable to parse docker.cleanup.container: %s", err)
}
cleanupImage, err := strconv.ParseBool(d.config.ReadDefault("docker.cleanup.image", "true"))
if err != nil {
return nil, fmt.Errorf("Unable to parse docker.cleanup.image: %s", err)
}
// Split the handle
pidBytes := []byte(strings.TrimPrefix(handleID, "DOCKER:"))
pid := &dockerPID{}
err = json.Unmarshal(pidBytes, pid)
if err != nil {
return nil, fmt.Errorf("Failed to parse handle '%s': %v", handleID, err)
}
d.logger.Printf("[INFO] driver.docker: re-attaching to docker process: %s", handleID)
// Initialize docker API client
dockerEndpoint := d.config.ReadDefault("docker.endpoint", "unix:///var/run/docker.sock")
client, err := docker.NewClient(dockerEndpoint)
if err != nil {
return nil, fmt.Errorf("Failed to connect to docker.endpoint (%s): %s", dockerEndpoint, err)
}
// Look for a running container with this ID
containers, err := client.ListContainers(docker.ListContainersOptions{
Filters: map[string][]string{
"id": []string{pid.ContainerID},
},
})
if err != nil {
return nil, fmt.Errorf("Failed to query for container %s: %v", pid.ContainerID, err)
}
found := false
for _, container := range containers {
if container.ID == pid.ContainerID {
found = true
}
}
if !found {
return nil, fmt.Errorf("Failed to find container %s: %v", pid.ContainerID, err)
}
// Return a driver handle
h := &dockerHandle{
client: client,
cleanupContainer: cleanupContainer,
cleanupImage: cleanupImage,
logger: d.logger,
imageID: pid.ImageID,
containerID: pid.ContainerID,
doneCh: make(chan struct{}),
waitCh: make(chan error, 1),
}
go h.run()
return h, nil
}
示例6: ListContainers
// ListContainers returns a slice of all containers in the cluster matching the
// given criteria.
func (c *Cluster) ListContainers(opts dcli.ListContainersOptions) ([]docker.APIContainers, error) {
nodes, err := c.scheduler.Nodes()
if err != nil {
return nil, err
}
var wg sync.WaitGroup
result := make(chan []docker.APIContainers, len(nodes))
errs := make(chan error, len(nodes))
for _, n := range nodes {
wg.Add(1)
client, _ := dcli.NewClient(n.Address)
go func(n node) {
defer wg.Done()
if containers, err := n.ListContainers(opts); err != nil {
errs <- err
} else {
result <- containers
}
}(node{id: n.ID, Client: client})
}
wg.Wait()
var group []docker.APIContainers
for {
select {
case containers := <-result:
group = append(group, containers...)
case err = <-errs:
default:
return group, err
}
}
}
示例7: TestProvisionRemoveOldUnitForward
func (s *S) TestProvisionRemoveOldUnitForward(c *gocheck.C) {
err := newImage("tsuru/python", s.server.URL())
c.Assert(err, gocheck.IsNil)
container, err := s.newContainer(nil)
c.Assert(err, gocheck.IsNil)
defer rtesting.FakeRouter.RemoveBackend(container.AppName)
client, err := dockerClient.NewClient(s.server.URL())
c.Assert(err, gocheck.IsNil)
err = client.StartContainer(container.ID, nil)
c.Assert(err, gocheck.IsNil)
app := testing.NewFakeApp(container.AppName, "python", 0)
unit := provision.Unit{
Name: container.ID,
AppName: app.GetName(),
Type: app.GetPlatform(),
Ip: container.HostAddr,
Status: provision.StatusBuilding,
}
context := action.FWContext{Params: []interface{}{app, "", *container}, Previous: unit}
result, err := provisionRemoveOldUnit.Forward(context)
c.Assert(err, gocheck.IsNil)
retUnit := result.(provision.Unit)
c.Assert(retUnit, gocheck.DeepEquals, unit)
_, err = getContainer(container.ID)
c.Assert(err, gocheck.NotNil)
}
示例8: createClient
func createClient() {
var (
dockerEndpoint string
err error
)
if dockerEndpoint = os.Getenv("DOCKER_HOST"); len(dockerEndpoint) == 0 {
dockerEndpoint = "unix:///var/run/docker.sock"
}
if debug {
log.Println("Connecting to Docker daemon via:", dockerEndpoint)
}
client, err = docker.NewClient(dockerEndpoint)
if err != nil {
log.Fatal(err)
}
dockerVersion, err = client.Version()
if err != nil {
log.Fatal("Could not connect to Docker daemon: ", err)
}
if debug {
showDockerVersion()
}
}
示例9: NewServiceRuntime
func NewServiceRuntime(configStore *config.Store, dns, hostIP string) *ServiceRuntime {
var err error
var client *docker.Client
dockerZero, err := dockerBridgeIp()
if err != nil {
log.Fatalf("ERROR: Unable to find docker0 bridge: %s", err)
}
endpoint := GetEndpoint()
if certPath := os.Getenv("DOCKER_CERT_PATH"); certPath != "" {
cert := certPath + "/cert.pem"
key := certPath + "/key.pem"
ca := certPath + "/ca.pem"
client, err = docker.NewTLSClient(endpoint, cert, key, ca)
} else {
client, err = docker.NewClient(endpoint)
}
if err != nil {
log.Fatalf("ERROR: Unable to initialize docker client: %s: %s", err, endpoint)
}
client.HTTPClient.Timeout = 60 * time.Second
return &ServiceRuntime{
dns: dns,
configStore: configStore,
hostIP: hostIP,
dockerIP: dockerZero,
dockerClient: client,
}
}
示例10: Run
// Run runs a slug process
func Run(slugUrl string, cmd ...string) (*dcli.Container, error) {
docker, err := dcli.NewClient(getRandomServer())
if err != nil {
return nil, err
}
port := strconv.Itoa(getRandomPort())
opts := dcli.CreateContainerOptions{
"",
&dcli.Config{
Cmd: cmd,
Env: []string{"PORT=" + port, "SLUG_URL=" + slugUrl},
PortSpecs: []string{port + "/tcp"},
Image: "flynn/slugrunner",
Tty: true,
},
}
container, err := docker.CreateContainer(opts)
if err != nil {
return nil, err
}
err = docker.StartContainer(container.ID, &dcli.HostConfig{})
if err != nil {
return nil, err
}
return docker.InspectContainer(container.ID)
}
示例11: TestRemoveUnitInSameHostAsAnotherUnitShouldEnqueueAnotherBind
func (s *S) TestRemoveUnitInSameHostAsAnotherUnitShouldEnqueueAnotherBind(c *gocheck.C) {
err := newImage("tsuru/python", s.server.URL())
c.Assert(err, gocheck.IsNil)
c1, err := s.newContainer(nil)
c.Assert(err, gocheck.IsNil)
c2, err := s.newContainer(nil)
c.Assert(err, gocheck.IsNil)
defer rtesting.FakeRouter.RemoveBackend(c1.AppName)
client, err := dockerClient.NewClient(s.server.URL())
c.Assert(err, gocheck.IsNil)
err = client.StartContainer(c1.ID, nil)
c.Assert(err, gocheck.IsNil)
a := testing.NewFakeApp(c1.AppName, "python", 0)
var p dockerProvisioner
err = p.RemoveUnit(a, c1.ID)
c.Assert(err, gocheck.IsNil)
_, err = getContainer(c1.ID)
c.Assert(err, gocheck.NotNil)
_, err = getContainer(c2.ID)
c.Assert(err, gocheck.IsNil)
q, err := getQueue()
c.Assert(err, gocheck.IsNil)
message, err := q.Get(1e6)
c.Assert(err, gocheck.IsNil)
expected := &queue.Message{Action: app.BindService, Args: []string{a.GetName(), c2.ID}}
c.Assert(message, gocheck.DeepEquals, expected)
}
示例12: validateDockerInfo
func validateDockerInfo() (string, string) {
client, err := dclient.NewClient(*docker.ArgDockerEndpoint)
if err == nil {
info, err := client.Info()
if err == nil {
execDriver := info.Get("ExecutionDriver")
storageDriver := info.Get("Driver")
desc := fmt.Sprintf("Docker exec driver is %s. Storage driver is %s.\n", execDriver, storageDriver)
if docker.UseSystemd() {
desc += "\tsystemd is being used to create cgroups.\n"
} else {
desc += "\tCgroups are being created through cgroup filesystem.\n"
}
if strings.Contains(execDriver, "native") {
stateFile := docker.DockerStateDir()
if !utils.FileExists(stateFile) {
desc += fmt.Sprintf("\tDocker container state directory %q is not accessible.\n", stateFile)
return Unsupported, desc
}
desc += fmt.Sprintf("\tDocker container state directory is at %q and is accessible.\n", stateFile)
return Recommended, desc
} else if strings.Contains(execDriver, "lxc") {
return Supported, desc
}
return Unknown, desc
}
}
return Unknown, "Docker remote API not reachable\n\t"
}
示例13: TestProvisionerStopSkipAlreadyStoppedContainers
func (s *S) TestProvisionerStopSkipAlreadyStoppedContainers(c *gocheck.C) {
dcli, _ := dockerClient.NewClient(s.server.URL())
app := testing.NewFakeApp("almah", "static", 2)
p := dockerProvisioner{}
container, err := s.newContainer(&newContainerOpts{AppName: app.GetName()})
c.Assert(err, gocheck.IsNil)
defer s.removeTestContainer(container)
err = dcli.StartContainer(container.ID, nil)
c.Assert(err, gocheck.IsNil)
dockerContainer, err := dcli.InspectContainer(container.ID)
c.Assert(err, gocheck.IsNil)
c.Assert(dockerContainer.State.Running, gocheck.Equals, true)
container2, err := s.newContainer(&newContainerOpts{AppName: app.GetName()})
c.Assert(err, gocheck.IsNil)
defer s.removeTestContainer(container2)
err = dcli.StartContainer(container2.ID, nil)
c.Assert(err, gocheck.IsNil)
err = dcli.StopContainer(container2.ID, 1)
c.Assert(err, gocheck.IsNil)
container2.setStatus(provision.StatusStopped.String())
dockerContainer2, err := dcli.InspectContainer(container2.ID)
c.Assert(err, gocheck.IsNil)
c.Assert(dockerContainer2.State.Running, gocheck.Equals, false)
err = p.Stop(app)
c.Assert(err, gocheck.IsNil)
dockerContainer, err = dcli.InspectContainer(container.ID)
c.Assert(err, gocheck.IsNil)
c.Assert(dockerContainer.State.Running, gocheck.Equals, false)
dockerContainer2, err = dcli.InspectContainer(container2.ID)
c.Assert(err, gocheck.IsNil)
c.Assert(dockerContainer2.State.Running, gocheck.Equals, false)
}
示例14: NewClient
func NewClient(endpoint string) (*dockerClient.Client, error) {
client, err := dockerClient.NewClient(endpoint)
if err != nil {
return nil, err
}
retry := false
for i := 0; i < (MAX_WAIT / INTERVAL); i++ {
_, err = client.Info()
if err == nil {
break
}
retry = true
log.Infof("Waiting for Docker at %s", endpoint)
time.Sleep(INTERVAL * time.Millisecond)
}
if err != nil {
return nil, err
}
if retry {
log.Infof("Connected to Docker at %s", endpoint)
}
return client, nil
}
示例15: CreateCassandraAndStart
func CreateCassandraAndStart(name, port string) (*docker.Container, error) {
endpoint := "unix:///var/run/docker.sock"
client, err := docker.NewClient(endpoint)
if err != nil {
return nil, err
}
hostConf := &docker.HostConfig{
PortBindings: map[docker.Port][]docker.PortBinding{
docker.Port("9042/tcp"): []docker.PortBinding{{
HostIP: "0.0.0.0",
HostPort: port,
}},
},
}
/*hostConf := &docker.HostConfig{
PublishAllPorts: true,
}*/
opts := docker.CreateContainerOptions{
Name: name,
Config: &docker.Config{
Image: "spotify/cassandra:latest",
},
HostConfig: hostConf,
}
container, err := client.CreateContainer(opts)
if err != nil {
return nil, err
}
return container, client.StartContainer(container.ID, hostConf)
}