Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it that when I install a new npm package I have to run "docker-compose down" followed by "docker-compose up --build"?

Shouldn't docker-compose up --build suffice?

Here's our Dockerfile:

FROM node:8.12.0-alpine

# Set app directory
WORKDIR /usr/src/app

# Install git in order to allow installing
# npm packages from Github
RUN set -xe \
    && apk add --no-cache git

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install

# Bundle app source
COPY . .

ENV NODE_ENV development

EXPOSE 8080

CMD ["npm","start"]

And our docker-compose file:

version: '3'
services:

  web:
    build: .
    ports:
      - 8080:8080
    environment:
      MONGODB_URI: mongodb://admin:app@mongo:27017/app?authSource=admin
    volumes:
      - .:/usr/src/app
      # More info on the node_modules volume at:
      # http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html
      # under "The node_modules Volume Trick"
      - /usr/src/app/node_modules
    # Run nodemon with legacy watch
    # https://github.com/remy/nodemon#application-isnt-restarting
    # Ignore public/ because generating documentation programatically
    # on startup was causing an infinite loop
    command: ./node_modules/.bin/nodemon --legacy-watch --inspect=0.0.0.0:5858 --ignore public/
    depends_on:
      - mongo

  mongo:
    image: mongo:4.0
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: app
    volumes:
      # Using a named volume because windows doesn't deal well with mounting local folders
      - app-api-mongodb-storage:/data/db

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: app
    depends_on:
      - mongo

volumes:
  # Database named module has to be declared globally
  app-api-mongodb-storage:

From what I can tell, docker-compose down should delete the node_modules volume, because it is created by docker-compose up.

Is there some kind of caching that is preventing package.json from being refreshed unless docker-compose --build is used in order to explicitly rebuild the Docker image?

like image 331
Marcos Pereira Avatar asked Oct 21 '25 02:10

Marcos Pereira


1 Answers

With your current setup, at image build time you are running npm install which creates and populates a node_modules folder inside the image.

When you start a container from this image, with your current docker-compose.yml file, docker creates a new randomly-named volume, copies the content from the image's node_modules folder into this new volumes, and mounts the volume on the node_modules folder.

Your update to package.json will be visible within the container, because you are mounting your current folder as /usr/src/app (this hides the version packaged in the image).

You haven't mentioned how you are running npm install after updating your package.json file. My guess is that you are running it directly on your host. If correct, this is your problem : it won't work, because your node_modules folder is bound to the docker volume, not your local folder. You need to run npm install inside the container, by doing something like docker-compose exec web npm install.

like image 54
lbndev Avatar answered Oct 22 '25 19:10

lbndev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!