@@ -315,13 +315,19 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
315
315
if err != nil {
316
316
return err
317
317
}
318
+ inspectedContainer , err := s .apiClient .ContainerInspect (ctx , created .ID )
319
+ if err != nil {
320
+ return err
321
+ }
318
322
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
+ },
322
329
}
323
330
cState .Add (createdContainer )
324
-
325
331
links , err := s .getLinks (ctx , service )
326
332
if err != nil {
327
333
return err
@@ -336,7 +342,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
336
342
aliases = append (aliases , cfg .Aliases ... )
337
343
}
338
344
}
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
+ }
340
354
err = s .connectContainerToNetwork (ctx , created .ID , netwrk .Name , cfg , links , aliases ... )
341
355
if err != nil {
342
356
return err
@@ -345,6 +359,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
345
359
return nil
346
360
}
347
361
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
+
348
371
func (s * composeService ) connectContainerToNetwork (ctx context.Context , id string , netwrk string , cfg * types.ServiceNetworkConfig , links []string , aliases ... string ) error {
349
372
var (
350
373
ipv4ddress string
@@ -354,12 +377,7 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
354
377
ipv4ddress = cfg .Ipv4Address
355
378
ipv6Address = cfg .Ipv6Address
356
379
}
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 {
363
381
Aliases : aliases ,
364
382
IPAddress : ipv4ddress ,
365
383
GlobalIPv6Address : ipv6Address ,
0 commit comments