Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile not COPY pakcage json

My folder structure is like this

project/
   -backend/
       Dockerfile
       package.json
   - docker-compose.yml

My dockerfile looks like this

FROM node:14-alpine as base

WORKDIR /src
COPY ./package*.json /src
EXPOSE 3000


FROM base as dev
ENV NODE_ENV=development
RUN npm install -g nodemon && npm install
COPY . /
CMD ["npm run start:dev"]

just a simple dockerfile, nothing crazy

and my docker-compose like

version: '3'
services:

  web:
    build:
      context: ./backend
      target: dev
    volumes:
      - .:/src
    command: npm run start:dev
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: development
      DEBUG: nodejs-docker-express:*

When I do docker-compose up an error shows up saying

web_1  | npm ERR! code ENOENT
web_1  | npm ERR! syscall open
web_1  | npm ERR! path /src/package.json
web_1  | npm ERR! errno -2
web_1  | npm ERR! enoent ENOENT: no such file or directory, open '/src/package.json'
web_1  | npm ERR! enoent This is related to npm not being able to find a file.
web_1  | npm ERR! enoent
web_1  |
web_1  | npm ERR! A complete log of this run can be found in:
web_1  | npm ERR!     /root/.npm/_logs/2021-05-22T20_54_52_629Z-debug.log

This means that the package.json hasn't been copied I know. But what I don't know is how to fix it. I think the error is in the dockerfile but I coulnd't fix it

can someone tell me what I am doing wrong?

like image 796
Andres Avatar asked Mar 20 '26 05:03

Andres


1 Answers

Try out by editing your docker compose, you have a problem in your volumes that is copying the hole project directory.

replace

    volumes:
      - .:/src

with

    volumes:
      - ./backend:/src
like image 66
Julian Mendez Avatar answered Mar 22 '26 22:03

Julian Mendez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!