sfarzoso
sfarzoso

Reputation: 1610

node_modules not mounted in docker container

I have dockerized a php application which require some npm dependencies, so I've installed in the Docker container nodejs and the required packages using:

FROM php:8.0.2-fpm-alpine
WORKDIR /var/www/html

RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli
RUN apk add icu-dev

RUN docker-php-ext-configure intl && docker-php-ext-install intl

RUN apk add --update libzip-dev curl-dev &&\
    docker-php-ext-install curl && \
    apk del gcc g++ &&\
    rm -rf /var/cache/apk/*

COPY docker/php-fpm/config/php.ini /usr/local/etc/php/

COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

RUN apk add --update nodejs nodejs-npm
RUN npm install gulp-cli -g
RUN npm install

COPY src src/

CMD ["php-fpm"]

EXPOSE 9000

this is my docker-compose.yml:

version: '3.7'

services:
  php-fpm:
    container_name: boilerplate_app
    restart: always
    build:
      context: .
      dockerfile: ./docker/php-fpm/Dockerfile
    volumes:
      - ./src:/var/www/html

the problem's that when I enter in the container using: docker exec -ti boilerplate_app sh

and launch that command: ls -la I can't see any node_modules folder, infact, if I try to execute the installed dependency gulp I get:

Local modules not found in /var/www/html Try running: npm install

What I did wrong?

Upvotes: 0

Views: 633

Answers (1)

Simone Ripamonti
Simone Ripamonti

Reputation: 322

There are two issues:

  1. You are running npm install in a folder that does not contain any package.json listing the required node modules to install. If you inspect the logs you should see something like
no such file or directory, open '/var/www/html/package.json'
  1. Moreover, when you mount your local src folder, you are replacing the content of /var/www/html/ with src content, which might not include any node_modules folder
    volumes:
      - ./src:/var/www/html

Upvotes: 1

Related Questions