最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I am getting binsh: [npm: not found error while executing the react image in Docker - Stack Overflow

programmeradmin9浏览0评论

I have added Dockerfile in the react project and run this mand docker build . -t *image name*.My build was success.But when I start to run my image (docker run *imagename*) I got this error /bin/sh: [npm: not found.I have attached my Dockerfile details below:

FROM node:alpine
WORKDIR '/app'

COPY package.json .
RUN npm install
COPY . .
CMD ["npm" "start"]

Anyone help me with this error?

I have added Dockerfile in the react project and run this mand docker build . -t *image name*.My build was success.But when I start to run my image (docker run *imagename*) I got this error /bin/sh: [npm: not found.I have attached my Dockerfile details below:

FROM node:alpine
WORKDIR '/app'

COPY package.json .
RUN npm install
COPY . .
CMD ["npm" "start"]

Anyone help me with this error?

Share Improve this question asked Sep 4, 2020 at 15:41 DineshDinesh 251 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

I would revise the Dockerfile with the following:

FROM node:alpine
WORKDIR /app

COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "start"]

In case this helps anyone else,

FROM node:15.13-alpine

WORKDIR /code/

#mapping environment path for node modules
ENV PATH="./node_modules/.bin:$PATH"

COPY package.json yarn.lock /code/
# COPY package-lock.json /code/

RUN npm install

#adding the rest of the client code 
COPY . /code/

EXPOSE 3000

CMD ["npm", "start"]

All i had to do to solve this error was change the first line. Change WORKDIR /code/ to /code

The accepted answer covers it but it is easy to miss, as the answer mainly focuses on changing the last line which was not the case for me.

I had the same isssue, then followed the node js document and updated my Dockerfile and problem resolved;

Before:

FROM node:16-alpine

COPY . . 
RUN npm i

CMD ['npm', 'start']

After:

FROM node:16-alpine

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm ci --only=production

COPY . . 
EXPOSE 8080
CMD ['npm', 'start']

# or CMD ['node', 'server.js']
# both works
发布评论

评论列表(0)

  1. 暂无评论