Verified Solution

[StackOverflow/docker] Cannot get npm installed app to be present in final Docker image

Sponsored Content
### ROOT CAUSE The issue arises because the Docker image is built with a multi-stage build where the final image does not include the built application. The npm install and build commands are executed in a build stage, but the resulting files are not copied to the final image. The final image typically uses a minimal base image (e.g., `alpine`) without Node.js tools, so the build artifacts are missing. ### CODE FIX Modify the Dockerfile to explicitly copy the built application from the build stage to the final image. Here's the corrected structure: ```dockerfile # Build stage FROM node:16 AS builder WORKDIR /app COPY package.json package-lock.json . RUN npm install COPY . . RUN npm run build # Replace with your build command (e.g., `npm run build`) # Final image (minimal base) FROM node:16-alpine WORKDIR /app COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/package*.json . COPY --from=builder /app/src ./src # Copy only the source files CMD ["node", "src/index.js"] # Replace with your entrypoint ``` **Explanation:** - The build stage compiles the app using npm/yarn and outputs the result. - The final image copies the compiled files (node_modules, package.json, source files) from the build stage, ensuring the app is present in the final image. - Use `npm run build` if your app requires a build step (e.g., React/Vue projects). Adjust paths (e.g., `src` directory) according to your project structure.
Deploy on DigitalOcean ($200 Credit)

Related Fixes

[StackOverflow/docker] How to checkout a branch when all branches are already checked out at '<current_working_tree>'
[golang/go] crypto/internal/fips140test:exe_external: unrecognized failures
[StackOverflow/kubernetes] Airflow Kubernetes Pods Exception ERROR - (404) Reason: Not Found