How to setup laravel with npm using docker-compose?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Setup Laravel with NPM using Docker Compose: Mastering Frontend Asset Compilation
Setting up a modern web application, especially one that relies on frontend assets compiled via Node Package Manager (NPM), within a containerized environment like Docker Compose requires careful orchestration. Many developers running projects inspired by frameworks like Laravel find themselves hitting roadblocks when trying to execute `npm install` or asset compilation commands inside their service containers.
This post will walk you through the common pitfalls and provide a robust, practical solution for integrating NPM access into your Docker setup using Docker Compose, ensuring you can successfully build your JavaScript, CSS, and copy necessary dependencies from `node_modules`.
## The Challenge: Accessing NPM in a Container
You have provided a setup where you are trying to run a PHP/Apache environment (`web` service) but need the capabilities of a Node/NPM environment to handle frontend compilation. Your attempt to extend the `web.dockerfile` with an npm image failed because simply adding another base image doesn't automatically grant shell access or the necessary NPM binaries within the running container context for execution.
The core issue is not just installing the package manager, but ensuring that the build process *executes* the commands in an environment where those tools are available and correctly configured.
## The Solution: Building the Environment Correctly
To solve this, we need to ensure that the `web` service container has both the PHP environment *and* the necessary Node/NPM tooling installed as part of its build process or accessible at runtime. We will focus on using a robust base image and layering the required tools correctly.
### Step 1: Selecting the Right Base Image
Instead of attempting to stack two separate PHP images, we should choose an image that is already tailored for development environments, or explicitly install the necessary components within a standard base. For this scenario, combining PHP necessities with Node/NPM utilities is key.
We will modify your `web.dockerfile` to ensure Node and NPM are installed alongside the existing PHP dependencies.
### Step 2: Implementing Multi-Stage Build for Efficiency (Best Practice)
While we can install everything in one place, a cleaner, more efficient approach often involves using multi-stage builds. This separates the *build* environment (where you run `npm install`) from the final *runtime* environment (where your PHP application actually runs).
Since your current setup seems geared toward running PHP/Apache directly, we will focus on ensuring the necessary tools are present in the final image used by the `web` service.
### Updated Dockerfile Example (`web.dockerfile`)
We will start with a stable Ubuntu base and install Node.js and NPM explicitly. Note that you must adjust this based on your specific PHP version requirements, but for modern development environments, this approach is highly effective.
```dockerfile
# Use an appropriate base image that supports both PHP and Node tools
FROM node:16-slim
# Install necessary system dependencies for PHP/Apache if needed later (or stick to a combined base)
RUN apt-get update && apt-get install -y \
libmcrypt-dev \
mysql-client \
&& rm -rf /var/lib/apt/lists/*
# Install PHP dependencies here if you plan to run PHP commands later,
# or switch back to a PHP base if necessary.
# For this specific goal (running npm), we ensure Node is ready.
WORKDIR /var/www/html
# Copy application files (this will be done via volume mounting in docker-compose)
COPY . .
# Optionally, install node modules here if the build context requires it:
# RUN npm install
```
### Step 3: Revisiting `docker-compose.yml`
The key to making this work is ensuring that when you run `docker compose build`, the context correctly executes the commands within the container environment. If your goal is to run NPM *before* copying files, it must be done in a specific build step.
If you are using the standard Laravel setup where `npm install` happens locally, and then you copy the resulting `node_modules` folder into the container via volumes, the process is simpler: **ensure the application code structure correctly places `package.json` and `node_modules` relative to the volume mount.**
For asset compilation within the container, a better pattern for Laravel projects often involves running the NPM commands *inside* the build stage if the resulting assets are needed at runtime.
**If you need to execute `npm install` inside the container:** You must ensure the environment is set up correctly, which usually means using an image that already contains Node/NPM tooling (like the `node` images) rather than trying to layer it onto a PHP image unless you manage cross-language dependencies very carefully.
## Conclusion: Orchestrating Dependencies with Docker
Setting up complex environments in Docker Compose is fundamentally about defining clear, isolated layers for each service. For integrating NPM workflows into your Laravel setup, the solution lies not just in adding an image, but in understanding *which* container needs *which* tools. By ensuring your `web` service has a runtime environment capable of executing Node commandsâeven if it's slightly different from a pure PHP imageâyou successfully manage frontend compilation alongside your backend services.
For deeper dives into structuring complex applications using Docker and Laravel, exploring resources from the official team at [laravelcompany.com](https://laravelcompany.com) is highly recommended for understanding modern deployment patterns. By mastering these container concepts, you gain the power to deploy any application reliably, no matter how complex its dependencies are.