Ming
Ming

Reputation: 1602

docker and docker-compose node api: npm ERR! code EACCES

HELLO I have the following folders structure

main

|- .docker-compose.yml
|- backend/
   |- micro-hr/
      |-.dockerfile
   |- rabbitmq

my docker compose:

 micro-hr:
   build: 
     context: .
     dockerfile: backend/micro-hr/Dockerfile
  entrypoint: /usr/src/api/.docker/entrypoint.sh
  container_name: micro-hr
  environment:
    - CHOKIDAR_USEPOLLING=true
  ports:
  - 3001:3000

my dockerfile:

FROM node:lts-alpine
#create app directory
#xx
RUN mkdir /usr/src
RUN apk add --no-cache bash git
RUN touch /usr/src/.bashrc | echo "PS1='\w\$ '" >> /usr/src/.bashrc
#copy files
COPY backend/micro-hr/ormconfig.ts backend/micro-hr/.env backend/micro-hr/package.json backend/micro-hr/yarn.* /usr/src/api/
COPY backend/rabbitmq/package.json /usr/src/rabbitmq/
#install modules lib modules
RUN cd /usr/src/api/ && npm install
#copy other files
COPY backend/micro-hr/ /usr/src/api/
COPY backend/rabbitmq /usr/src/rabbitmq

RUN chown -R node:node /usr/src/api
RUN chown -R node:node /usr/src/rabbitmq
WORKDIR /usr/src/api

#set work dir
USER node

my entrypoint.sh:

#!/bin/bash

npm config set cache /usr/src/api/.npm-cache --global

cd /usr/src/api

npm install
npm run start:dev

i got this error on docker-compose up --build:

micro-hr              | > [email protected] postinstall /usr/src/api
micro-hr              | > npm link ../rabbitmq/
micro-hr              | 
micro-hr              | npm ERR! code EACCES
micro-hr              | npm ERR! syscall symlink
micro-hr              | npm ERR! path /usr/src/rabbitmq
micro-hr              | npm ERR! dest /usr/local/lib/node_modules/rabbitmq
micro-hr              | npm ERR! errno -13
micro-hr              | npm ERR! Error: EACCES: permission denied, symlink '/usr/src/rabbitmq' -> '/usr/local/lib/node_modules/rabbitmq'
micro-hr              | npm ERR!  [Error: EACCES: permission denied, symlink '/usr/src/rabbitmq' -> '/usr/local/lib/node_modules/rabbitmq'] {
micro-hr              | npm ERR!   errno: -13,
micro-hr              | npm ERR!   code: 'EACCES',
micro-hr              | npm ERR!   syscall: 'symlink',
micro-hr              | npm ERR!   path: '/usr/src/rabbitmq',
micro-hr              | npm ERR!   dest: '/usr/local/lib/node_modules/rabbitmq'
micro-hr              | npm ERR! }

and:

micro-hr | npm WARN checkPermissions Missing write access to /usr/src/api/node_modules/has-symbols micro-hr | npm WARN checkPermissions Missing write access to /usr/src/api/node_modules/highlight.js

I know it's a problem with permission but I can't figure out how to fix it. the problem is when installing my npm link modules:

"postinstall": "npm link ../rabbitmq/",
"postupdate": "npm link ../rabbitmq/",

Upvotes: 0

Views: 5538

Answers (1)

Konrad Botor
Konrad Botor

Reputation: 5043

I think the problem is running npm install as root.

Try

USER node
RUN cd /usr/src/api/ && npm install
#copy other files
COPY --chown node:node backend/micro-hr/ /usr/src/api/
COPY --chown node:node backend/rabbitmq /usr/src/rabbitmq
WORKDIR /usr/src/api

instead of

RUN cd /usr/src/api/ && npm install
#copy other files
COPY backend/micro-hr/ /usr/src/api/
COPY backend/rabbitmq /usr/src/rabbitmq

RUN chown -R node:node /usr/src/api
RUN chown -R node:node /usr/src/rabbitmq
WORKDIR /usr/src/api

#set work dir
USER node

Based on this article I would modify my previous suggestion to:

USER node
RUN mkdir ~/.npm-global && npm config set prefix '~/.npm-global' && \
    export PATH=~/.npm-global/bin:$PATH && \
    cd /usr/src/api/ && npm install
#copy other files
ENV PATH="~/.npm-global/bin:${PATH}"
COPY --chown node:node backend/micro-hr/ /usr/src/api/
COPY --chown node:node backend/rabbitmq /usr/src/rabbitmq
WORKDIR /usr/src/api

Edit:

I think this line in entrypoint.sh may cause the new problem:

npm config set cache /usr/src/api/.npm-cache --global

Try removing or commenting it out and rebuilding the image.

Upvotes: 3

Related Questions