@@ -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"
@@ -317,8 +318,14 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
317
318
createdContainer := moby.Container {
318
319
ID : created .ID ,
319
320
Labels : containerConfig .Labels ,
321
+ Names : []string {"/" + name },
320
322
}
321
323
cState .Add (createdContainer )
324
+
325
+ links , err := s .getLinks (ctx , service )
326
+ if err != nil {
327
+ return err
328
+ }
322
329
for _ , netName := range service .NetworksByPriority () {
323
330
netwrk := project .Networks [netName ]
324
331
cfg := service .Networks [netName ]
@@ -330,15 +337,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
330
337
}
331
338
}
332
339
333
- err = s .connectContainerToNetwork (ctx , created .ID , netwrk .Name , cfg , aliases ... )
340
+ err = s .connectContainerToNetwork (ctx , created .ID , netwrk .Name , cfg , links , aliases ... )
334
341
if err != nil {
335
342
return err
336
343
}
337
344
}
338
345
return nil
339
346
}
340
347
341
- func (s * composeService ) connectContainerToNetwork (ctx context.Context , id string , netwrk string , cfg * types.ServiceNetworkConfig , aliases ... string ) error {
348
+ func (s * composeService ) connectContainerToNetwork (ctx context.Context , id string , netwrk string , cfg * types.ServiceNetworkConfig , links [] string , aliases ... string ) error {
342
349
var (
343
350
ipv4ddress string
344
351
ipv6Address string
@@ -347,17 +354,54 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
347
354
ipv4ddress = cfg .Ipv4Address
348
355
ipv6Address = cfg .Ipv6Address
349
356
}
350
- err := s .apiClient .NetworkConnect (ctx , netwrk , id , & network.EndpointSettings {
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 {
351
363
Aliases : aliases ,
352
364
IPAddress : ipv4ddress ,
353
365
GlobalIPv6Address : ipv6Address ,
366
+ Links : links ,
354
367
})
355
368
if err != nil {
356
369
return err
357
370
}
358
371
return nil
359
372
}
360
373
374
+ func (s * composeService ) getLinks (ctx context.Context , service types.ServiceConfig ) ([]string , error ) {
375
+ cState , err := GetContextContainerState (ctx )
376
+ if err != nil {
377
+ return nil , err
378
+ }
379
+ links := []string {}
380
+ for _ , serviceLink := range service .Links {
381
+ s := strings .Split (serviceLink , ":" )
382
+ serviceName := serviceLink
383
+ serviceAlias := ""
384
+ if len (s ) == 2 {
385
+ serviceName = s [0 ]
386
+ serviceAlias = s [1 ]
387
+ }
388
+ containers := cState .GetContainers ()
389
+ depServiceContainers := containers .filter (isService (serviceName ))
390
+ for _ , container := range depServiceContainers {
391
+ name := getCanonicalContainerName (container )
392
+ if serviceAlias != "" {
393
+ links = append (links ,
394
+ fmt .Sprintf ("%s:%s" , name , serviceAlias ))
395
+ }
396
+ links = append (links ,
397
+ fmt .Sprintf ("%s:%s" , name , name ),
398
+ fmt .Sprintf ("%s:%s" , name , getContainerNameWithoutProject (container )))
399
+ }
400
+ }
401
+ links = append (links , service .ExternalLinks ... )
402
+ return links , nil
403
+ }
404
+
361
405
func (s * composeService ) isServiceHealthy (ctx context.Context , project * types.Project , service string ) (bool , error ) {
362
406
containers , err := s .getContainers (ctx , project .Name , oneOffExclude , false , service )
363
407
if err != nil {
0 commit comments