hoang
hoang

Reputation: 45

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

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

Upvotes: 4

Views: 8946

Answers (5)

Adam
Adam

Reputation: 1755

I had a very similar config to what the OP has and ran into the same error. Several people have correctly identified that the problem is with how the volumes are configured but no one has answered with a solution that would actually fix the volumes mapping for the OP's config.

The issue is this mapping:

volumes:
  - ./:/app

That's mapping the top-level directory, where the docker-compose.yml file is, to the /app directory inside the container. This means that when the container runs there will just be the frontend/ subfolder inside the /app directory of the container instead of the project files. The error happens because yarn now can't find package.json in the /app directory since it's inside of app/frontend.

What we want to do instead is map the frontend directory to /app. This is what the entire volumes section should look like:

volumes:
  - ./frontend:/app
  - '/app/node_modules'

Upvotes: 0

Akshay Vijay Jain
Akshay Vijay Jain

Reputation: 15935

I spent 2 days on this issue, you need to add working_dir in the docker-compose file as well along with specifying WORKDIR in dockerfile

  api:
    container_name: my_app
    build:
      dockerfile: Dockerfile
      context: ./api
    working_dir: /usr/src/app
FROM node:alpine
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install

Upvotes: 1

randomuser
randomuser

Reputation: 11

or you can change the line in docker-compose.yml, works for me

volumes:
  - ./:/app

to

volumes:
  - ./app:/app

Upvotes: 1

David Maze
David Maze

Reputation: 158758

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'

Upvotes: 2

Robert-Jan Kuyper
Robert-Jan Kuyper

Reputation: 3306

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

COPY ./package.json /package.json

Upvotes: 4

Related Questions