@@ -29,7 +29,6 @@ import (
29
29
"github.com/containerd/containerd/platforms"
30
30
moby "github.com/docker/docker/api/types"
31
31
containerType "github.com/docker/docker/api/types/container"
32
- "github.com/docker/docker/api/types/network"
33
32
specs "github.com/opencontainers/image-spec/specs-go/v1"
34
33
"github.com/pkg/errors"
35
34
"github.com/sirupsen/logrus"
@@ -233,7 +232,13 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
233
232
name := getContainerName (project .Name , service , number )
234
233
i := i
235
234
eg .Go (func () error {
236
- container , err := c .service .createContainer (ctx , project , service , name , number , false , true , false )
235
+ opts := createOptions {
236
+ AutoRemove : false ,
237
+ AttachStdin : false ,
238
+ UseNetworkAliases : true ,
239
+ Labels : mergeLabels (service .Labels , service .CustomLabels ),
240
+ }
241
+ container , err := c .service .createContainer (ctx , project , service , name , number , opts )
237
242
updated [actual + i ] = container
238
243
return err
239
244
})
@@ -399,12 +404,11 @@ func getScale(config types.ServiceConfig) (int, error) {
399
404
}
400
405
401
406
func (s * composeService ) createContainer (ctx context.Context , project * types.Project , service types.ServiceConfig ,
402
- name string , number int , autoRemove bool , useNetworkAliases bool , attachStdin bool ) (container moby.Container , err error ) {
407
+ name string , number int , opts createOptions ) (container moby.Container , err error ) {
403
408
w := progress .ContextWriter (ctx )
404
409
eventName := "Container " + name
405
410
w .Event (progress .CreatingEvent (eventName ))
406
- container , err = s .createMobyContainer (ctx , project , service , name , number , nil ,
407
- autoRemove , useNetworkAliases , attachStdin , w , mergeLabels (service .Labels , service .CustomLabels ))
411
+ container , err = s .createMobyContainer (ctx , project , service , name , number , nil , opts , w )
408
412
if err != nil {
409
413
return
410
414
}
@@ -429,9 +433,13 @@ func (s *composeService) recreateContainer(ctx context.Context, project *types.P
429
433
}
430
434
name := getContainerName (project .Name , service , number )
431
435
tmpName := fmt .Sprintf ("%s_%s" , replaced .ID [:12 ], name )
432
- created , err = s .createMobyContainer (ctx , project , service , tmpName , number , inherited ,
433
- false , true , false , w ,
434
- mergeLabels (service .Labels , service .CustomLabels ).Add (api .ContainerReplaceLabel , replaced .ID ))
436
+ opts := createOptions {
437
+ AutoRemove : false ,
438
+ AttachStdin : false ,
439
+ UseNetworkAliases : true ,
440
+ Labels : mergeLabels (service .Labels , service .CustomLabels ).Add (api .ContainerReplaceLabel , replaced .ID ),
441
+ }
442
+ created , err = s .createMobyContainer (ctx , project , service , tmpName , number , inherited , opts , w )
435
443
if err != nil {
436
444
return created , err
437
445
}
@@ -484,19 +492,18 @@ func (s *composeService) startContainer(ctx context.Context, container moby.Cont
484
492
return nil
485
493
}
486
494
487
- func (s * composeService ) createMobyContainer (ctx context.Context , //nolint:gocyclo
495
+ func (s * composeService ) createMobyContainer (ctx context.Context ,
488
496
project * types.Project ,
489
497
service types.ServiceConfig ,
490
498
name string ,
491
499
number int ,
492
500
inherit * moby.Container ,
493
- autoRemove , useNetworkAliases , attachStdin bool ,
501
+ opts createOptions ,
494
502
w progress.Writer ,
495
- labels types.Labels ,
496
503
) (moby.Container , error ) {
497
504
var created moby.Container
498
- containerConfig , hostConfig , networkingConfig , err := s .getCreateOptions (ctx , project , service , number , inherit ,
499
- autoRemove , attachStdin , labels )
505
+ cfgs , err := s .getCreateConfigs (ctx , project , service , number , inherit , opts )
506
+
500
507
if err != nil {
501
508
return created , err
502
509
}
@@ -514,18 +521,7 @@ func (s *composeService) createMobyContainer(ctx context.Context, //nolint:gocyc
514
521
plat = & p
515
522
}
516
523
517
- links , err := s .getLinks (ctx , project .Name , service , number )
518
- if err != nil {
519
- return created , err
520
- }
521
- if networkingConfig != nil {
522
- for k , s := range networkingConfig .EndpointsConfig {
523
- s .Links = links
524
- networkingConfig .EndpointsConfig [k ] = s
525
- }
526
- }
527
-
528
- response , err := s .apiClient ().ContainerCreate (ctx , containerConfig , hostConfig , networkingConfig , plat , name )
524
+ response , err := s .apiClient ().ContainerCreate (ctx , cfgs .Container , cfgs .Host , cfgs .Network , plat , name )
529
525
if err != nil {
530
526
return created , err
531
527
}
@@ -548,29 +544,19 @@ func (s *composeService) createMobyContainer(ctx context.Context, //nolint:gocyc
548
544
Networks : inspectedContainer .NetworkSettings .Networks ,
549
545
},
550
546
}
551
- for _ , netName := range service .NetworksByPriority () {
552
- netwrk := project .Networks [netName ]
553
- cfg := service .Networks [netName ]
554
- aliases := []string {getContainerName (project .Name , service , number )}
555
- if useNetworkAliases {
556
- aliases = append (aliases , service .Name )
557
- if cfg != nil {
558
- aliases = append (aliases , cfg .Aliases ... )
559
- }
560
- }
561
- if val , ok := created .NetworkSettings .Networks [netwrk .Name ]; ok {
562
- if shortIDAliasExists (created .ID , val .Aliases ... ) {
563
- continue
564
- }
565
- err = s .apiClient ().NetworkDisconnect (ctx , netwrk .Name , created .ID , false )
566
- if err != nil {
547
+
548
+ // the highest-priority network is the primary and is included in the ContainerCreate API
549
+ // call via container.NetworkMode & network.NetworkingConfig
550
+ // any remaining networks are connected one-by-one here after creation (but before start)
551
+ serviceNetworks := service .NetworksByPriority ()
552
+ if len (serviceNetworks ) > 1 {
553
+ for _ , networkKey := range serviceNetworks [1 :] {
554
+ mobyNetworkName := project .Networks [networkKey ].Name
555
+ epSettings := createEndpointSettings (project , service , number , networkKey , cfgs .Links , opts .UseNetworkAliases )
556
+ if err := s .apiClient ().NetworkConnect (ctx , mobyNetworkName , created .ID , epSettings ); err != nil {
567
557
return created , err
568
558
}
569
559
}
570
- err = s .connectContainerToNetwork (ctx , created .ID , netwrk .Name , cfg , links , aliases ... )
571
- if err != nil {
572
- return created , err
573
- }
574
560
}
575
561
576
562
err = s .injectSecrets (ctx , project , service , created .ID )
@@ -635,43 +621,6 @@ func (s *composeService) getLinks(ctx context.Context, projectName string, servi
635
621
return links , nil
636
622
}
637
623
638
- func shortIDAliasExists (containerID string , aliases ... string ) bool {
639
- for _ , alias := range aliases {
640
- if alias == containerID [:12 ] {
641
- return true
642
- }
643
- }
644
- return false
645
- }
646
-
647
- func (s * composeService ) connectContainerToNetwork (ctx context.Context , id string , netwrk string , cfg * types.ServiceNetworkConfig , links []string , aliases ... string ) error {
648
- var (
649
- ipv4Address string
650
- ipv6Address string
651
- ipam * network.EndpointIPAMConfig
652
- )
653
- if cfg != nil {
654
- ipv4Address = cfg .Ipv4Address
655
- ipv6Address = cfg .Ipv6Address
656
- ipam = & network.EndpointIPAMConfig {
657
- IPv4Address : ipv4Address ,
658
- IPv6Address : ipv6Address ,
659
- LinkLocalIPs : cfg .LinkLocalIPs ,
660
- }
661
- }
662
- err := s .apiClient ().NetworkConnect (ctx , netwrk , id , & network.EndpointSettings {
663
- Aliases : aliases ,
664
- IPAddress : ipv4Address ,
665
- GlobalIPv6Address : ipv6Address ,
666
- Links : links ,
667
- IPAMConfig : ipam ,
668
- })
669
- if err != nil {
670
- return err
671
- }
672
- return nil
673
- }
674
-
675
624
func (s * composeService ) isServiceHealthy (ctx context.Context , containers Containers , fallbackRunning bool ) (bool , error ) {
676
625
for _ , c := range containers {
677
626
container , err := s .apiClient ().ContainerInspect (ctx , c .ID )
0 commit comments