|
@@ -10,6 +10,7 @@ import (
|
|
|
"github.com/dotcloud/docker/utils"
|
|
|
"io"
|
|
|
"io/ioutil"
|
|
|
+ "mime/multipart"
|
|
|
"net"
|
|
|
"net/http"
|
|
|
"net/http/httputil"
|
|
@@ -72,37 +73,37 @@ func (cli *DockerCli) CmdHelp(args ...string) error {
|
|
|
}
|
|
|
}
|
|
|
help := fmt.Sprintf("Usage: docker [OPTIONS] COMMAND [arg...]\n -H=\"%s:%d\": Host:port to bind/connect to\n\nA self-sufficient runtime for linux containers.\n\nCommands:\n", cli.host, cli.port)
|
|
|
- for cmd, description := range map[string]string{
|
|
|
- "attach": "Attach to a running container",
|
|
|
- "build": "Build a container from Dockerfile or via stdin",
|
|
|
- "commit": "Create a new image from a container's changes",
|
|
|
- "diff": "Inspect changes on a container's filesystem",
|
|
|
- "export": "Stream the contents of a container as a tar archive",
|
|
|
- "history": "Show the history of an image",
|
|
|
- "images": "List images",
|
|
|
- "import": "Create a new filesystem image from the contents of a tarball",
|
|
|
- "info": "Display system-wide information",
|
|
|
- "insert": "Insert a file in an image",
|
|
|
- "inspect": "Return low-level information on a container",
|
|
|
- "kill": "Kill a running container",
|
|
|
- "login": "Register or Login to the docker registry server",
|
|
|
- "logs": "Fetch the logs of a container",
|
|
|
- "port": "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT",
|
|
|
- "ps": "List containers",
|
|
|
- "pull": "Pull an image or a repository from the docker registry server",
|
|
|
- "push": "Push an image or a repository to the docker registry server",
|
|
|
- "restart": "Restart a running container",
|
|
|
- "rm": "Remove a container",
|
|
|
- "rmi": "Remove an image",
|
|
|
- "run": "Run a command in a new container",
|
|
|
- "search": "Search for an image in the docker index",
|
|
|
- "start": "Start a stopped container",
|
|
|
- "stop": "Stop a running container",
|
|
|
- "tag": "Tag an image into a repository",
|
|
|
- "version": "Show the docker version information",
|
|
|
- "wait": "Block until a container stops, then print its exit code",
|
|
|
+ for _, command := range [][2]string{
|
|
|
+ {"attach", "Attach to a running container"},
|
|
|
+ {"build", "Build a container from a Dockerfile"},
|
|
|
+ {"commit", "Create a new image from a container's changes"},
|
|
|
+ {"diff", "Inspect changes on a container's filesystem"},
|
|
|
+ {"export", "Stream the contents of a container as a tar archive"},
|
|
|
+ {"history", "Show the history of an image"},
|
|
|
+ {"images", "List images"},
|
|
|
+ {"import", "Create a new filesystem image from the contents of a tarball"},
|
|
|
+ {"info", "Display system-wide information"},
|
|
|
+ {"insert", "Insert a file in an image"},
|
|
|
+ {"inspect", "Return low-level information on a container"},
|
|
|
+ {"kill", "Kill a running container"},
|
|
|
+ {"login", "Register or Login to the docker registry server"},
|
|
|
+ {"logs", "Fetch the logs of a container"},
|
|
|
+ {"port", "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"},
|
|
|
+ {"ps", "List containers"},
|
|
|
+ {"pull", "Pull an image or a repository from the docker registry server"},
|
|
|
+ {"push", "Push an image or a repository to the docker registry server"},
|
|
|
+ {"restart", "Restart a running container"},
|
|
|
+ {"rm", "Remove a container"},
|
|
|
+ {"rmi", "Remove an image"},
|
|
|
+ {"run", "Run a command in a new container"},
|
|
|
+ {"search", "Search for an image in the docker index"},
|
|
|
+ {"start", "Start a stopped container"},
|
|
|
+ {"stop", "Stop a running container"},
|
|
|
+ {"tag", "Tag an image into a repository"},
|
|
|
+ {"version", "Show the docker version information"},
|
|
|
+ {"wait", "Block until a container stops}, then print its exit code"},
|
|
|
} {
|
|
|
- help += fmt.Sprintf(" %-10.10s%s\n", cmd, description)
|
|
|
+ help += fmt.Sprintf(" %-10.10s%s\n", command[0], command[1])
|
|
|
}
|
|
|
fmt.Println(help)
|
|
|
return nil
|
|
@@ -122,39 +123,103 @@ func (cli *DockerCli) CmdInsert(args ...string) error {
|
|
|
v.Set("url", cmd.Arg(1))
|
|
|
v.Set("path", cmd.Arg(2))
|
|
|
|
|
|
- err := cli.stream("POST", "/images/"+cmd.Arg(0)+"/insert?"+v.Encode(), nil, os.Stdout)
|
|
|
- if err != nil {
|
|
|
+ if err := cli.stream("POST", "/images/"+cmd.Arg(0)+"/insert?"+v.Encode(), nil, os.Stdout); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (cli *DockerCli) CmdBuild(args ...string) error {
|
|
|
- cmd := Subcmd("build", "-|Dockerfile", "Build an image from Dockerfile or via stdin")
|
|
|
+ cmd := Subcmd("build", "[OPTIONS] [CONTEXT]", "Build an image from a Dockerfile")
|
|
|
+ fileName := cmd.String("f", "Dockerfile", "Use `file` as Dockerfile. Can be '-' for stdin")
|
|
|
if err := cmd.Parse(args); err != nil {
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
var (
|
|
|
- file io.ReadCloser
|
|
|
- err error
|
|
|
+ file io.ReadCloser
|
|
|
+ multipartBody io.Reader
|
|
|
+ err error
|
|
|
)
|
|
|
|
|
|
- if cmd.NArg() == 0 {
|
|
|
- file, err = os.Open("Dockerfile")
|
|
|
+ // Init the needed component for the Multipart
|
|
|
+ buff := bytes.NewBuffer([]byte{})
|
|
|
+ multipartBody = buff
|
|
|
+ w := multipart.NewWriter(buff)
|
|
|
+ boundary := strings.NewReader("\r\n--" + w.Boundary() + "--\r\n")
|
|
|
+
|
|
|
+ // Create a FormFile multipart for the Dockerfile
|
|
|
+ if *fileName == "-" {
|
|
|
+ file = os.Stdin
|
|
|
+ } else {
|
|
|
+ file, err = os.Open(*fileName)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- } else if cmd.Arg(0) == "-" {
|
|
|
- file = os.Stdin
|
|
|
+ defer file.Close()
|
|
|
+ }
|
|
|
+ if wField, err := w.CreateFormFile("Dockerfile", *fileName); err != nil {
|
|
|
+ return err
|
|
|
} else {
|
|
|
- file, err = os.Open(cmd.Arg(0))
|
|
|
+ io.Copy(wField, file)
|
|
|
+ }
|
|
|
+ multipartBody = io.MultiReader(multipartBody, boundary)
|
|
|
+
|
|
|
+ compression := Bzip2
|
|
|
+
|
|
|
+ // Create a FormFile multipart for the context if needed
|
|
|
+ if cmd.Arg(0) != "" {
|
|
|
+ // FIXME: Use NewTempArchive in order to have the size and avoid too much memory usage?
|
|
|
+ context, err := Tar(cmd.Arg(0), compression)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+ // NOTE: Do this in case '.' or '..' is input
|
|
|
+ absPath, err := filepath.Abs(cmd.Arg(0))
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if wField, err := w.CreateFormFile("Context", filepath.Base(absPath)+"."+compression.Extension()); err != nil {
|
|
|
+ return err
|
|
|
+ } else {
|
|
|
+ // FIXME: Find a way to have a progressbar for the upload too
|
|
|
+ io.Copy(wField, utils.ProgressReader(ioutil.NopCloser(context), -1, os.Stdout, "Caching Context %v/%v (%v)\r", false))
|
|
|
+ }
|
|
|
+
|
|
|
+ multipartBody = io.MultiReader(multipartBody, boundary)
|
|
|
}
|
|
|
- if _, err := NewBuilderClient("0.0.0.0", 4243).Build(file); err != nil {
|
|
|
+
|
|
|
+ // Send the multipart request with correct content-type
|
|
|
+ req, err := http.NewRequest("POST", fmt.Sprintf("http://%s:%d%s", cli.host, cli.port, "/build"), multipartBody)
|
|
|
+ if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+ req.Header.Set("Content-Type", w.FormDataContentType())
|
|
|
+ if cmd.Arg(0) != "" {
|
|
|
+ req.Header.Set("X-Docker-Context-Compression", compression.Flag())
|
|
|
+ fmt.Println("Uploading Context...")
|
|
|
+ }
|
|
|
+
|
|
|
+ resp, err := http.DefaultClient.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ // Check for errors
|
|
|
+ if resp.StatusCode < 200 || resp.StatusCode >= 400 {
|
|
|
+ body, err := ioutil.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return fmt.Errorf("error: %s", body)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Output the result
|
|
|
+ if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -301,12 +366,10 @@ func (cli *DockerCli) CmdWait(args ...string) error {
|
|
|
// 'docker version': show version information
|
|
|
func (cli *DockerCli) CmdVersion(args ...string) error {
|
|
|
cmd := Subcmd("version", "", "Show the docker version information.")
|
|
|
- fmt.Println(len(args))
|
|
|
if err := cmd.Parse(args); err != nil {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- fmt.Println(cmd.NArg())
|
|
|
if cmd.NArg() > 0 {
|
|
|
cmd.Usage()
|
|
|
return nil
|
|
@@ -788,7 +851,7 @@ func (cli *DockerCli) CmdPs(args ...string) error {
|
|
|
v.Set("before", *before)
|
|
|
}
|
|
|
|
|
|
- body, _, err := cli.call("GET", "/containers/ps?"+v.Encode(), nil)
|
|
|
+ body, _, err := cli.call("GET", "/containers/json?"+v.Encode(), nil)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -806,9 +869,9 @@ func (cli *DockerCli) CmdPs(args ...string) error {
|
|
|
for _, out := range outs {
|
|
|
if !*quiet {
|
|
|
if *noTrunc {
|
|
|
- fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", out.Id, out.Image, out.Command, out.Status, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Ports)
|
|
|
+ fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\n", out.Id, out.Image, out.Command, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, out.Ports)
|
|
|
} else {
|
|
|
- fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", utils.TruncateId(out.Id), out.Image, utils.Trunc(out.Command, 20), out.Status, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Ports)
|
|
|
+ fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\n", utils.TruncateId(out.Id), out.Image, utils.Trunc(out.Command, 20), utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, out.Ports)
|
|
|
}
|
|
|
} else {
|
|
|
if *noTrunc {
|
|
@@ -950,15 +1013,35 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ splitStderr := container.Config.Tty
|
|
|
+
|
|
|
+ connections := 1
|
|
|
+ if splitStderr {
|
|
|
+ connections += 1
|
|
|
+ }
|
|
|
+ chErrors := make(chan error, connections)
|
|
|
+ cli.monitorTtySize(cmd.Arg(0))
|
|
|
+ if splitStderr {
|
|
|
+ go func() {
|
|
|
+ chErrors <- cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?stream=1&stderr=1", false, nil, os.Stderr)
|
|
|
+ }()
|
|
|
+ }
|
|
|
v := url.Values{}
|
|
|
v.Set("stream", "1")
|
|
|
- v.Set("stdout", "1")
|
|
|
- v.Set("stderr", "1")
|
|
|
v.Set("stdin", "1")
|
|
|
-
|
|
|
- cli.monitorTtySize(cmd.Arg(0))
|
|
|
- if err := cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), container.Config.Tty); err != nil {
|
|
|
- return err
|
|
|
+ v.Set("stdout", "1")
|
|
|
+ if !splitStderr {
|
|
|
+ v.Set("stderr", "1")
|
|
|
+ }
|
|
|
+ go func() {
|
|
|
+ chErrors <- cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), container.Config.Tty, os.Stdin, os.Stdout)
|
|
|
+ }()
|
|
|
+ for connections > 0 {
|
|
|
+ err := <-chErrors
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ connections -= 1
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
@@ -1122,19 +1205,14 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
|
|
fmt.Fprintln(os.Stderr, "WARNING: ", warning)
|
|
|
}
|
|
|
|
|
|
- v := url.Values{}
|
|
|
- v.Set("logs", "1")
|
|
|
- v.Set("stream", "1")
|
|
|
+ splitStderr := !config.Tty
|
|
|
|
|
|
- if config.AttachStdin {
|
|
|
- v.Set("stdin", "1")
|
|
|
- }
|
|
|
- if config.AttachStdout {
|
|
|
- v.Set("stdout", "1")
|
|
|
+ connections := 0
|
|
|
+ if config.AttachStdin || config.AttachStdout || (!splitStderr && config.AttachStderr) {
|
|
|
+ connections += 1
|
|
|
}
|
|
|
- if config.AttachStderr {
|
|
|
- v.Set("stderr", "1")
|
|
|
-
|
|
|
+ if splitStderr && config.AttachStderr {
|
|
|
+ connections += 1
|
|
|
}
|
|
|
|
|
|
//start the container
|
|
@@ -1143,10 +1221,38 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- if config.AttachStdin || config.AttachStdout || config.AttachStderr {
|
|
|
+ if connections > 0 {
|
|
|
+ chErrors := make(chan error, connections)
|
|
|
cli.monitorTtySize(out.Id)
|
|
|
- if err := cli.hijack("POST", "/containers/"+out.Id+"/attach?"+v.Encode(), config.Tty); err != nil {
|
|
|
- return err
|
|
|
+
|
|
|
+ if splitStderr && config.AttachStderr {
|
|
|
+ go func() {
|
|
|
+ chErrors <- cli.hijack("POST", "/containers/"+out.Id+"/attach?logs=1&stream=1&stderr=1", config.Tty, nil, os.Stderr)
|
|
|
+ }()
|
|
|
+ }
|
|
|
+
|
|
|
+ v := url.Values{}
|
|
|
+ v.Set("logs", "1")
|
|
|
+ v.Set("stream", "1")
|
|
|
+
|
|
|
+ if config.AttachStdin {
|
|
|
+ v.Set("stdin", "1")
|
|
|
+ }
|
|
|
+ if config.AttachStdout {
|
|
|
+ v.Set("stdout", "1")
|
|
|
+ }
|
|
|
+ if !splitStderr && config.AttachStderr {
|
|
|
+ v.Set("stderr", "1")
|
|
|
+ }
|
|
|
+ go func() {
|
|
|
+ chErrors <- cli.hijack("POST", "/containers/"+out.Id+"/attach?"+v.Encode(), config.Tty, os.Stdin, os.Stdout)
|
|
|
+ }()
|
|
|
+ for connections > 0 {
|
|
|
+ err := <-chErrors
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ connections -= 1
|
|
|
}
|
|
|
}
|
|
|
if !config.AttachStdout && !config.AttachStderr {
|
|
@@ -1282,7 +1388,7 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (cli *DockerCli) hijack(method, path string, setRawTerminal bool) error {
|
|
|
+func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in *os.File, out io.Writer) error {
|
|
|
req, err := http.NewRequest(method, fmt.Sprintf("/v%g%s", API_VERSION, path), nil)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -1300,20 +1406,19 @@ func (cli *DockerCli) hijack(method, path string, setRawTerminal bool) error {
|
|
|
defer rwc.Close()
|
|
|
|
|
|
receiveStdout := utils.Go(func() error {
|
|
|
- _, err := io.Copy(os.Stdout, br)
|
|
|
+ _, err := io.Copy(out, br)
|
|
|
return err
|
|
|
})
|
|
|
|
|
|
- if setRawTerminal && term.IsTerminal(int(os.Stdin.Fd())) && os.Getenv("NORAW") == "" {
|
|
|
+ if in != nil && setRawTerminal && term.IsTerminal(int(in.Fd())) && os.Getenv("NORAW") == "" {
|
|
|
if oldState, err := term.SetRawTerminal(); err != nil {
|
|
|
return err
|
|
|
} else {
|
|
|
defer term.RestoreTerminal(oldState)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
sendStdin := utils.Go(func() error {
|
|
|
- _, err := io.Copy(rwc, os.Stdin)
|
|
|
+ _, err := io.Copy(rwc, in)
|
|
|
if err := rwc.(*net.TCPConn).CloseWrite(); err != nil {
|
|
|
fmt.Fprintf(os.Stderr, "Couldn't send EOF: %s\n", err)
|
|
|
}
|