'docker run -e': set environment variables in a container
This commit is contained in:
parent
9b5f0fac81
commit
34fbaa5f6d
15
commands.go
15
commands.go
|
@ -766,6 +766,18 @@ func (p *ports) Set(value string) error {
|
||||||
return nil
|
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 {
|
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")
|
cmd := rcli.Subcmd(stdout, "tag", "[OPTIONS] IMAGE REPOSITORY [TAG]", "Tag an image into a repository")
|
||||||
force := cmd.Bool("f", false, "Force")
|
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
|
var fl_ports ports
|
||||||
|
|
||||||
cmd.Var(&fl_ports, "p", "Map a network port to the container")
|
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 {
|
if err := cmd.Parse(args); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -819,6 +833,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
||||||
Tty: *fl_tty,
|
Tty: *fl_tty,
|
||||||
OpenStdin: *fl_stdin,
|
OpenStdin: *fl_stdin,
|
||||||
Memory: *fl_memory,
|
Memory: *fl_memory,
|
||||||
|
Env: fl_env,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Error creating container: " + err.Error())
|
return errors.New("Error creating container: " + err.Error())
|
||||||
|
|
10
container.go
10
container.go
|
@ -53,6 +53,7 @@ type Config struct {
|
||||||
Ports []int
|
Ports []int
|
||||||
Tty bool // Attach standard streams to a tty, including stdin if it is not closed.
|
Tty bool // Attach standard streams to a tty, including stdin if it is not closed.
|
||||||
OpenStdin bool // Open stdin
|
OpenStdin bool // Open stdin
|
||||||
|
Env []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type NetworkSettings struct {
|
type NetworkSettings struct {
|
||||||
|
@ -200,6 +201,15 @@ func (container *Container) Start() error {
|
||||||
|
|
||||||
container.cmd = exec.Command("/usr/bin/lxc-start", params...)
|
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
|
var err error
|
||||||
if container.Config.Tty {
|
if container.Config.Tty {
|
||||||
err = container.startPty()
|
err = container.startPty()
|
||||||
|
|
|
@ -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) {
|
func executeProgram(name string, args []string) {
|
||||||
path, err := exec.LookPath(name)
|
path, err := exec.LookPath(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -86,6 +79,5 @@ func SysInit() {
|
||||||
|
|
||||||
setupNetworking(*gw)
|
setupNetworking(*gw)
|
||||||
changeUser(*u)
|
changeUser(*u)
|
||||||
setupEnv()
|
|
||||||
executeProgram(flag.Arg(0), flag.Args())
|
executeProgram(flag.Arg(0), flag.Args())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue