Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit b5d3eda

Browse files
committed
only disconnect active networks in a container
Signed-off-by: Lorena Rangel <[email protected]>
1 parent b38d35a commit b5d3eda

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

pkg/compose/convergence.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,19 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
315315
if err != nil {
316316
return err
317317
}
318+
inspectedContainer, err := s.apiClient.ContainerInspect(ctx, created.ID)
319+
if err != nil {
320+
return err
321+
}
318322
createdContainer := moby.Container{
319-
ID: created.ID,
320-
Labels: containerConfig.Labels,
321-
Names: []string{"/" + name},
323+
ID: inspectedContainer.ID,
324+
Labels: inspectedContainer.Config.Labels,
325+
Names: []string{inspectedContainer.Name},
326+
NetworkSettings: &moby.SummaryNetworkSettings{
327+
Networks: inspectedContainer.NetworkSettings.Networks,
328+
},
322329
}
323330
cState.Add(createdContainer)
324-
325331
links, err := s.getLinks(ctx, service)
326332
if err != nil {
327333
return err
@@ -336,7 +342,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
336342
aliases = append(aliases, cfg.Aliases...)
337343
}
338344
}
339-
345+
if val, ok := createdContainer.NetworkSettings.Networks[netwrk.Name]; ok {
346+
if shortIDAliasExists(createdContainer.ID, val.Aliases...) {
347+
continue
348+
}
349+
err := s.apiClient.NetworkDisconnect(ctx, netwrk.Name, createdContainer.ID, false)
350+
if err != nil {
351+
return err
352+
}
353+
}
340354
err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, links, aliases...)
341355
if err != nil {
342356
return err
@@ -345,6 +359,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
345359
return nil
346360
}
347361

362+
func shortIDAliasExists(containerID string, aliases ...string) bool {
363+
for _, alias := range aliases {
364+
if alias == containerID[:12] {
365+
return true
366+
}
367+
}
368+
return false
369+
}
370+
348371
func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, links []string, aliases ...string) error {
349372
var (
350373
ipv4ddress string
@@ -354,12 +377,7 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
354377
ipv4ddress = cfg.Ipv4Address
355378
ipv6Address = cfg.Ipv6Address
356379
}
357-
err := s.apiClient.NetworkDisconnect(ctx, netwrk, id, false)
358-
if err != nil {
359-
return err
360-
}
361-
362-
err = s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
380+
err := s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
363381
Aliases: aliases,
364382
IPAddress: ipv4ddress,
365383
GlobalIPv6Address: ipv6Address,

0 commit comments

Comments
 (0)