New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workdir in a multi-stage build isn't relative to the source stage #33753

Closed
sgreenmsft opened this Issue Jun 20, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@sgreenmsft

sgreenmsft commented Jun 20, 2017

In a multi-stage build, workdir is relative to the previous stage instead of the source stage. For example, consider:

FROM microsoft/aspnetcore as base
WORKDIR /src
RUN pwd
#output is /src

FROM base AS c1
WORKDIR c1
RUN pwd
#output is /src/c1

FROM base AS c2
WORKDIR c2
RUN pwd
#output is /src/c1/c2

FROM base AS c3
WORKDIR /src/c3
RUN pwd
#output is /src/c3

I expected c2's output to be /src/c2, but it is instead /src/c1/c2. This isn't a major issue (because one can simply specify the rooted path, as in c3), but I'm curious to know whether, firstly, this was an intentional design decision and, secondly, what, if any, other context passes between stages.

@thaJeztah

This comment has been minimized.

Show comment
Hide comment
@thaJeztah

thaJeztah Sep 21, 2017

Member

I don't think that's intentional; WORKDIR would be inherited from the base image, but should not be preserved between build-steps.

I'm able to reproduce this on Linux as well (docker 17.09-rc2):

docker build -t repro-33753 --no-cache -<<EOF
FROM busybox as base
WORKDIR /src
RUN pwd

FROM base AS c1
WORKDIR c1
RUN pwd

FROM base AS c2
WORKDIR c2
RUN pwd

FROM base AS c3
WORKDIR /src/c3
RUN pwd
EOF

Which shows:

Sending build context to Docker daemon  2.048kB
Step 1/12 : FROM busybox as base
 ---> efe10ee6727f
Step 2/12 : WORKDIR /src
 ---> f0ebf1cf9e88
Removing intermediate container caee58ac3645
Step 3/12 : RUN pwd
 ---> Running in 6808ef583a91
/src
 ---> 41001128aa87
Removing intermediate container 6808ef583a91
Step 4/12 : FROM base AS c1
 ---> 41001128aa87
Step 5/12 : WORKDIR c1
 ---> 7125dca5706e
Removing intermediate container 8db0b8fc8527
Step 6/12 : RUN pwd
 ---> Running in a44230834528
/src/c1
 ---> 49e78ea0d6c8
Removing intermediate container a44230834528
Step 7/12 : FROM base AS c2
 ---> 41001128aa87
Step 8/12 : WORKDIR c2
 ---> db9fd581f9de
Removing intermediate container cfba87c716b9
Step 9/12 : RUN pwd
 ---> Running in 167e8289ce28
/src/c1/c2
 ---> 87c17ce96802
Removing intermediate container 167e8289ce28
Step 10/12 : FROM base AS c3
 ---> 41001128aa87
Step 11/12 : WORKDIR /src/c3
 ---> f37414cdd929
Removing intermediate container b0a498a02356
Step 12/12 : RUN pwd
 ---> Running in 21375e2cc993
/src/c3
 ---> fcc706d21692
Removing intermediate container 21375e2cc993
Successfully built fcc706d21692
Successfully tagged repro-33753:latest

Or, just the pwd outputs:

/src
/src/c1
/src/c1/c2
/src/c3

The expected output would (I think) be:

/src
/src/c1
/src/c2
/src/c3

ping @tonistiigi @AkihiroSuda PTAL

Member

thaJeztah commented Sep 21, 2017

I don't think that's intentional; WORKDIR would be inherited from the base image, but should not be preserved between build-steps.

I'm able to reproduce this on Linux as well (docker 17.09-rc2):

docker build -t repro-33753 --no-cache -<<EOF
FROM busybox as base
WORKDIR /src
RUN pwd

FROM base AS c1
WORKDIR c1
RUN pwd

FROM base AS c2
WORKDIR c2
RUN pwd

FROM base AS c3
WORKDIR /src/c3
RUN pwd
EOF

Which shows:

Sending build context to Docker daemon  2.048kB
Step 1/12 : FROM busybox as base
 ---> efe10ee6727f
Step 2/12 : WORKDIR /src
 ---> f0ebf1cf9e88
Removing intermediate container caee58ac3645
Step 3/12 : RUN pwd
 ---> Running in 6808ef583a91
/src
 ---> 41001128aa87
Removing intermediate container 6808ef583a91
Step 4/12 : FROM base AS c1
 ---> 41001128aa87
Step 5/12 : WORKDIR c1
 ---> 7125dca5706e
Removing intermediate container 8db0b8fc8527
Step 6/12 : RUN pwd
 ---> Running in a44230834528
/src/c1
 ---> 49e78ea0d6c8
Removing intermediate container a44230834528
Step 7/12 : FROM base AS c2
 ---> 41001128aa87
Step 8/12 : WORKDIR c2
 ---> db9fd581f9de
Removing intermediate container cfba87c716b9
Step 9/12 : RUN pwd
 ---> Running in 167e8289ce28
/src/c1/c2
 ---> 87c17ce96802
Removing intermediate container 167e8289ce28
Step 10/12 : FROM base AS c3
 ---> 41001128aa87
Step 11/12 : WORKDIR /src/c3
 ---> f37414cdd929
Removing intermediate container b0a498a02356
Step 12/12 : RUN pwd
 ---> Running in 21375e2cc993
/src/c3
 ---> fcc706d21692
Removing intermediate container 21375e2cc993
Successfully built fcc706d21692
Successfully tagged repro-33753:latest

Or, just the pwd outputs:

/src
/src/c1
/src/c1/c2
/src/c3

The expected output would (I think) be:

/src
/src/c1
/src/c2
/src/c3

ping @tonistiigi @AkihiroSuda PTAL

@thaJeztah

This comment has been minimized.

Show comment
Hide comment
@thaJeztah

thaJeztah Sep 21, 2017

Member

Discussed this with @tonistiigi and this is fixed on master, but part of a big refactor, so we need a different patch for 17.09 (and possibly 17.06)

Member

thaJeztah commented Sep 21, 2017

Discussed this with @tonistiigi and this is fixed on master, but part of a big refactor, so we need a different patch for 17.09 (and possibly 17.06)

@thaJeztah

This comment has been minimized.

Show comment
Hide comment
@thaJeztah

thaJeztah Nov 15, 2017

Member

Closing, because this was fixed

Member

thaJeztah commented Nov 15, 2017

Closing, because this was fixed

@thaJeztah thaJeztah closed this Nov 15, 2017

JeremJR added a commit to Bonitasoft-Community/docker_bonita that referenced this issue Feb 6, 2018

release 7.6.2 (#59)
* release 7.6.2

* tr(subscription): refactor docker multistage build to avoid repeating variables
- REQUIRES docker-ce version greater than 17.09.1-ce
- @see moby/moby#33753 docker bug fix

JeremJR added a commit to Bonitasoft-Community/docker_bonita that referenced this issue Jul 4, 2018

release 7.6.2 (#59)
* release 7.6.2

* tr(subscription): refactor docker multistage build to avoid repeating variables
- REQUIRES docker-ce version greater than 17.09.1-ce
- @see moby/moby#33753 docker bug fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment