What might be the problem here…
- User
What might be the problem here "C:\Users\alexandru.ursachianu\Desktop\DataBrew\data-brew\frontend>podman build -t databrew-fe:01-10-2024 . [1/2] STEP 1/6: FROM node:20-alpine AS build Resolved "node" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf) Trying to pull docker.io/library/node:20-alpine... Getting image source signatures Copying blob sha256:737da86e5e86be8d490b910c63f7a45bc0a97913a42ff71d396c801c8bf06a58 Copying blob sha256:43c4264eed91be63b206e17d93e75256a6097070ce643c5e8f0379998b44f170 Copying blob sha256:c36270121b0cfc4d7ddaf49f4711e7bcad4cf5e364694d5ff4bd5b65b01ed571 Copying blob sha256:842cd80f9366843dd6175d28abb6233501045711892a1a5a700676a0ed2d6ec4 Copying config sha256:45a59611ca84e7fa7e39413f7a657afd43e2b71b8d6cb3cb722d97ffc842decb Writing manifest to image destination [1/2] STEP 2/6: WORKDIR /usr/src/app --> 85572cfd4760 [1/2] STEP 3/6: COPY package*.json ./ --> 1bfbb22683b9 [1/2] STEP 4/6: RUN npm install
added 1068 packages, and audited 1069 packages in 15s
148 packages are looking for funding run
npm fundfor details16 vulnerabilities (1 low, 7 moderate, 8 high)
To address issues that do not require attention, run: npm audit fix
To address all issues, run: npm audit fix --force
Run
npm auditfor details. npm notice npm notice New patch version of npm available! 10.8.2 -> 10.8.3 npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.3 npm notice To update run: npm install -g npm@10.8.3 npm notice --> 9b37a6b635d7 [1/2] STEP 5/6: COPY . . --> 9c41b0be127e [1/2] STEP 6/6: RUN npm run builddata-brew@0.0.0 build ng build
sh: ng: Permission denied Error: building at STEP "RUN npm run build": while running runtime: exit status 126". Here's the Containerfile "# Build stage FROM node:20-alpine as build WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . RUN npm run build
Run stage
FROM node:20-alpine WORKDIR /usr/src/app COPY --from=build /usr/src/app/dist /usr/src/app/dist COPY --from=build /usr/src/app/package*.json ./ RUN npm install --only=production EXPOSE 4000 ENV PORT=4000 CMD ["node", "dist/data-brew/server/server.mjs"] "
- Assistant
The issue you're encountering occurs during the
RUN npm run buildstep in your Dockerfile (Containerfile). Specifically, the error message is:> ng build sh: ng: Permission denied Error: building at STEP "RUN npm run build": while running runtime: exit status 126This error indicates that the shell cannot execute the
ngcommand due to a "Permission denied" issue. Here's a detailed explanation of the problem and how to fix it.Problem Explanation
1. Permissions in Alpine Linux
You're using the
node:20-alpineimage as your base image for both the build and run stages. Alpine Linux, while lightweight, uses a different file system and has stricter permissions compared to other base images like Debian-based ones. When you install packages usingnpmin Alpine, the execution permissions for scripts innode_modules/.binmay not be set correctly.2. The
ngCommand Is Not ExecutableThe
ngcommand (Angular CLI) is likely installed locally in your project undernode_modules/.bin/ng. Whennpm run buildtries to executeng build, it fails because the script does not have execute permissions. This is why the shell throws a "Permission denied" error.3. Line Endings Issue
Additionally, if your host system is Windows, there might be a line ending issue. Scripts copied into the Docker context might have Windows-style line endings (
\r\n) instead of Unix-style (\n), which can cause the shebang line (#!/usr/bin/env node) in thengscript to be misinterpreted, leading to a permission error.Solution
Option 1: Adjust File Permissions
Add a
chmodcommand in your Dockerfile to set the correct execute permissions on thengscript before runningnpm run build:# Build stage FROM node:20-alpine as build WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . RUN chmod +x node_modules/.bin/ng # Add this line RUN npm run buildThis ensures that the
ngscript is executable.Option 2: Ensure Correct Line Endings
Install
dos2unixto convert line endings of scripts innode_modules/.bin:# Build stage FROM node:20-alpine as build WORKDIR /usr/src/app RUN apk add --no-cache dos2unix # Install dos2unix COPY package*.json ./ RUN npm install COPY . . RUN find node_modules/.bin -type f -exec dos2unix {} \; # Convert line endings RUN npm run buildThis converts any Windows-style line endings to Unix-style, preventing potential shebang misinterpretation.
Option 3: Use the Full Node Image Instead of Alpine
Switching from the Alpine-based image to the full Node.js image (Debian-based) can resolve permissions issues and line ending problems because it handles permissions differently and doesn't have the same issues with line endings:
# Build stage FROM node:20 as build # Use the full Node.js image WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . RUN npm run build # Run stage FROM node:20 # Use the full Node.js image WORKDIR /usr/src/app COPY --from=build /usr/src/app/dist /usr/src/app/dist COPY --from=build /usr/src/app/package*.json ./ RUN npm install --only=production EXPOSE 4000 ENV PORT=4000 CMD ["node", "dist/data-brew/server/server.mjs"]The full Node.js image is based on Debian and doesn't have the strict permissions and line ending issues that Alpine does.
Option 4: Use
npxor Modify the Build ScriptModify your
package.jsonbuild script to usenpx, which executes binaries fromnode_modules/.bin:{ "scripts": { "build": "npx ng build" } }Or call
npx ng builddirectly in the Dockerfile:RUN npx ng buildThis avoids the permission issue because
npxcorrectly handles the execution of local binaries.Recommendation
Use Option 3: Switch to the full Node.js image (
node:20) instead of the Alpine-based image. This is the most straightforward solution and avoids permission and line ending issues inherent to the Alpine image.Updated Dockerfile:
# Build stage FROM node:20 as build # Use the full Node.js image WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . RUN npm run build # Run stage FROM node:20 # Use the full Node.js image WORKDIR /usr/src/app COPY --from=build /usr/src/app/dist /usr/src/app/dist COPY --from=build /usr/src/app/package*.json ./ RUN npm install --only=production EXPOSE 4000 ENV PORT=4000 CMD ["node", "dist/data-brew/server/server.mjs"]By making this change, you should no longer encounter the "Permission denied" error when running
npm run build.Additional Notes
-
Alpine vs. Debian-based Images: While Alpine images are smaller, they can introduce complexities due to their minimalist nature and differences in how they handle libraries and permissions. Unless image size is a critical concern, using Debian-based images can save time and avoid such issues.
-
Best Practices:
- Avoid installing packages globally inside Docker containers.
- Keep your
Dockerfilesimple and only include what's necessary for your application. - Be cautious with file permissions and line endings, especially when working across different operating systems.
-
Troubleshooting Steps:
- Verify file permissions inside the container using
ls -lcommands during the build process. - Check for correct shebang lines in scripts.
- Use tools like
dos2unixto fix line ending issues.
- Verify file permissions inside the container using
In summary, the "Permission denied" error occurs because the
ngscript isn't executable in the Alpine-based image due to stricter permissions handling. Switching to the full Node.js image resolves the issue by providing an environment that properly handles script execution permissions.
References:
-