From 2dba72a6f762fbf9325da9443144dcffba86e0b1 Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Sat, 15 Mar 2014 14:00:35 -0600 Subject: [PATCH 1/3] Add proper support for relative WORKDIR instructions Docker-DCO-1.1-Signed-off-by: Andrew Page (github: tianon) Upstream-commit: 054b85a7b25e46935c0d91f544aac69dc3497468 Component: engine --- components/engine/integration/buildfile_test.go | 17 +++++++++++++++++ components/engine/server/buildfile.go | 9 ++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/components/engine/integration/buildfile_test.go b/components/engine/integration/buildfile_test.go index 7f6e69ece3..9c986d74c2 100644 --- a/components/engine/integration/buildfile_test.go +++ b/components/engine/integration/buildfile_test.go @@ -441,6 +441,23 @@ 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" = '/test1/test2' ] + `, nil, nil}, t, nil, true) + if err != nil { + t.Fatal(err) + } + if img.Config.WorkingDir != "/test1/test2" { + t.Fail() + } +} + func TestBuildEnv(t *testing.T) { img, err := buildImage(testContextTemplate{` from {IMAGE} diff --git a/components/engine/server/buildfile.go b/components/engine/server/buildfile.go index af6702cc1d..5d5fda4d8e 100644 --- a/components/engine/server/buildfile.go +++ b/components/engine/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)) } From 1cf125633cd4044939dd0df13daa1410f97fffea Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Mon, 17 Mar 2014 19:26:08 -0600 Subject: [PATCH 2/3] Improve WORKDIR test to cover more edge cases Docker-DCO-1.1-Signed-off-by: Andrew Page (github: tianon) Upstream-commit: c1f492755b8774005b3627da8ee001ee0b2df4eb Component: engine --- components/engine/integration/buildfile_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/components/engine/integration/buildfile_test.go b/components/engine/integration/buildfile_test.go index 9c986d74c2..95d5abb8a7 100644 --- a/components/engine/integration/buildfile_test.go +++ b/components/engine/integration/buildfile_test.go @@ -445,16 +445,18 @@ func TestBuildRelativeWorkdir(t *testing.T) { img, err := buildImage(testContextTemplate{` FROM {IMAGE} RUN [ "$PWD" = '/' ] - WORKDIR /test1 + WORKDIR test1 RUN [ "$PWD" = '/test1' ] - WORKDIR test2 - RUN [ "$PWD" = '/test1/test2' ] + 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 != "/test1/test2" { - t.Fail() + if img.Config.WorkingDir != "/test2/test3" { + t.Fatalf("Expected workdir to be '/test2/test3', received '%s'", img.Config.WorkingDir) } } From e398c4200d138d81627f5d1aec3f1b542b3c2e6e Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Mon, 17 Mar 2014 19:26:39 -0600 Subject: [PATCH 3/3] Add some documentation about relative WORKDIR values Docker-DCO-1.1-Signed-off-by: Andrew Page (github: tianon) Upstream-commit: 69299f041f78f8a86bf810142e740bbd72fe4b1b Component: engine --- components/engine/docs/sources/reference/builder.rst | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/components/engine/docs/sources/reference/builder.rst b/components/engine/docs/sources/reference/builder.rst index 0d8d750a04..1c8331e98f 100644 --- a/components/engine/docs/sources/reference/builder.rst +++ b/components/engine/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 ------------