我在将我的Node.js项目进行Docker化时遇到了错误。

huangapple go评论70阅读模式
英文:

I get an error while dockerizing my nodejs project

问题

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 privileges 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

# Project volumes
volumes:
  project-mysql-data:
  project-mongo-orion-data:
  project-mongo-data:
英文:

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:

答案1

得分: 0

删除 projectapp 下的 volumes: 块。

此块的作用是使用主机系统中的内容替换镜像中的整个应用程序。 (想象一下正常安装 Node,然后单独检出 Node 的源代码并解压缩到您已经安装的 Node 上。)没有特定的保证或检查镜像和主机内容是否"匹配"。像镜像中 npm installnode_modules 树等内容也会被隐藏。

在您的情况下,您正在从 ./projectapp 子目录构建镜像,但是在镜像代码上进行绑定挂载到当前目录 .,这会导致在运行容器时没有 package.json,从而产生您看到的错误。

我可能会对此设置进行两个更改。如果您删除文件中的所有 networks: 块,Compose 将创建一个名为 default 的网络并将所有容器附加到它;这将与您现在拥有的网络完全相同,但需要更少的设置。您还可以使用简写的 build: directoryname,其含义与您现在拥有的 build: { context: directoryname } 相同。这样做,再加上删除不必要的 volumes:,将使您的配置简化为:

version: '3.8'

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

  ...

  mongo-data:
    build: ./mongoData
    volumes:  # 保留卷以用于持久数据库数据
      - project-mongo-data:/data/db

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

orion 服务还具有已过时的 links: 选项,您也可以将其删除(或更改为 depends_on:);名称 mongo-orion 仍然会使用正常的 Docker 网络解析。

英文:

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.

答案2

得分: 0

问题是我有这行代码:

volumes:
- .:/projectapp   # 仅用于开发阶段

这样我就不必在每次进行更改时都重新启动 Docker,但这造成了问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年1月10日 18:28:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75068631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定