codebarz
codebarz

Reputation: 327

Docker compose cant find package.json in path

I am trying to dockerize my node app but I can't seem to understand why it can't find the package.json file because the package.json file is in the same path as the docker compose and dockerfile. Here is the folder structure:

enter image description here

Below is the dockerfile:

Dockerfile

FROM node:12

# RUN mkdir -p ./usr/src/app/
COPY . /usr/src/app/

WORKDIR /usr/src/app/

# ADD package.json /usr/src/app/package.json
COPY package*.json ./

RUN yarn
RUN yarn global add pm2

COPY . .

EXPOSE 3005
EXPOSE 9200

CMD yarn start:dev

And the docker compose file:

docker-compose.yml

version: "3.6"
services:
  api:
    image: node:12.13.0-alpine
    container_name: cont
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 3005:3005
    environment:
      - JWT_TOKEN_SECRET=ajsonwebtokensecret
      - MONGO_URI=mongodb://localhost:27017/em
      - PORT=3005
      - MAIL_USER=******
      - MAIL_PASS=******
      - [email protected]
      - CLIENT_SIDE_URL=http://localhost:3001
      - ELASTICSEARCH_URI=http://localhost:9200
   volumes:
      - .:/usr/src/app/
    command: yarn start:dev
    links:
      - elasticsearch
    depends_on:
      - elasticsearch
    networks:
      - esnet
  elasticsearch:
    container_name: em-elasticsearch
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.0
    volumes:
      - esdata:/usr/share/elasticsearch/data
    environment:
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
    logging:
      driver: none
    ports:
      - 9300:9300
      - 9200:9200
    networks:
      - esnet
volumes:
  esdata:
networks:
  esnet:

When i try to run docker compose up i get this error

cont | yarn run v1.19.1

cont | error Couldn't find a package.json file in "/"

cont | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

cont exited with code 1

I have tried this, but all solutions I have tried don't work. Does anyone have a solution to this?

Upvotes: 2

Views: 6769

Answers (1)

Josh Wulf
Josh Wulf

Reputation: 4877

Relative pathing. Note that the working COPY command is COPY . .. The . there means 'the current directory'. So what you need is:

COPY ./package*.json ./

You would simplify your debugging by running docker build -t scratch . to eliminate docker-compose from the equation. Always drive it down to absolute minimal reproducer to isolate the exact issue.

Upvotes: 3

Related Questions