简体   繁体   中英

Docker-compose up : no such file or directory, open '/usr/src/app/package.json'

I'm using docker and docker-compose to run my express nodejs api.

Here is my docker file:

FROM node:10-alpine

ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

RUN chmod 755 /usr/src/app

CMD [ "npm", "start" ]

And as I mentioned I'm using docker-compose, here is the docker-compose.yml file content:

version: "3"

services:
  service:
    build: .
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - 3001:3001
    command: npm start

After running docker-compose up, I'm facing an error says it's not able to find package.json.

Here is the error:

service_1  | npm ERR! path /usr/src/app/package.json 
service_1  | npm ERR! code ENOENT 
service_1  | npm ERR! errno -2 service_1  | npm ERR! syscall open 
service_1  | npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json' 
service_1  | npm ERR! enoent This is related to npm not being able to find a file. 
service_1  | npm ERR! enoent 
service_1  | 
service_1  | npm ERR! A complete log of this run can be found in: 
service_1  | npm ERR! /root/.npm/_logs/2019-04-17T07_54_07_773Z-debug.log
xuser-api_service_1 exited with code 254

Please help to find my mistake.

your working directory is /usr/src/app and you copied the package file on root directory. you have to something like this

# set working directory 
WORKDIR /usr/src/app

# install node_modules
ADD package.json /usr/src/app/package.json
RUN npm install

# copy codebase to docker codebase
ADD . /usr/src/app

you may be using an old image which does not contain latest changes. make sure you using the latest image of your docker file.

docker-compose build 

then run

docker-compose up

if you doing frequent changes to Dockerfile for testing then use.

docker-compose up --build

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM