@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "fmt"
2222 "strconv"
23+ "strings"
2324 "time"
2425
2526 "github.com/compose-spec/compose-go/types"
@@ -314,11 +315,23 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
314315 if err != nil {
315316 return err
316317 }
318+ inspectedContainer , err := s .apiClient .ContainerInspect (ctx , created .ID )
319+ if err != nil {
320+ return err
321+ }
317322 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+ },
320329 }
321330 cState .Add (createdContainer )
331+ links , err := s .getLinks (ctx , service )
332+ if err != nil {
333+ return err
334+ }
322335 for _ , netName := range service .NetworksByPriority () {
323336 netwrk := project .Networks [netName ]
324337 cfg := service .Networks [netName ]
@@ -329,16 +342,33 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
329342 aliases = append (aliases , cfg .Aliases ... )
330343 }
331344 }
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 ... )
334355 if err != nil {
335356 return err
336357 }
337358 }
338359 return nil
339360}
340361
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 {
342372 var (
343373 ipv4ddress string
344374 ipv6Address string
@@ -351,13 +381,45 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
351381 Aliases : aliases ,
352382 IPAddress : ipv4ddress ,
353383 GlobalIPv6Address : ipv6Address ,
384+ Links : links ,
354385 })
355386 if err != nil {
356387 return err
357388 }
358389 return nil
359390}
360391
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+
361423func (s * composeService ) isServiceHealthy (ctx context.Context , project * types.Project , service string ) (bool , error ) {
362424 containers , err := s .getContainers (ctx , project .Name , oneOffExclude , false , service )
363425 if err != nil {
0 commit comments