Home > Net >  Docker Compose couldn't find a package.json file in "/app"
Docker Compose couldn't find a package.json file in "/app"

Time:11-15

Here is my project struture

.
├── README.md
├── docker-compose.yml
└── frontend
    ├── Dockerfile
    ├── README.md
    ├── build
    ├── package.json
    ├── yarn.lock
    └── ...

Docker-compose.yml

version: '3.7'

services:
  frontend:
    build:
      context: frontend
      dockerfile: Dockerfile
    volumes:
      - '.:/app'
      - '/app/node_modules'
    ports:
      - 3000:3000

Dockerfile

FROM node:14-alpine

WORKDIR /app

COPY ./package.json /app/package.json
RUN yarn install --no-lockfile

COPY . .

CMD ["yarn", "start"]

If i build container using docker, it works fine

But docker-compose up --build always returns

Attaching to book-marketplace_frontend_1
frontend_1  | yarn run v1.22.15
frontend_1  | error Couldn't find a package.json file in "/app"
frontend_1  | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
book-marketplace_frontend_1 exited with code 1

it's weird that docker-compose can still run yarn install ( which needs package.json) but can not locate it later. Hope someone can help me

CodePudding user response:

Because you are already in workdir app as you earlier defined as WORKDIR. So change the line to:

COPY ./package.json /package.json

CodePudding user response:

Your volumes: block is replacing the /app directory in the container with something totally different. (You're using a frontend subdirectory as the build context, but then bind-mounting . over /app; if you docker-compose run frontend ls you'll see a frontend subdirectory and not your application.)

You can resolve this by deleting the volumes: block. Your container will run the code that's built into the image, and not something else. A minimal functional Compose setup can look like

version: '3.8'
services:
  frontend:
    build: frontend
    ports:
      - '3000:3000'
  • Related