From 34fbaa5f6d0d8bd31504f43db56090be6dfa9af4 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Fri, 22 Mar 2013 20:36:34 -0700 Subject: [PATCH] 'docker run -e': set environment variables in a container --- commands.go | 15 +++++++++++++++ container.go | 10 ++++++++++ sysinit.go | 8 -------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/commands.go b/commands.go index 6e8342ea37..bab2b7df71 100644 --- a/commands.go +++ b/commands.go @@ -766,6 +766,18 @@ func (p *ports) Set(value string) error { return nil } +// ListOpts type +type ListOpts []string + +func (opts *ListOpts) String() string { + return fmt.Sprint(*opts) +} + +func (opts *ListOpts) Set(value string) error { + *opts = append(*opts, value) + return nil +} + func (srv *Server) CmdTag(stdin io.ReadCloser, stdout io.Writer, args ...string) error { cmd := rcli.Subcmd(stdout, "tag", "[OPTIONS] IMAGE REPOSITORY [TAG]", "Tag an image into a repository") force := cmd.Bool("f", false, "Force") @@ -789,6 +801,8 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) var fl_ports ports cmd.Var(&fl_ports, "p", "Map a network port to the container") + var fl_env ListOpts + cmd.Var(&fl_env, "e", "Set environment variables") if err := cmd.Parse(args); err != nil { return nil } @@ -819,6 +833,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) Tty: *fl_tty, OpenStdin: *fl_stdin, Memory: *fl_memory, + Env: fl_env, }) if err != nil { return errors.New("Error creating container: " + err.Error()) diff --git a/container.go b/container.go index ea091b8fa5..2b94fa7e00 100644 --- a/container.go +++ b/container.go @@ -53,6 +53,7 @@ type Config struct { Ports []int Tty bool // Attach standard streams to a tty, including stdin if it is not closed. OpenStdin bool // Open stdin + Env []string } type NetworkSettings struct { @@ -200,6 +201,15 @@ func (container *Container) Start() error { container.cmd = exec.Command("/usr/bin/lxc-start", params...) + // Setup environment + container.cmd.Env = append( + []string{ + "HOME=/", + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + }, + container.Config.Env..., + ) + var err error if container.Config.Tty { err = container.startPty() diff --git a/sysinit.go b/sysinit.go index c475b3365d..f701417978 100644 --- a/sysinit.go +++ b/sysinit.go @@ -52,13 +52,6 @@ func changeUser(u string) { } } -// Set the environment to a known, repeatable state -func setupEnv() { - os.Clearenv() - os.Setenv("HOME", "/") - os.Setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") -} - func executeProgram(name string, args []string) { path, err := exec.LookPath(name) if err != nil { @@ -86,6 +79,5 @@ func SysInit() { setupNetworking(*gw) changeUser(*u) - setupEnv() executeProgram(flag.Arg(0), flag.Args()) }