@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "fmt"
2222 "strconv"
23+ "strings"
2324 "time"
2425
2526 "github.com/compose-spec/compose-go/types"
@@ -317,8 +318,14 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
317318 createdContainer := moby.Container {
318319 ID : created .ID ,
319320 Labels : containerConfig .Labels ,
321+ Names : []string {"/" + name },
320322 }
321323 cState .Add (createdContainer )
324+
325+ links , err := s .getLinks (ctx , service )
326+ if err != nil {
327+ return err
328+ }
322329 for _ , netName := range service .NetworksByPriority () {
323330 netwrk := project .Networks [netName ]
324331 cfg := service .Networks [netName ]
@@ -330,15 +337,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
330337 }
331338 }
332339
333- err = s .connectContainerToNetwork (ctx , created .ID , netwrk .Name , cfg , aliases ... )
340+ err = s .connectContainerToNetwork (ctx , created .ID , netwrk .Name , cfg , links , aliases ... )
334341 if err != nil {
335342 return err
336343 }
337344 }
338345 return nil
339346}
340347
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 {
342349 var (
343350 ipv4ddress string
344351 ipv6Address string
@@ -347,17 +354,54 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
347354 ipv4ddress = cfg .Ipv4Address
348355 ipv6Address = cfg .Ipv6Address
349356 }
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 {
351363 Aliases : aliases ,
352364 IPAddress : ipv4ddress ,
353365 GlobalIPv6Address : ipv6Address ,
366+ Links : links ,
354367 })
355368 if err != nil {
356369 return err
357370 }
358371 return nil
359372}
360373
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+
361405func (s * composeService ) isServiceHealthy (ctx context.Context , project * types.Project , service string ) (bool , error ) {
362406 containers , err := s .getContainers (ctx , project .Name , oneOffExclude , false , service )
363407 if err != nil {
0 commit comments