Browse code

dispatchers: warn user if try to use EXPOSE ip:hostPort:containerPort

We could use EXPOSE ip:hostPort:containerPort,
but actually it did as EXPOSE ::containerPort

commit 2275c833 already warned user on daemon side.
This patch will print warning message on client side.

Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>

Chen Hanxiao authored on 2015/03/05 11:09:50
Showing 2 changed files
... ...
@@ -339,11 +339,19 @@ func expose(b *Builder, args []string, attributes map[string]bool, original stri
339 339
 		b.Config.ExposedPorts = make(nat.PortSet)
340 340
 	}
341 341
 
342
-	ports, _, err := nat.ParsePortSpecs(append(portsTab, b.Config.PortSpecs...))
342
+	ports, bindingMap, err := nat.ParsePortSpecs(append(portsTab, b.Config.PortSpecs...))
343 343
 	if err != nil {
344 344
 		return err
345 345
 	}
346 346
 
347
+	for _, bindings := range bindingMap {
348
+		if bindings[0].HostIp != "" || bindings[0].HostPort != "" {
349
+			fmt.Fprintf(b.ErrStream, " ---> Using Dockerfile's EXPOSE instruction"+
350
+				"      to map host ports to container ports (ip:hostPort:containerPort) is deprecated.\n"+
351
+				"      Please use -p to publish the ports.\n")
352
+		}
353
+	}
354
+
347 355
 	// instead of using ports directly, we build a list of ports and sort it so
348 356
 	// the order is consistent. This prevents cache burst where map ordering
349 357
 	// changes between builds
... ...
@@ -2343,6 +2343,33 @@ func TestBuildExposeUpperCaseProto(t *testing.T) {
2343 2343
 	logDone("build - expose port with upper case proto")
2344 2344
 }
2345 2345
 
2346
+func TestBuildExposeHostPort(t *testing.T) {
2347
+	// start building docker file with ip:hostPort:containerPort
2348
+	name := "testbuildexpose"
2349
+	expected := "map[5678/tcp:map[]]"
2350
+	defer deleteImages(name)
2351
+	_, out, err := buildImageWithOut(name,
2352
+		`FROM scratch
2353
+        EXPOSE 192.168.1.2:2375:5678`,
2354
+		true)
2355
+	if err != nil {
2356
+		t.Fatal(err)
2357
+	}
2358
+
2359
+	if !strings.Contains(out, "to map host ports to container ports (ip:hostPort:containerPort) is deprecated.") {
2360
+		t.Fatal("Missing warning message")
2361
+	}
2362
+
2363
+	res, err := inspectField(name, "Config.ExposedPorts")
2364
+	if err != nil {
2365
+		t.Fatal(err)
2366
+	}
2367
+	if res != expected {
2368
+		t.Fatalf("Exposed ports %s, expected %s", res, expected)
2369
+	}
2370
+	logDone("build - ignore exposing host's port")
2371
+}
2372
+
2346 2373
 func TestBuildEmptyEntrypointInheritance(t *testing.T) {
2347 2374
 	name := "testbuildentrypointinheritance"
2348 2375
 	name2 := "testbuildentrypointinheritance2"