Schovi Schovi - 1 month ago 18
Node.js Question

Docker - Override content of linked volume

Having simple Node.js docker container

docker-compose.yml:

app:
build: ./dockerfiles/app
volumes:
- /Users/home/work/app:/usr/app


Dockerfile:

FROM node:6.7-slim

COPY package.json /tmp
RUN cd /tmp && npm install

RUN mkdir -p /usr/app
WORKDIR /usr/app

CMD ["node", "./src/app.js"]


What I want to achieve is container where I have package.json and installed node modules (npm install). Part where I copy package.json and install modules inside container is pretty straighforward, but problem occur, when I want to use these node_modules inside linked app. I can't find any way, how to copy /tmp/node_modules into /usr/app/node_modules

Is there any Docker way ho to do that? If not, can I tell my node app to look for node_modules somewhere else than in root directory?

Answer

Thing that helped me is following usage of volumes

volumes:
  - ${APP_PATH}:/usr/app
  # Empty node_modules directory
  - /usr/app/node_modules

Then in Dockerfile:

FROM node

# Node modules
COPY *.json /tmp/
RUN cd /tmp && yarn
ENV NODE_PATH /tmp/node_modules:${NODE_PATH}

# App
RUN mkdir -p /usr/app
WORKDIR /usr/app
ENV NODE_ENV docker

CMD ["run-node", "src/app.js"]

This allow me to have node_modules in another directory and app will look for them there.