Selaa lähdekoodia

Merge pull request #4691 from tianon/relative-workdir

Add proper support for relative WORKDIR instructions
Michael Crosby 11 vuotta sitten
vanhempi
commit
47470d299d
3 muutettua tiedostoa jossa 37 lisäystä ja 2 poistoa
  1. 10 1
      docs/sources/reference/builder.rst
  2. 19 0
      integration/buildfile_test.go
  3. 8 1
      server/buildfile.go

+ 10 - 1
docs/sources/reference/builder.rst

@@ -407,7 +407,16 @@ the image.
 The ``WORKDIR`` instruction sets the working directory for the ``RUN``, ``CMD`` and
 ``ENTRYPOINT``  Dockerfile commands that follow it.
 
-It can be used multiple times in the one Dockerfile.
+It can be used multiple times in the one Dockerfile.  If a relative path is
+provided, it will be relative to the path of the previous ``WORKDIR``
+instruction.  For example:
+
+    WORKDIR /a
+    WORKDIR b
+    WORKDIR c
+    RUN pwd
+
+The output of the final ``pwd`` command in this Dockerfile would be ``/a/b/c``.
 
 3.11 ONBUILD
 ------------

+ 19 - 0
integration/buildfile_test.go

@@ -441,6 +441,25 @@ func TestBuildUser(t *testing.T) {
 	}
 }
 
+func TestBuildRelativeWorkdir(t *testing.T) {
+	img, err := buildImage(testContextTemplate{`
+		FROM {IMAGE}
+		RUN [ "$PWD" = '/' ]
+		WORKDIR test1
+		RUN [ "$PWD" = '/test1' ]
+		WORKDIR /test2
+		RUN [ "$PWD" = '/test2' ]
+		WORKDIR test3
+		RUN [ "$PWD" = '/test2/test3' ]
+	`, nil, nil}, t, nil, true)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if img.Config.WorkingDir != "/test2/test3" {
+		t.Fatalf("Expected workdir to be '/test2/test3', received '%s'", img.Config.WorkingDir)
+	}
+}
+
 func TestBuildEnv(t *testing.T) {
 	img, err := buildImage(testContextTemplate{`
         from {IMAGE}

+ 8 - 1
server/buildfile.go

@@ -338,7 +338,14 @@ func (b *buildFile) CmdCopy(args string) error {
 }
 
 func (b *buildFile) CmdWorkdir(workdir string) error {
-	b.config.WorkingDir = workdir
+	if workdir[0] == '/' {
+		b.config.WorkingDir = workdir
+	} else {
+		if b.config.WorkingDir == "" {
+			b.config.WorkingDir = "/"
+		}
+		b.config.WorkingDir = filepath.Join(b.config.WorkingDir, workdir)
+	}
 	return b.commit("", b.config.Cmd, fmt.Sprintf("WORKDIR %v", workdir))
 }