Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'npm install' in a Dockerfile doesn't install any dependencies

It's kind of weird, I didn't see any errors when run docker-compose build but when I access into container, I didn't see node_modules folder, just only package.json and package-lock.json files. Any the logs when I run docker-compose build show normally. For example:

....
added 552 packages from 678 contributors and audited 4007 packages in 11.2s
found 0 vulnerabilities
....

My Dockerfile:

FROM node:10

ENV APP_PATH /app
WORKDIR $APP_PATH

COPY ./app_front/package*.json ./

RUN npm install -g ts-node typescript
RUN npm install

COPY . .

EXPOSE 3000
CMD [ "npm", "run", "start" ]

And docker-compose file

version: "3.5"
services:
    mysql:
        image: mysql:5.7
        container_name: racer_mysql
        restart: always
        environment:
          MYSQL_DATABASE: xxxx
          MYSQL_USER: "xxxx"
          MYSQL_PASSWORD: "xxxx"
          MYSQL_ROOT_PASSWORD: xxxx
        ports:
          - "3306:3306"
        expose:
          - "3306"
        volumes:
          - ./docker/database:/var/lib/mysql
          - ./docker/mysql_init:/docker-entrypoint-initdb.d
    app_front:
        container_name: app_front
        build:
          context: .
          dockerfile: ./docker/app_front/Dockerfile
        volumes:
          - ./app_front:/app_front
        ports:
          - "3000:3000"

Note: But when I add multiple commands in docker-compose, it works. But I want to install packages in Dockerfile, not docker-compose file.

       command:
          - /bin/bash
          - -c
          - |
            npm install
            npm start

My problem is exactly the same with this issue, I have tried, but without success. Any advice is welcomed!

like image 645
Tran B. V. Son Avatar asked Nov 14 '25 18:11

Tran B. V. Son


2 Answers

I think you build your App in /app_front/ folder in build Process then you delete ""overwriting" all the files in that folder by using :

volumes:
          - ./app_front:/app_front

in your compose , I suggest you to remove the volumes section and try again.

like image 97
LinPy Avatar answered Nov 17 '25 09:11

LinPy


What might be happening is COPY . . is overwriting your node_modules directory in the image. Create a .dockerignore file in the same directory as your Dockerfile and ignore node_modules.

like image 20
peterevans Avatar answered Nov 17 '25 09:11

peterevans