only disconnect active networks in a container

Signed-off-by: Lorena Rangel <lorena.rangel@docker.com>
This commit is contained in:
Lorena Rangel 2021-06-15 22:56:24 +02:00
parent b38d35ae1c
commit b5d3eda223
1 changed files with 29 additions and 11 deletions

View File

@ -315,13 +315,19 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
if err != nil { if err != nil {
return err return err
} }
inspectedContainer, err := s.apiClient.ContainerInspect(ctx, created.ID)
if err != nil {
return err
}
createdContainer := moby.Container{ createdContainer := moby.Container{
ID: created.ID, ID: inspectedContainer.ID,
Labels: containerConfig.Labels, Labels: inspectedContainer.Config.Labels,
Names: []string{"/" + name}, Names: []string{inspectedContainer.Name},
NetworkSettings: &moby.SummaryNetworkSettings{
Networks: inspectedContainer.NetworkSettings.Networks,
},
} }
cState.Add(createdContainer) cState.Add(createdContainer)
links, err := s.getLinks(ctx, service) links, err := s.getLinks(ctx, service)
if err != nil { if err != nil {
return err return err
@ -336,7 +342,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
aliases = append(aliases, cfg.Aliases...) aliases = append(aliases, cfg.Aliases...)
} }
} }
if val, ok := createdContainer.NetworkSettings.Networks[netwrk.Name]; ok {
if shortIDAliasExists(createdContainer.ID, val.Aliases...) {
continue
}
err := s.apiClient.NetworkDisconnect(ctx, netwrk.Name, createdContainer.ID, false)
if err != nil {
return err
}
}
err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, links, aliases...) err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, links, aliases...)
if err != nil { if err != nil {
return err return err
@ -345,6 +359,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
return nil return nil
} }
func shortIDAliasExists(containerID string, aliases ...string) bool {
for _, alias := range aliases {
if alias == containerID[:12] {
return true
}
}
return false
}
func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, links []string, aliases ...string) error { func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, links []string, aliases ...string) error {
var ( var (
ipv4ddress string ipv4ddress string
@ -354,12 +377,7 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
ipv4ddress = cfg.Ipv4Address ipv4ddress = cfg.Ipv4Address
ipv6Address = cfg.Ipv6Address ipv6Address = cfg.Ipv6Address
} }
err := s.apiClient.NetworkDisconnect(ctx, netwrk, id, false) err := s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
if err != nil {
return err
}
err = s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
Aliases: aliases, Aliases: aliases,
IPAddress: ipv4ddress, IPAddress: ipv4ddress,
GlobalIPv6Address: ipv6Address, GlobalIPv6Address: ipv6Address,