Home > Mobile >  I get an error while dockerizing my nodejs project
I get an error while dockerizing my nodejs project

Time:01-10

I am trying to dockerize an application for a project. I'm using node.js as my server. In the process of dockerizing it throws this error:

cloud_project2-projectapp-1           | npm ERR! code ENOENT
cloud_project2-projectapp-1           | npm ERR! syscall open
cloud_project2-projectapp-1           | npm ERR! path /projectapp/package.json
cloud_project2-projectapp-1           | npm ERR! errno -2
cloud_project2-projectapp-1           | npm ERR! enoent ENOENT: no such file or directory, open '/projectapp/package.json'
cloud_project2-projectapp-1           | npm ERR! enoent This is related to npm not being able to find a file.
cloud_project2-projectapp-1           | npm ERR! enoent 

This is my Dockerfile:

FROM node:18-alpine
WORKDIR /projectapp
COPY package*.json .
RUN npm ci
COPY . .
ENV PORT = 8080
EXPOSE 8080
CMD ["npm","run","devStart"]

The dockerfile is exactly as I've seen in many videos and in the documentation. I believe the error is thrown in the RUN npm ci command and for some reason it cannot find the package.json file even though I copy it in the above command. I also searched for some answers and maybe it's a priviledges issue, but I'm new to linux and have a hard time understanding these.

I get a couple more errors with mysql and keyrock, but I'll debug these later.

version: "3.9"

networks:
  idm_network:
    driver: bridge

#project images
services:
  projectapp:
     build:
       context: ./projectapp
     networks:
       - idm_network
     volumes:    
      - .:/projectapp   #for development phase only
     ports:
       - "8080:8080"  

  mysql:
      build:
        context:
          ./mysql
      networks:
        - idm_network
      volumes:
        - project-mysql-data:/var/lib/mysql
      environment:
        - MYSQL_ROOT_PASSWORD=idm
        - MYSQL_ROOT_HOST=%


  keyrock:
    image: fiware/idm:latest  
    networks:
        - idm_network
    depends_on:
        - mysql
    environment:
        - IDM_DB_HOST=mysql


  mongo-orion:
    image: mongo:latest
    volumes:
      - project-mongo-orion-data:/data/db
    networks:
      - idm_network
    command: --nojournal

  orion:
    image: fiware/orion
    links:
     - mongo-orion
    networks:
      - idm_network
    command: -dbhost mongo-orion

  mongo-data:
    build:
      context: ./mongoData
    volumes:
      - project-mongo-data:/data/db
    networks:
      - idm_network

  data-storage-server:
    build:
      context: ./dataStorageServer
    volumes:   
      - .:/dataserver  #for development phase only
    depends_on:
      - mongo-data
    networks:
      - idm_network

  #pep-proxy-datastorage:
  #  image: fiware/pep-proxy:latest
  #  networks:
  #    - idm_network

  #pep-proxy-orion:
  #image: fiware/pep-proxy:latest
  #networks:
  #  - idm_network

#project volumes
volumes:
  project-mysql-data:
  project-mongo-orion-data:
  project-mongo-data:

CodePudding user response:

Delete the volumes: block under projectapp.

What this block does is replace your entire application in the image with content from the host system. (Imagine installing a copy of Node normally, then separately checking out Node's source code and unpacking it over the installed Node you already have.) There's no particular guarantee or check that the image and the host content "match". Things like the node_modules tree that the image npm installs get hidden as well.

In your case, you're building the image out of a ./projectapp subdirectory, but bind-mounting the current directory . over the image code. That causes there to not be a package.json when you run the container, which produces the error you see.

I might make two more changes to this setup. If you delete all of the networks: blocks in the file, Compose will create a network named default and attach all of the containers to it; this will work exactly the same way as the network you have now, but require less setup. You can also use a shorthand build: directoryname which means the same as the build: { context: directoryname } you have now. That, plus deleting the unnecessary volumes:, would get you down to

version: '3.8'

services:
  projectapp:
    build: ./projectapp
    ports:
      - "8080:8080"  

  ...

  mongo-data:
    build: ./mongoData
    volumes:  # keep volumes for persistent database data
      - project-mongo-data:/data/db

  data-storage-server:
    build: ./dataStorageServer
    depends_on:
      - mongo-data

The orion service also has the obsolete links: option and you can just delete that block as well (or change it to depends_on:); the name mongo-orion will still resolve using normal Docker networking.

CodePudding user response:

Problem was I had this line:

volumes:    
      - .:/projectapp   #for development phase only

so that I don't have to restart docker every time I made a change, but it created a problem.

  • Related