Install Node.js and NPM in Docker Container for Laravel

Install Node.js and NPM in Docker Container for Laravel

Today, we will learn the process of how to install Node.js and NPM in a Docker container while working on our Laravel project. Node.js and NPM are important for the compilation of CSS and JavaScript in Laravel.

We will be building a Docker file and docker-compose, which will provide us with PHP, Composer, Node.js, and NPM, and thus we will be able to manage the back-end and front-end of our Laravel application inside the container.

Laravel Development Service

Steps to Install Node.js and NPM in a Docker Container for Laravel:

Step 1: Create a Laravel Project

laravel new my-app

Step 2: Create Dockerfile

FROM php:8.3-fpm
WORKDIR /var/www/html
RUN apt-get update & apt-get install -y 
libzip-dev 
unzip 
& docker-php-ext-install zip pdo_mysql
#Install NodeJS RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - 
& apt-get install -y nodejs 
& npm install -g npm@latest

Composer Install

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . /var/www/html

Composer Install for Laravel Project

RUN composer install

#Install npm RUN npm install & npm run build

RUN chown -R www-data:www-data /var/www/html RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache /var/www/html/vendor RUN chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache /var/www/html/vendor

Step 3: Create NGINX Config File

.docker/apache/default.conf 

server { 
    listen 80; 
    index index.php index.html; 
    root /var/www/html/public;
    location / { 
        try_files $uri $uri/ /index.php?$query_string; 
    }

    location ~ .php$ { 
        include fastcgi_params;
        fastcgi_pass web:9000; 
        fastcgi_index index.php; 
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; 
        fastcgi_param PATH_INFO $fastcgi_path_info; 
        fastcgi_buffers 16 16k; 
        fastcgi_buffer_size 32k; 
    }
    location ~ /\.ht {
        deny all;
    }
}

Step 4: Create docker-compose.yml

docker-compose.yml
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    image: my-laravel-app
    volumes:
      - .:/var/www/html
      - /var/www/html/storage
      - /var/www/html/bootstrap/cache
      - /var/www/html/vendor
    working_dir: /var/www/html
    user: "www-data"
    depends_on:
      - db
nginx:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:  
      - .:/var/www/html
      - ./.docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - web
db:
  image: mysql:8.0
 environment: 
 MYSQL_ROOT_PASSWORD: root 
 MYSQL_DATABASE: laravel 
 MYSQL_USER: laravel 
 MYSQL_PASSWORD: secret 
ports: - 
 "3307:3306" 
volumes: - dbdata:/var/lib/mysql
volumes:
 dbdata:

Run Laravel App:

All the required steps have been done. Now, you have to type the command given below and hit enter to run the Laravel app:

php artisan serve

Now, go to your web browser, type the given URL, and view the app output:

Read Also: Laravel Docker #8 – Install Composer in Docker Container

http://localhost:8080

Conclusion

Installing Node.js and NPM inside your Laravel Docker container provides a complete development environment for both backend and frontend tasks. With PHP, Composer, Node.js, and NPM configured in a single Docker setup, you can easily install dependencies, compile assets using Vite, and manage your Laravel application without installing these tools directly on your local machine.

Once your Dockerfile, Nginx configuration, and Docker Compose file are ready, start the containers using Docker Compose. Once the services are up and running, your Laravel application will be up and running at http://localhost:8080, and you can work on your project conveniently and consistently across multiple devices.

FAQ

1. Why do I need Node.js and NPM for a Laravel project?

Laravel requires Node.js and NPM because this framework makes use of these to install front-end dependencies and compile CSS and JavaScript assets through Vite or Laravel Mix.

2. Which version of Node.js should I install while working on Laravel?

The Node LTS versions can be used with Laravel without any issues. In this tutorial, we will be utilizing Node.js version 20 because it is commonly used for Laravel projects.

Previous Article

The Real Cost of Maintaining a Magento or Shopify Store as You Scale

Next Article

How to Start a Magento Store From Scratch in 2026 

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨