0

I created a dockerfile for frontend project and a docker compose project for the whole project. Running the frontend within a container works fine, however when I try with docker compose I always get the same error

Attaching to frontend_dev
frontend_dev | npm ERR! code ENOENT
frontend_dev | npm ERR! syscall open
frontend_dev | npm ERR! path /app/package.json
frontend_dev | npm ERR! errno -2
frontend_dev | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
frontend_dev | npm ERR! enoent This is related to npm not being able to find a file.
frontend_dev | npm ERR! enoent 
frontend_dev | 
frontend_dev | npm ERR! A complete log of this run can be found in:
frontend_dev | npm ERR!     /root/.npm/_logs/2021-01-10T22_25_43_776Z-debug.log
frontend_dev exited with code 254

The dockerfile in the frontend folder is like this

#pull base image
FROM node:13.12.0-alpine

#set working directory
WORKDIR /app

#install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install 

#add frontend to docker container
COPY . ./

#launch frontend app
CMD [ "npm", "start" ]

and the docker-compose.yml is as this:

version: "3.0"

services:
  frontend:
    container_name: frontend_dev
    build: 
      context: ./frontend
      dockerfile: Dockerfile
    volumes:
      - /app/node_modules
      - .:/app
    ports:
      - 3001:3000
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true

and here is the structure of the project.project structre , docker-compose.yml is at the root of both frontend and backend, and just frontend contains the Dockerfile related to it.

khalil elloumi
  • 67
  • 2
  • 10
  • I tried running ls -al in the container and it shows the package.json file exists with . and .. – khalil elloumi Jan 10 '21 at 22:33
  • 3
    I think that `docker build` creates a folder called `/app` in the image and then later `docker-compose` mounts `.` in that location--hiding the files that the build placed there. Is there a `package.json` in the same directory as `docker-compose.yml`? – MatrixManAtYrService Jan 10 '21 at 22:47
  • Can you share the structure of folder and files, please? – vpalmerini Jan 10 '21 at 23:15
  • Does this answer your question? [Docker-compose: node\_modules not present in a volume after npm install succeeds](https://stackoverflow.com/questions/30043872/docker-compose-node-modules-not-present-in-a-volume-after-npm-install-succeeds) – Zeitounator Jan 10 '21 at 23:24
  • @vpalmerini I shared the project structure in an image – khalil elloumi Jan 11 '21 at 08:22
  • @MatrixManAtYrService no there isn't any package.json with the docker-compose file – khalil elloumi Jan 11 '21 at 08:23
  • @Zeitounator that issue doesn't help my case – khalil elloumi Jan 11 '21 at 08:25
  • @khalilelloumi Your main issue is that you are bind mounting your a local folder into your container app directory, totally wiping out the `npm install` done during build. This is what the duplicated issue I posted explains, unless I totally missed the point. Moreover, it seems you mount your project root whereas your `package.json` is situated (I guess...) in the `frontend` folder. Do you want to build your app in the container and run it? => remove the bind mount. Or do you want to mount your dev folder? => mount the correct folder and init the application on container start when needed. – Zeitounator Jan 11 '21 at 12:40
  • @Zeitounator I'm pretty sure I mounted the dev folder correctly in the compose file however I don't how I could run the app in the container if the local folder isn't there – khalil elloumi Jan 11 '21 at 12:55
  • What is the point of copying that folder at build time then if you don't want to run it directly from the image? Regarding the mount (if you want to use this for dev....) you are telling us yourself in a previous comment that there is no `package.json` adjacent to your compose file. This is the confirmation you are not mounting the correct folder. My guess (once again since we don't have your full project structure) replace `- .:/app` with `- ./frontend:/app`. This still leaves the problem that it will wash out all you have done during build. – Zeitounator Jan 11 '21 at 13:51
  • 1
    @Zeitounator now I see what you mean, guess I need to understand docker more to figure this out correctly, thank you for your help – khalil elloumi Jan 11 '21 at 14:13

0 Answers0