CodexBloom - Programming Q&A Platform

Docker container using multi-stage build scenarios to copy files: 'No such file or directory'

👀 Views: 23 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-03
docker multi-stage-build node.js Dockerfile

I'm performance testing and I'm writing unit tests and I'm trying to use Docker multi-stage builds to optimize my image size while building a Node.js application, but I'm working with an scenario when I try to copy files from the build stage to the final image... Specifically, I'm getting the following behavior during the build process: ``` COPY --from=builder /app/dist /usr/src/app/dist behavior: Service 'myapp' failed to build: COPY failed: stat /var/lib/docker/overlay2/.../diff/app/dist: no such file or directory ``` In my `Dockerfile`, I have the following configuration: ```dockerfile # Stage 1: Build FROM node:14 AS builder WORKDIR /app COPY package.json package-lock.json ./ RUN npm install COPY . . RUN npm run build # Stage 2: Production FROM node:14 WORKDIR /usr/src/app COPY --from=builder /app/dist /usr/src/app/dist COPY package.json ./ RUN npm install --production CMD ["node", "dist/index.js"] ``` I double-checked the `build` script in my `package.json`, and it looks like this: ```json "scripts": { "build": "tsc" } ``` This suggests that the TypeScript compiler should generate the output files in the `dist` folder. I confirmed that the `dist` folder exists in the builder stage after running `npm run build`, but for some reason, the COPY command fails to find it when moving to the second stage. I've tried running `docker build` with the `--no-cache` option to ensure there are no cached layers causing the scenario. Additionally, I've verified that the path is correct by adding a `RUN ls -la /app` command just before the COPY command in the Dockerfile, which does show the `dist` directory. I'm running Docker version 20.10.7 on Ubuntu 20.04. Any help would be appreciated! I've been using Dockerfile for about a year now. Cheers for any assistance!