Browse code

Don't write pull output to stdout on container creating

Fixes #8632

Signed-off-by: Alexandr Morozov <lk4d4@docker.com>

Alexandr Morozov authored on 2014/10/18 03:06:05
Showing 2 changed files
... ...
@@ -1986,6 +1986,10 @@ func (cli *DockerCli) CmdTag(args ...string) error {
1986 1986
 }
1987 1987
 
1988 1988
 func (cli *DockerCli) pullImage(image string) error {
1989
+	return cli.pullImageCustomOut(image, cli.out)
1990
+}
1991
+
1992
+func (cli *DockerCli) pullImageCustomOut(image string, out io.Writer) error {
1989 1993
 	v := url.Values{}
1990 1994
 	repos, tag := parsers.ParseRepositoryTag(image)
1991 1995
 	// pull only the image tagged 'latest' if no tag was specified
... ...
@@ -2014,7 +2018,7 @@ func (cli *DockerCli) pullImage(image string) error {
2014 2014
 	registryAuthHeader := []string{
2015 2015
 		base64.URLEncoding.EncodeToString(buf),
2016 2016
 	}
2017
-	if err = cli.stream("POST", "/images/create?"+v.Encode(), nil, cli.out, map[string][]string{"X-Registry-Auth": registryAuthHeader}); err != nil {
2017
+	if err = cli.stream("POST", "/images/create?"+v.Encode(), nil, out, map[string][]string{"X-Registry-Auth": registryAuthHeader}); err != nil {
2018 2018
 		return err
2019 2019
 	}
2020 2020
 	return nil
... ...
@@ -2081,7 +2085,8 @@ func (cli *DockerCli) createContainer(config *runconfig.Config, hostConfig *runc
2081 2081
 	if statusCode == 404 {
2082 2082
 		fmt.Fprintf(cli.err, "Unable to find image '%s' locally\n", config.Image)
2083 2083
 
2084
-		if err = cli.pullImage(config.Image); err != nil {
2084
+		// we don't want to write to stdout anything apart from container.ID
2085
+		if err = cli.pullImageCustomOut(config.Image, cli.err); err != nil {
2085 2086
 			return nil, err
2086 2087
 		}
2087 2088
 		// Retry
... ...
@@ -2,6 +2,7 @@ package main
2 2
 
3 3
 import (
4 4
 	"bufio"
5
+	"bytes"
5 6
 	"fmt"
6 7
 	"io/ioutil"
7 8
 	"net"
... ...
@@ -2380,3 +2381,18 @@ func TestRunVolumesNotRecreatedOnStart(t *testing.T) {
2380 2380
 
2381 2381
 	logDone("run - volumes not recreated on start")
2382 2382
 }
2383
+
2384
+func TestRunNoOutputFromPullInStdout(t *testing.T) {
2385
+	defer deleteAllContainers()
2386
+	// just run with unknown image
2387
+	cmd := exec.Command(dockerBinary, "run", "asdfsg")
2388
+	stdout := bytes.NewBuffer(nil)
2389
+	cmd.Stdout = stdout
2390
+	if err := cmd.Run(); err == nil {
2391
+		t.Fatal("Run with unknown image should fail")
2392
+	}
2393
+	if stdout.Len() != 0 {
2394
+		t.Fatalf("Stdout contains output from pull: %s", stdout)
2395
+	}
2396
+	logDone("run - no output from pull in stdout")
2397
+}