Hello guys,
I am using the latest node image and I am trying to map the .npm folder to another folder in my project root. Unfortunately, the folder in the docker container as well as on the host remains empty. Any ideas?
Dockerfile
FROM node:latest
WORKDIR /root/app
RUN apt-get update && apt-get install nano && \
npm install -g typescript ts-node forever nodemon mocha
ADD package.json package-lock.json ./
RUN npm install
CMD npm start
Excerpt of the docker-compose
version: "3"
services:
app:
build: .
container_name: app
volumes:
- ./.cache:/root/.npm
- ./server:/root/app/server
- ./package-lock.json:/root/app/package-lock.json
You shouldn't run npm or node as root. This might work better:
FROM node:latest
RUN apt-get update && apt-get install nano
USER node
RUN npm install -g typescript ts-node forever nodemon mocha
WORKDIR /home/node/app
ADD package.json package-lock.json ./
RUN npm install
CMD npm start
version: "3"
services:
app:
build: .
container_name: app
volumes:
- ./.cache:/home/node/.npm
- ./server:/home/node/app/server
- ./package-lock.json:/home/node/app/package-lock.json
@LaurentGoderre Thank you for your answer. I am trying this out but I get Error: EACCES: permission denied, access '/usr/local/lib/node_modules'' for the global install of typescript, ...
Is it fine to run it as sudo or what is the recommend way?
UPDATE: Same with the second npm install.
UPDATE 2: When I completely remove the USER command, I also get a EACCES error after a while.
Here's how it works
FROM node:latest
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
RUN apt-get update && apt-get install nano
USER node
RUN mkdir ~/.npm-global \
&& mkdir ~/app \
&& npm install -g typescript ts-node forever nodemon mocha
WORKDIR /home/node/app
ADD package.json package-lock.json ./
RUN npm install
CMD npm start
@chorrell should we document the use of npm global install?
Yeah, that might be a good thing to document.
Thanks for your response. Do I still need to map .npm on the host machine then?
If you want to have access to the npm cache from the npm install, then yes
Though it might not be the same path
oooh, wait a minute.....if you map it then you will loose access to that folder
My use case is to map the folder onto the host in order to allow npm to use caching between rebuilds.
You can't use Docker compose for this use case because the Docker compose mount happens at runtime. I think you would need to copy out the npm cache then copy it back in on build.
I don't think it''s possible to do that as part of the build. You would need to create a temporary container, copy out the npm cache after the build then add a line to your Dockerfile to copy the cache folder
@LaurentGoderre It would be great, if docker-node makes use of the inbuilt cache somehow. But for now, thank you very much guys!
Most helpful comment
Here's how it works