Browse code

Add ENV variables support to WORKDIR build command

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
Docker-DCO-1.1-Signed-off-by: Adrien Folie <folie.adrien@gmail.com> (github: folieadrien)

Conflicts:
builder/builder.go
This file has been deleted.

Adrien Folie authored on 2014/07/16 02:17:20
Showing 2 changed files
... ...
@@ -531,9 +531,19 @@ instruction. For example:
531 531
     WORKDIR c
532 532
     RUN pwd
533 533
 
534
-The output of the final `pwd` command in this Dockerfile would be
534
+The output of the final `pwd` command in this `Dockerfile` would be
535 535
 `/a/b/c`.
536 536
 
537
+The `WORKDIR` instruction can resolve environment variables previously set using
538
+`ENV`. You can only use environment variables explicitly set in the `Dockerfile`.
539
+For example:
540
+
541
+    ENV DIRPATH /path
542
+    WORKDIR $DIRPATH/$DIRNAME
543
+
544
+The output of the final `pwd` command in this `Dockerfile` would be
545
+`/path/$DIRNAME`
546
+
537 547
 ## ONBUILD
538 548
 
539 549
     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]"