@@ -614,7 +614,7 @@ func (c *Container) ociEnv() []string {
614
614
return envs
615
615
}
616
616
617
- func (c * Container ) ociSpec (cjson * dockertypes.ContainerJSON , cmds []string ) * specs.Spec {
617
+ func (c * Container ) ociSpec (cjson * dockertypes.ContainerJSON , cmds []string , user string ) * specs.Spec {
618
618
var ocispec specs.Spec
619
619
620
620
ocispec = oci .DefaultSpec ()
@@ -628,12 +628,7 @@ func (c *Container) ociSpec(cjson *dockertypes.ContainerJSON, cmds []string) *sp
628
628
629
629
ocispec .Hostname = c .p .globalSpec .Hostname
630
630
631
- /*
632
- * ocispec used the user's UID and GID instead of user name and group name,
633
- * thus it needed to convert the user name and group name to UID and GID in
634
- * the future, here just set it to "0" as default.
635
- */
636
- ocispec .Process .User = specs.User {UID : 0 , GID : 0 }
631
+ ocispec .Process .User = specs.User {Username : user }
637
632
638
633
for _ , l := range c .spec .Ulimits {
639
634
ltype := strings .ToLower (l .Name )
@@ -648,7 +643,8 @@ func (c *Container) ociSpec(cjson *dockertypes.ContainerJSON, cmds []string) *sp
648
643
}
649
644
650
645
func (c * Container ) containerConfig (cjson * dockertypes.ContainerJSON ) (* vc.ContainerConfig , error ) {
651
- var user , group string
646
+ var user = "0"
647
+ var group = "0"
652
648
var ociSpec * specs.Spec
653
649
var cmds []string
654
650
@@ -665,10 +661,30 @@ func (c *Container) containerConfig(cjson *dockertypes.ContainerJSON) (*vc.Conta
665
661
c .spec .StopSignal = "TERM"
666
662
}
667
663
664
+ if c .spec .User != nil {
665
+ if c .spec .User .Name != "" {
666
+ user = c .spec .User .Name
667
+ }
668
+ if c .spec .User .Group != "" {
669
+ group = c .spec .User .Group
670
+ }
671
+ } else if cjson .Config .User != "" {
672
+ users := strings .Split (cjson .Config .User , ":" )
673
+ if len (users ) > 2 {
674
+ return nil , fmt .Errorf ("container %s invalid user group config: %s" , cjson .Name , cjson .Config .User )
675
+ }
676
+ if len (users ) == 2 {
677
+ user = users [0 ]
678
+ group = users [1 ]
679
+ } else {
680
+ user = cjson .Config .User
681
+ }
682
+ }
683
+
668
684
cmds = append (cmds , cjson .Config .Entrypoint .Slice ()... )
669
685
cmds = append (cmds , cjson .Config .Cmd .Slice ()... )
670
686
671
- ociSpec = c .ociSpec (cjson , cmds )
687
+ ociSpec = c .ociSpec (cjson , cmds , user )
672
688
673
689
//remove those namespace types from ocispec
674
690
for _ , ns := range []specs.LinuxNamespaceType {
@@ -706,11 +722,6 @@ func (c *Container) containerConfig(cjson *dockertypes.ContainerJSON) (*vc.Conta
706
722
}
707
723
c .Log (DEBUG , "mount id: %s" , mountId )
708
724
709
- if c .spec .User != nil {
710
- user = c .spec .User .Name
711
- group = c .spec .User .Group
712
- }
713
-
714
725
cmd := vc.Cmd {
715
726
Args : cmds ,
716
727
Envs : c .cmdEnvs ([]vc.EnvVar {}),
0 commit comments