Change networks opts into a slice when creating a container

This commit is contained in:
Marcos Lilljedahl
2017-08-25 14:56:57 -03:00
parent a8567d8670
commit e9f5de5677
3 changed files with 15 additions and 12 deletions

View File

@@ -219,7 +219,7 @@ type CreateContainerOpts struct {
Privileged bool Privileged bool
HostFQDN string HostFQDN string
Labels map[string]string Labels map[string]string
Networks map[string]string Networks []string
} }
func (d *docker) CreateContainer(opts CreateContainerOpts) error { func (d *docker) CreateContainer(opts CreateContainerOpts) error {
@@ -286,17 +286,10 @@ func (d *docker) CreateContainer(opts CreateContainerOpts) error {
Labels: opts.Labels, Labels: opts.Labels,
} }
networkConf := &network.NetworkingConfig{} networkConf := &network.NetworkingConfig{
ec := map[string]*network.EndpointSettings{} EndpointsConfig: map[string]*network.EndpointSettings{opts.Networks[0]: &network.EndpointSettings{}},
for netId, hostname := range opts.Networks {
es := &network.EndpointSettings{}
if hostname != "" {
es.Aliases = []string{hostname}
}
ec[netId] = es
} }
networkConf.EndpointsConfig = ec
container, err := d.c.ContainerCreate(context.Background(), cf, h, networkConf, opts.ContainerName) container, err := d.c.ContainerCreate(context.Background(), cf, h, networkConf, opts.ContainerName)
if err != nil { if err != nil {
@@ -314,6 +307,16 @@ func (d *docker) CreateContainer(opts CreateContainerOpts) error {
} }
} }
//connect remaining networks if there are any
if len(opts.Networks) > 1 {
for _, nid := range opts.Networks {
err := d.c.NetworkConnect(context.Background(), nid, container.ID, &network.EndpointSettings{})
if err != nil {
return "", err
}
}
}
if err := d.copyIfSet(opts.ServerCert, "cert.pem", containerCertDir, opts.ContainerName); err != nil { if err := d.copyIfSet(opts.ServerCert, "cert.pem", containerCertDir, opts.ContainerName); err != nil {
return err return err
} }

View File

@@ -64,7 +64,7 @@ func (d *DinD) InstanceNew(session *types.Session, conf types.InstanceConfig) (*
CACert: conf.CACert, CACert: conf.CACert,
HostFQDN: conf.Host, HostFQDN: conf.Host,
Privileged: true, Privileged: true,
Networks: map[string]string{session.Id: conf.Hostname}, Networks: []string{session.Id},
} }
dockerClient, err := d.factory.GetForSession(session.Id) dockerClient, err := d.factory.GetForSession(session.Id)

View File

@@ -82,7 +82,7 @@ func (d *windows) InstanceNew(session *types.Session, conf types.InstanceConfig)
CACert: conf.CACert, CACert: conf.CACert,
Privileged: false, Privileged: false,
HostFQDN: conf.Host, HostFQDN: conf.Host,
Networks: map[string]string{session.Id: conf.Hostname}, Networks: []string{session.Id},
} }
dockerClient, err := d.factory.GetForSession(session.Id) dockerClient, err := d.factory.GetForSession(session.Id)