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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.