I'm looking for a way to achieve these goals at the same time:
node_modules
inside container (to not to "pollute" the working directory on the host)Dockerfile
I'm not sure if these goals are considered "best practice". For example, keeping node_modules
inside the container has its disadvantages.
Currently my compose file is like this:
services:
# ...
node:
image: "node:9"
user: "node"
working_dir: /home/node/app
environment:
# - NODE_ENV=production
- NPM_CONFIG_PREFIX=/home/node/.npm-global
- PATH=$PATH:/home/node/.npm-global/bin
volumes:
- ./proj/:/home/node/app
- /home/node/app/node_modules # mark1
ports:
- "3001:3001"
command: >
bash -c "echo hello
&& ls -lh /home/node/app/
&& npm install
&& npm i -g babel-cli
&& npm i -g flow-bin
&& npm start"
depends_on:
- redis
but there's
"Error: EACCES: permission denied, access
'/home/node/app/node_modules'".
If I comment out the #mark1
line, the container runs, however node_modules
will be written onto the host (since ./proj
is mounted)
I have read these two articles on the topic:
but neither meets my goal.
I added a line of ls -lh /home/node/app/
and found node_modules
is owned by root
. This could be the problem.
yml. When I run docker-compose build , everything works as expected and all npm modules are installed in /worker/node_modules as I'd expect. Turns out none of the modules are present in /worker/node_modules (on host or in the container). If on the host, I npm install , then everything works just fine.
By default that Unix socket is owned by the user root and other users can only access it using sudo. The Docker daemon always runs as the root user .
I ended up using a Dockerfile
. It's minimum. (I keep some commented out lines for anyone may find them useful.)
We need to change the owner of node_modules
inside the container. It seems the (update: Sorry. I forgot to remove the built container with node:9
image doesn't require this. So this is only for node:9-alpine
.docker system prune
. Both images need this. Here is a discussion on permissions/ownership of named volumes`)
FROM node:9-alpine
#ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
#ENV PATH=$PATH:/home/node/.npm-global/bin
RUN mkdir -p /home/node/app/node_modules
RUN chown -R node:node /home/node/app
#USER node
#WORKDIR /home/node/app
#RUN npm install --silent --progress=false ; \
# npm i -g babel-cli --silent --progress=false ;\
# npm i -g flow-bin --silent --progress=false
The docker-compose.yml
ended up being like:
services:
# ...
node:
# image: "node:9-alpine"
build: ./proj
user: "node"
working_dir: /home/node/app
environment:
# - NODE_ENV=production
- NPM_CONFIG_PREFIX=/home/node/.npm-global
- PATH=$PATH:/home/node/.npm-global/bin
volumes:
- ./proj/:/home/node/app
- /home/node/app/node_modules/
ports:
- "3006:3001"
command: >
/bin/sh -c "echo hello
&& ls -lh /home/node/app/
&& npm install
&& npm i -g babel-cli
&& npm i -g flow-bin
&& npm start"
depends_on:
- redis
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With