|
@@ -2,6 +2,7 @@ package docker
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
+ "errors"
|
|
|
"flag"
|
|
|
"fmt"
|
|
|
"github.com/dotcloud/docker/term"
|
|
@@ -76,6 +77,7 @@ type Config struct {
|
|
|
Image string // Name of the image as it was passed by the operator (eg. could be symbolic)
|
|
|
Volumes map[string]struct{}
|
|
|
VolumesFrom string
|
|
|
+ WorkingDir string
|
|
|
Entrypoint []string
|
|
|
NetworkDisabled bool
|
|
|
Privileged bool
|
|
@@ -92,6 +94,10 @@ type BindMap struct {
|
|
|
Mode string
|
|
|
}
|
|
|
|
|
|
+var (
|
|
|
+ ErrInvaidWorikingDirectory = errors.New("The working directory is invalid. It needs to be an absolute path.")
|
|
|
+)
|
|
|
+
|
|
|
func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, *flag.FlagSet, error) {
|
|
|
cmd := Subcmd("run", "[OPTIONS] IMAGE [COMMAND] [ARG...]", "Run a command in a new container")
|
|
|
if len(args) > 0 && args[0] != "--help" {
|
|
@@ -100,6 +106,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig,
|
|
|
}
|
|
|
|
|
|
flHostname := cmd.String("h", "", "Container host name")
|
|
|
+ flWorkingDir := cmd.String("w", "", "Working directory inside the container")
|
|
|
flUser := cmd.String("u", "", "Username or UID")
|
|
|
flDetach := cmd.Bool("d", false, "Detached mode: Run container in the background, print new container id")
|
|
|
flAttach := NewAttachOpts()
|
|
@@ -139,6 +146,9 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig,
|
|
|
if *flDetach && len(flAttach) > 0 {
|
|
|
return nil, nil, cmd, fmt.Errorf("Conflicting options: -a and -d")
|
|
|
}
|
|
|
+ if *flWorkingDir != "" && !path.IsAbs(*flWorkingDir) {
|
|
|
+ return nil, nil, cmd, ErrInvaidWorikingDirectory
|
|
|
+ }
|
|
|
// If neither -d or -a are set, attach to everything by default
|
|
|
if len(flAttach) == 0 && !*flDetach {
|
|
|
if !*flDetach {
|
|
@@ -197,6 +207,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig,
|
|
|
VolumesFrom: *flVolumesFrom,
|
|
|
Entrypoint: entrypoint,
|
|
|
Privileged: *flPrivileged,
|
|
|
+ WorkingDir: *flWorkingDir,
|
|
|
}
|
|
|
hostConfig := &HostConfig{
|
|
|
Binds: binds,
|
|
@@ -666,6 +677,18 @@ func (container *Container) Start(hostConfig *HostConfig) error {
|
|
|
"-e", "container=lxc",
|
|
|
"-e", "HOSTNAME="+container.Config.Hostname,
|
|
|
)
|
|
|
+ if container.Config.WorkingDir != "" {
|
|
|
+ workingDir := path.Clean(container.Config.WorkingDir)
|
|
|
+ utils.Debugf("[working dir] working dir is %s", workingDir)
|
|
|
+
|
|
|
+ if err := os.MkdirAll(path.Join(container.RootfsPath(), workingDir), 0755); err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ params = append(params,
|
|
|
+ "-w", workingDir,
|
|
|
+ )
|
|
|
+ }
|
|
|
|
|
|
for _, elem := range container.Config.Env {
|
|
|
params = append(params, "-e", elem)
|