'docker run -e': set environment variables in a container

This commit is contained in:
Solomon Hykes 2013-03-22 20:36:34 -07:00
parent 9b5f0fac81
commit 34fbaa5f6d
3 changed files with 25 additions and 8 deletions

View File

@ -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())

View File

@ -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()

View File

@ -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())
}