Trying to build my dockerfile, and getting a permission denied error.
The project is a nest.js server. Here is the dockerfile:
FROM node:12.13-alpine
WORKDIR /usr/src/app
COPY package.json .
RUN npm install <<< this fails
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "run", "start:prod"]
and the error (on npm install) is:
internal/fs/utils.js:220
throw err;
^
Error: EACCES: permission denied, open '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
at Object.openSync (fs.js:440:3)
at Object.readFileSync (fs.js:342:35)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:994:22)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11 {
errno: -13,
syscall: 'open',
code: 'EACCES',
path: '/usr/local/lib/node_modules/npm/bin/npm-cli.js'
Any idea?
Solved. the solution was in 2 steps:
re-install docker.
changing owner to node, like this:
FROM node:10
RUN mkdir -p /home/node/app && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package.json .
USER node
RUN npm install
...
Related
I am using yarn berry on next.js.
and i need to reduce docker image size for this build,
so i converted to standalone build on next.js.
this is my Dockerfile.
FROM node:16-alpine as builder
WORKDIR /app
COPY package.json ./
COPY yarn.lock ./
COPY .pnp.cjs ./
COPY .pnp.loader.mjs ./
COPY .yarnrc.yml ./
COPY .yarn .yarn
RUN yarn install --immutable
COPY . .
ARG GITHUB_SHA=0
RUN yarn build
FROM node:16-alpine AS runner
WORKDIR /app
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
ENV NODE_ENV='production'
CMD node server.js
it can build to command docker build -t standalone .
then i can get the output image.
when i run this docker image with command docker run -d -p3000:3000 standalone.
I'm getting this error.
node:internal/modules/cjs/loader:988
throw err;
^
Error: Cannot find module 'next/dist/server/next-server'
Require stack:
- /app/server.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Module.require (node:internal/modules/cjs/loader:1057:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/app/server.js:4:20)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/app/server.js' ]
}
CMD ["node", "-r", "./.pnp.cjs", "server.js"]
https://yarnpkg.com/features/pnp#initializing-pnp
I'm trying to run an react project with a Docker.
I build a docker's image, but when i run it I've got the following message:
> react#1.0.0 start /app
> react-scripts start
Could not find a required file.
Name: index.html
Searched in: /app/public
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! react#1.0.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the react#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-09-13T13_09_01_107Z-debug.log
I don't understand the error, but i do have a index.html file at ./public/index.html
Here is my Dockerfile:
FROM node:14-alpine
WORKDIR /app
COPY . /app
RUN npm install
COPY . app/
EXPOSE 3090
CMD ["npm", "start"]
EDIT:
I changed my Dockerfile for the following:
FROM node:14-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3090
CMD ["npm", "start"]
RUN ls -la /app
RUN ls -la /app/public
Now i Have the error:
=> ERROR [7/7] RUN ls -la /app/public 0.4s
------
> [7/7] RUN ls -la /app/public:
#11 0.360 ls: /app/public: No such file or directory
------
executor failed running [/bin/sh -c ls -la /app/public]: exit code: 1
I guess it's because i didn't had copy, but i still have the error even if I had the following line :
COPY ./public /app
You could temporarily add a RUN ls -la /app or RUN ls -la /app/public to your Dockerfile. This allows you to see all the directories/files copied into the container.
There is also a possibility that a dockerignore file ignores the files you want to copy.
How do you add items to .dockerignore?
I have written a multi-stage dockerfile with pm-2 docker, but when I am trying to run pod with my image, pod crashes,
Dockerfile
FROM node:16.15.1-alpine3.16 as build
ENV NODE_TLS_REJECT_UNAUTHORIZED=0
# Create app directory
WORKDIR /app
ENV NODE_OPTIONS --max-old-space-size=4096
# Install app dependencies
COPY package.json .
RUN apk update && apk add vim jq busybox-extras
RUN npm install pm2 -g
RUN npm install --save-exact
COPY . .
FROM node:16.15.1-alpine3.16 as main
COPY --from=build /app /
CMD ["pm2-docker", "index.js"]
Error
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '/pm2-docker'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
multi-stage dockerfile written is correct?
I just faced this issue and like #Dr Claw said, adding npm install -g pm2 in the main image after COPY did the trick. I removed npm install -g pm2 from the build image
I'm running a very simple Node.js Koa "hello world" application on Docker 2.4.0 on Windows. If the base image is node:10-alpine, it works well:
FROM node:10-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 8080
CMD [ "node", "index.js" ]
But if I change the version to FROM node:15.0.1-alpine3.10
Step 6/9 : RUN npm install
Removing intermediate container 6ff212db1484
---> bfde467f103d
Step 6/9 : RUN npm install
---> Running in 37ef90ef4039
npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /home/node/app/package-lock.json
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, open '/home/node/app/package-lock.json'
npm ERR! [Error: EACCES: permission denied, open '/home/node/app/package-lock.json'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/home/node/app/package-lock.json'
npm ERR! }
........
npm install will try to update the package-lock.json file with the current actual versions of the packages it's installed. For installation in a Docker image, you don't really want this; use npm ci instead to avoid this.
The other problem you're encountering is that COPY by default makes files owned by the root user, but you've switched to an alternate "node" user. You probably want the application code in your image to be owned by root, and then run as an alternate user: if there's some sort of security issue, this gives you an extra layer of protection against the code in the container getting modified.
If you do both of these things, the corrected Dockerfile would roughly look like:
FROM node:15.0.1-alpine3.10
# WORKDIR also creates the directory. It will be owned by root,
# which is probably what you want. (So no `RUN mkdir ...`.)
WORKDIR /home/node/app
# Stay as the root user for now.
# Install packages:
COPY package*.json ./
RUN npm ci # not `npm install`
# Copy in the rest of the application (still owned by root):
COPY . .
# Declare runtime metadata. Only now switch to the "node" user.
# This will not be able to modify the source code (good).
USER node
EXPOSE 8080
CMD ["node", "index.js"]
I have a dockerfile setup
FROM node:8.10.0
USER node
RUN mkdir /home/node/.npm-global
ENV PATH=/home/node/.npm-global/bin:$PATH
ENV NPM_CONFIG_PREFIX=/home/node.npm-global
RUN npm install -g #angular/cli#1.6.8 ts-node#5.0.1
WORKDIR /opt/src
COPY . .
USER 0
RUN chown -R root /opt/src
RUN npm install
CMD ["npm", "run", "build:prod"]
this runs fine locally, but when I try to deploy it, seems like the chown command didn't work, as it errors on npm install and shows
Error: EACCES: permission denied, open '/opt/src/node_modules/nodent-runtime/dist/index.js'
what could be wrong?