@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"strconv"
23
+ "strings"
23
24
"time"
24
25
25
26
"github.com/compose-spec/compose-go/types"
@@ -314,11 +315,23 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
314
315
if err != nil {
315
316
return err
316
317
}
318
+ inspectedContainer , err := s .apiClient .ContainerInspect (ctx , created .ID )
319
+ if err != nil {
320
+ return err
321
+ }
317
322
createdContainer := moby.Container {
318
- ID : created .ID ,
319
- Labels : containerConfig .Labels ,
323
+ ID : inspectedContainer .ID ,
324
+ Labels : inspectedContainer .Config .Labels ,
325
+ Names : []string {inspectedContainer .Name },
326
+ NetworkSettings : & moby.SummaryNetworkSettings {
327
+ Networks : inspectedContainer .NetworkSettings .Networks ,
328
+ },
320
329
}
321
330
cState .Add (createdContainer )
331
+ links , err := s .getLinks (ctx , service )
332
+ if err != nil {
333
+ return err
334
+ }
322
335
for _ , netName := range service .NetworksByPriority () {
323
336
netwrk := project .Networks [netName ]
324
337
cfg := service .Networks [netName ]
@@ -329,16 +342,33 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
329
342
aliases = append (aliases , cfg .Aliases ... )
330
343
}
331
344
}
332
-
333
- err = s .connectContainerToNetwork (ctx , created .ID , netwrk .Name , cfg , aliases ... )
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
+ }
354
+ err = s .connectContainerToNetwork (ctx , created .ID , netwrk .Name , cfg , links , aliases ... )
334
355
if err != nil {
335
356
return err
336
357
}
337
358
}
338
359
return nil
339
360
}
340
361
341
- func (s * composeService ) connectContainerToNetwork (ctx context.Context , id string , netwrk string , cfg * types.ServiceNetworkConfig , aliases ... string ) error {
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
+
371
+ func (s * composeService ) connectContainerToNetwork (ctx context.Context , id string , netwrk string , cfg * types.ServiceNetworkConfig , links []string , aliases ... string ) error {
342
372
var (
343
373
ipv4ddress string
344
374
ipv6Address string
@@ -351,13 +381,45 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
351
381
Aliases : aliases ,
352
382
IPAddress : ipv4ddress ,
353
383
GlobalIPv6Address : ipv6Address ,
384
+ Links : links ,
354
385
})
355
386
if err != nil {
356
387
return err
357
388
}
358
389
return nil
359
390
}
360
391
392
+ func (s * composeService ) getLinks (ctx context.Context , service types.ServiceConfig ) ([]string , error ) {
393
+ cState , err := GetContextContainerState (ctx )
394
+ if err != nil {
395
+ return nil , err
396
+ }
397
+ links := []string {}
398
+ for _ , serviceLink := range service .Links {
399
+ s := strings .Split (serviceLink , ":" )
400
+ serviceName := serviceLink
401
+ serviceAlias := ""
402
+ if len (s ) == 2 {
403
+ serviceName = s [0 ]
404
+ serviceAlias = s [1 ]
405
+ }
406
+ containers := cState .GetContainers ()
407
+ depServiceContainers := containers .filter (isService (serviceName ))
408
+ for _ , container := range depServiceContainers {
409
+ name := getCanonicalContainerName (container )
410
+ if serviceAlias != "" {
411
+ links = append (links ,
412
+ fmt .Sprintf ("%s:%s" , name , serviceAlias ))
413
+ }
414
+ links = append (links ,
415
+ fmt .Sprintf ("%s:%s" , name , name ),
416
+ fmt .Sprintf ("%s:%s" , name , getContainerNameWithoutProject (container )))
417
+ }
418
+ }
419
+ links = append (links , service .ExternalLinks ... )
420
+ return links , nil
421
+ }
422
+
361
423
func (s * composeService ) isServiceHealthy (ctx context.Context , project * types.Project , service string ) (bool , error ) {
362
424
containers , err := s .getContainers (ctx , project .Name , oneOffExclude , false , service )
363
425
if err != nil {
0 commit comments