How can I run an npm command in a docker container?

I am trying to run an angular application in development mode inside a docker container, but when i run it with docker-compose build it works correctly but when i try to put up the container i obtain the below error:

ERROR: for sypgod  Cannot start service sypgod: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"npm\": executable file not found in $PATH

The real problem is that it doesn’t recognize the command npm serve, but why??

The setup would be below:

Docker container (Nginx Reverse proxy -> Angular running in port 4000)

I know that there are better ways of deploying this but at this moment I need this setup for some personals reasons


FROM node:10.9 

COPY package.json package-lock.json ./

RUN npm ci && mkdir /angular && mv ./node_modules ./angular

WORKDIR /angular

RUN npm install -g @angular/cli 

COPY . . 

FROM nginx:alpine
COPY toborFront.conf /etc/nginx/conf.d/
CMD ["nginx", "-g", "daemon off;"]
CMD ["npm", "serve", "--port 4000"]


    listen 80;
    server_name sypgod;

    location / {
            proxy_read_timeout 5m;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://localhost:4000/;



Docker Compose file(the important part where I have the problem)

 sypgod: # The name of the service
        container_name: sypgod  # Container name
            context: ../angular
            dockerfile: Dockerfile # Location of our Dockerfile

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The image that’s finally getting run is this:

FROM nginx:alpine
COPY toborFront.conf /etc/nginx/conf.d/
CMD ["npm", "serve", "--port 4000"]

The first stage doesn’t have any effect (you could COPY --from=... files out of it), and if there are multiple CMDs, only the last one has an effect. Since you’re running this in a plain nginx image, there’s no npm command, leading to the error you see.

I’d recommend using Node on the host for a live development environment. When you’ve built and tested your application and are looking to deploy it, then use Docker if that’s appropriate. In your Dockerfile, run ng build in the first stage to compile the application to static files, add a COPY --from=... in the second stage to get the built application into the Nginx image, and delete all the CMD lines (nginx has an appropriate default CMD). @VikramJakhar’s answer has a more complete Dockerfile showing this.

It looks like you might be trying to run both Nginx and the Angular development server in Docker. If that’s your goal, you need to run these in two separate containers. To do this:

  • Split this Dockerfile into two. Put the CMD ["npm", "serve"] line at the end of the first (Angular-only) Dockerfile.
  • Add a second block in the docker-compose.yml file to run the second container. The backend npm serve container doesn’t need to publish ports:.
  • Change the host name of the backend server in the Nginx config from localhost to the Docker Compose name of the other container.

Solution 2

It would appear the npm can’t be accessed from the container.
Try defining where it tries to execute it from:

docker run -v "$PWD":/usr/src/app -w /usr/src/app node:10.9 npm serve --port 4000


Also make sure that npm is installed on the computer running the docker container.

Solution 3

You can do something like below

### STAGE 1: Build ###

# We label our stage as ‘builder’
FROM node:alpine as builder

RUN apk --no-cache --virtual build-dependencies add \
    git \
    python \
    make \

    RUN mkdir -p /ng-app/dist

    WORKDIR /ng-app

    COPY package.json package-lock.json ./

    ## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
    RUN npm install

    COPY . .

    ## Build the angular app in production mode and store the artifacts in dist folder

    RUN npm run ng build -- --prod --output-path=dist

    ### STAGE 2: Setup ###

    FROM nginx:1.14.1-alpine

    ## Copy our default nginx config
    COPY toborFront.conf /etc/nginx/conf.d/

    ## Remove default nginx website
    RUN rm -rf "/usr/share/nginx/html/*"

    ## From ‘builder’ stage copy over the artifacts in dist folder to default nginx public folder
    COPY --from=builder /ng-app/dist /usr/share/nginx/html

    CMD ["nginx", "-g", "daemon off;"]

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply