<p>When creating an image using a Dockerfile, docker layers are created for each step. The description of layers can be found on the official docker <a href="https://docs.docker.com/storage/storagedriver/#images-and-layers" rel="nofollow noreferrer">page</a>.</p>
<p>Accordingly, when using Multistage to create an image, it introduces a new build step that can be named using an alias in the FROM statement, and all layers except the image layer created via the last FROM statement are deleted.</p>
<p>After build your image, check if the layer that contains WORKDIR whether exists or not using docker image inspect. I think you probably know the unique layer value for each step.</p>
<p>You can see the image consists with which layers.
e.g.,</p>
<pre><code>$docker image inspect [OPTIONS] 464503b73bfe # IMAGE
[
    {
        &quot;Id&quot;: &quot;sha256:464bf1...46c&quot;,
        ...

        &quot;RootFS&quot;: {
            &quot;Type&quot;: &quot;layers&quot;,
            &quot;Layers&quot;: [
                &quot;sha256:26711...8fcf&quot;,
                &quot;sha256:6c5a51...56e0&quot;
            ]
        },
        ...
</code></pre>

CC BY-SA 4.0
2020-12-10T18:17:23.210Z
2020-12-10T19:11:23.243Z
2020-12-10T19:11:23.243Z
Chachatonel Hashimotto
<p>My Dockerfile contains this:</p>
<pre><code>### STAGE 1: Build Angular app ###
FROM node:14.15 AS build
WORKDIR /usr/src/app
COPY src/hub/package.json ./
RUN npm cache verify
RUN npm install
COPY src/hub/ .
RUN npm run-script build-azdev
RUN npm prune --production

### STAGE 2: Run Angular app in nginx ###
FROM nginx:1.19-alpine
COPY /scripts/nginx/nginx.conf /etc/nginx/nginx.conf
RUN rm -rf /usr/share/nginx/html/*
COPY --from=build /usr/src/app/dist/* /usr/share/nginx/html/
EXPOSE 80
CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
</code></pre>
<p>Once <code>COPY --from=build /usr/src/app/dist/* /usr/share/nginx/html/</code> is done, should the contents of WORKDIR (/usr/src/app) be deleted once Stage 2 is done building?</p>

CC BY-SA 4.0
2020-12-10T16:41:24.993Z
Deleted from where? Show the command and output that has unexpected output for you.
BMitch
<docker>
In a multistage Docker build, should the WORKDIR be deleted?
Eric C
# In a multistage Docker build, should the WORKDIR be deleted?

(Asked by: username_0 on 2020-12-10)

My Dockerfile contains this:
### STAGE 1: Build Angular app ###
FROM node:14.15 AS build
WORKDIR /usr/src/app
COPY src/hub/package.json ./
RUN npm cache verify
RUN npm install
COPY src/hub/ .
RUN npm run-script build-azdev
RUN npm prune --production

### STAGE 2: Run Angular app in nginx ###
FROM nginx:1.19-alpine
COPY /scripts/nginx/nginx.conf /etc/nginx/nginx.conf
RUN rm -rf /usr/share/nginx/html/*
COPY --from=build /usr/src/app/dist/* /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Once COPY --from=build /usr/src/app/dist/* /usr/share/nginx/html/ is done, should the contents of WORKDIR (/usr/src/app) be deleted once Stage 2 is done building?

**Tags:** docker

> **Comments:**
> **username_1**: Deleted from where? Show the command and output that has unexpected output for you.  

---

## 1 answers

### Answer by username_2 on 2020-12-10. Score: 2 | Accepted answer

When creating an image using a Dockerfile, docker layers are created for each step. The description of layers can be found on the official docker page.
Accordingly, when using Multistage to create an image, it introduces a new build step that can be named using an alias in the FROM statement, and all layers except the image layer created via the last FROM statement are deleted.
After build your image, check if the layer that contains WORKDIR whether exists or not using docker image inspect. I think you probably know the unique layer value for each step.
You can see the image consists with which layers.
e.g.,
$docker image inspect [OPTIONS] 464503b73bfe # IMAGE
[
    {
        "Id": "sha256:464bf1...46c",
        ...

        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:26711...8fcf",
                "sha256:6c5a51...56e0"
            ]
        },
        ...


---

