@@ -432,13 +432,15 @@ func (u *ContainerService) ContainerUpdate(req dto.ContainerOperate) error {
432
432
hostConf := oldContainer .HostConfig
433
433
var networkConf network.NetworkingConfig
434
434
if err := loadConfigInfo (req , config , hostConf , & networkConf ); err != nil {
435
+ reCreateAfterUpdate (req .Name , client , oldContainer .Config , oldContainer .HostConfig , oldContainer .NetworkSettings )
435
436
return err
436
437
}
437
438
438
439
global .LOG .Infof ("new container info %s has been update, now start to recreate" , req .Name )
439
440
container , err := client .ContainerCreate (ctx , config , hostConf , & networkConf , & v1.Platform {}, req .Name )
440
441
if err != nil {
441
- return fmt .Errorf ("recreate contianer failed, err: %v" , err )
442
+ reCreateAfterUpdate (req .Name , client , oldContainer .Config , oldContainer .HostConfig , oldContainer .NetworkSettings )
443
+ return fmt .Errorf ("update contianer failed, err: %v" , err )
442
444
}
443
445
global .LOG .Infof ("update container %s successful! now check if the container is started." , req .Name )
444
446
if err := client .ContainerStart (ctx , container .ID , types.ContainerStartOptions {}); err != nil {
@@ -481,13 +483,14 @@ func (u *ContainerService) ContainerUpgrade(req dto.ContainerUpgrade) error {
481
483
}
482
484
483
485
global .LOG .Infof ("new container info %s has been update, now start to recreate" , req .Name )
484
- container , err := client .ContainerCreate (ctx , config , hostConf , & network. NetworkingConfig {} , & v1.Platform {}, req .Name )
486
+ container , err := client .ContainerCreate (ctx , config , hostConf , & networkConf , & v1.Platform {}, req .Name )
485
487
if err != nil {
486
- return fmt .Errorf ("recreate contianer failed, err: %v" , err )
488
+ reCreateAfterUpdate (req .Name , client , oldContainer .Config , oldContainer .HostConfig , oldContainer .NetworkSettings )
489
+ return fmt .Errorf ("upgrade contianer failed, err: %v" , err )
487
490
}
488
- global .LOG .Infof ("update container %s successful! now check if the container is started." , req .Name )
491
+ global .LOG .Infof ("upgrade container %s successful! now check if the container is started." , req .Name )
489
492
if err := client .ContainerStart (ctx , container .ID , types.ContainerStartOptions {}); err != nil {
490
- return fmt .Errorf ("update successful but start failed, err: %v" , err )
493
+ return fmt .Errorf ("upgrade successful but start failed, err: %v" , err )
491
494
}
492
495
493
496
return nil
@@ -781,6 +784,12 @@ func checkPortStats(ports []dto.PortHelper) (nat.PortMap, error) {
781
784
}
782
785
783
786
func loadConfigInfo (req dto.ContainerOperate , config * container.Config , hostConf * container.HostConfig , networkConf * network.NetworkingConfig ) error {
787
+ if len (req .Network ) != 0 {
788
+ networkConf .EndpointsConfig = map [string ]* network.EndpointSettings {req .Network : {}}
789
+ } else {
790
+ networkConf = & network.NetworkingConfig {}
791
+ }
792
+
784
793
portMap , err := checkPortStats (req .ExposedPorts )
785
794
if err != nil {
786
795
return err
@@ -795,8 +804,6 @@ func loadConfigInfo(req dto.ContainerOperate, config *container.Config, hostConf
795
804
config .Labels = stringsToMap (req .Labels )
796
805
config .ExposedPorts = exposeds
797
806
798
- networkConf .EndpointsConfig = map [string ]* network.EndpointSettings {req .Network : {}}
799
-
800
807
hostConf .AutoRemove = req .AutoRemove
801
808
hostConf .CPUShares = req .CPUShares
802
809
hostConf .PublishAllPorts = req .PublishAllPorts
@@ -815,3 +822,24 @@ func loadConfigInfo(req dto.ContainerOperate, config *container.Config, hostConf
815
822
}
816
823
return nil
817
824
}
825
+
826
+ func reCreateAfterUpdate (name string , client * client.Client , config * container.Config , hostConf * container.HostConfig , networkConf * types.NetworkSettings ) {
827
+ ctx := context .Background ()
828
+
829
+ var oldNetworkConf network.NetworkingConfig
830
+ if networkConf != nil {
831
+ for networkKey := range networkConf .Networks {
832
+ oldNetworkConf .EndpointsConfig = map [string ]* network.EndpointSettings {networkKey : {}}
833
+ break
834
+ }
835
+ }
836
+
837
+ oldContainer , err := client .ContainerCreate (ctx , config , hostConf , & oldNetworkConf , & v1.Platform {}, name )
838
+ if err != nil {
839
+ global .LOG .Errorf ("recreate after container update failed, err: %v" , err )
840
+ return
841
+ }
842
+ if err := client .ContainerStart (ctx , oldContainer .ID , types.ContainerStartOptions {}); err != nil {
843
+ global .LOG .Errorf ("restart after container update failed, err: %v" , err )
844
+ }
845
+ }
0 commit comments