From 8709f1b5bb5c73a2bc286fab5d7cdf6806ba2ecc Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 27 Mar 2017 15:53:47 -0700 Subject: [PATCH] Apply working dir and env from image config Signed-off-by: Mrunal Patel --- server/container_create.go | 52 +++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/server/container_create.go b/server/container_create.go index fe8b8c0a..dd39a4de 100644 --- a/server/container_create.go +++ b/server/container_create.go @@ -242,24 +242,6 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string, // creates a spec Generator with the default spec. specgen := generate.New() - cwd := containerConfig.WorkingDir - if cwd == "" { - cwd = "/" - } - specgen.SetProcessCwd(cwd) - - envs := containerConfig.GetEnvs() - if envs != nil { - for _, item := range envs { - key := item.Key - value := item.Value - if key == "" { - continue - } - specgen.AddProcessEnv(key, value) - } - } - if err := addOciBindMounts(sb, containerConfig, &specgen); err != nil { return nil, err } @@ -467,6 +449,40 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string, } specgen.SetProcessArgs(processArgs) + // Add environment variables from CRI and image config + envs := containerConfig.GetEnvs() + if envs != nil { + for _, item := range envs { + key := item.Key + value := item.Value + if key == "" { + continue + } + specgen.AddProcessEnv(key, value) + } + } + for _, item := range containerInfo.Config.Config.Env { + parts := strings.SplitN(item, "=", 2) + if len(parts) != 2 { + return nil, fmt.Errorf("invalid env from image: %s", item) + } + + if parts[0] == "" { + continue + } + specgen.AddProcessEnv(parts[0], parts[1]) + } + + // Set working directory + // Pick it up from image config first and override if specified in CRI + imageCwd := containerInfo.Config.Config.WorkingDir + specgen.SetProcessCwd(imageCwd) + + cwd := containerConfig.WorkingDir + if cwd != "" { + specgen.SetProcessCwd(cwd) + } + // by default, the root path is an empty string. set it now. specgen.SetRootPath(mountPoint)