Browse code

Merge pull request #8341 from unclejack/add_workdir_test

add test for workdir env vars and add docs

Sven Dowideit authored on 2014/10/07 13:27:32
Showing 2 changed files
... ...
@@ -552,9 +552,19 @@ instruction. For example:
552 552
     WORKDIR c
553 553
     RUN pwd
554 554
 
555
-The output of the final `pwd` command in this Dockerfile would be
555
+The output of the final `pwd` command in this `Dockerfile` would be
556 556
 `/a/b/c`.
557 557
 
558
+The `WORKDIR` instruction can resolve environment variables previously set using
559
+`ENV`. You can only use environment variables explicitly set in the `Dockerfile`.
560
+For example:
561
+
562
+    ENV DIRPATH /path
563
+    WORKDIR $DIRPATH/$DIRNAME
564
+
565
+The output of the final `pwd` command in this `Dockerfile` would be
566
+`/path/$DIRNAME`
567
+
558 568
 ## ONBUILD
559 569
 
560 570
     ONBUILD [INSTRUCTION]
... ...
@@ -973,6 +973,30 @@ func TestBuildRelativeWorkdir(t *testing.T) {
973 973
 	logDone("build - relative workdir")
974 974
 }
975 975
 
976
+func TestBuildWorkdirWithEnvVariables(t *testing.T) {
977
+	name := "testbuildworkdirwithenvvariables"
978
+	expected := "/test1/test2/$MISSING_VAR"
979
+	defer deleteImages(name)
980
+	_, err := buildImage(name,
981
+		`FROM busybox
982
+		ENV DIRPATH /test1
983
+		ENV SUBDIRNAME test2
984
+		WORKDIR $DIRPATH
985
+		WORKDIR $SUBDIRNAME/$MISSING_VAR`,
986
+		true)
987
+	if err != nil {
988
+		t.Fatal(err)
989
+	}
990
+	res, err := inspectField(name, "Config.WorkingDir")
991
+	if err != nil {
992
+		t.Fatal(err)
993
+	}
994
+	if res != expected {
995
+		t.Fatalf("Workdir %s, expected %s", res, expected)
996
+	}
997
+	logDone("build - workdir with env variables")
998
+}
999
+
976 1000
 func TestBuildEnv(t *testing.T) {
977 1001
 	name := "testbuildenv"
978 1002
 	expected := "[PATH=/test:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PORT=2375]"