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

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

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:

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

This is my Dockerfile:

  1. FROM node:18-alpine
  2. WORKDIR /projectapp
  3. COPY package*.json .
  4. RUN npm ci
  5. COPY . .
  6. ENV PORT=8080
  7. EXPOSE 8080
  8. 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.

  1. version: "3.9"
  2. networks:
  3. idm_network:
  4. driver: bridge
  5. # Project images
  6. services:
  7. projectapp:
  8. build:
  9. context: ./projectapp
  10. networks:
  11. - idm_network
  12. volumes:
  13. - .:/projectapp # for development phase only
  14. ports:
  15. - "8080:8080"
  16. mysql:
  17. build:
  18. context: ./mysql
  19. networks:
  20. - idm_network
  21. volumes:
  22. - project-mysql-data:/var/lib/mysql
  23. environment:
  24. - MYSQL_ROOT_PASSWORD=idm
  25. - MYSQL_ROOT_HOST=%
  26. keyrock:
  27. image: fiware/idm:latest
  28. networks:
  29. - idm_network
  30. depends_on:
  31. - mysql
  32. environment:
  33. - IDM_DB_HOST=mysql
  34. mongo-orion:
  35. image: mongo:latest
  36. volumes:
  37. - project-mongo-orion-data:/data/db
  38. networks:
  39. - idm_network
  40. command: --nojournal
  41. orion:
  42. image: fiware/orion
  43. links:
  44. - mongo-orion
  45. networks:
  46. - idm_network
  47. command: -dbhost mongo-orion
  48. mongo-data:
  49. build:
  50. context: ./mongoData
  51. volumes:
  52. - project-mongo-data:/data/db
  53. networks:
  54. - idm_network
  55. data-storage-server:
  56. build:
  57. context: ./dataStorageServer
  58. volumes:
  59. - .:/dataserver # for development phase only
  60. depends_on:
  61. - mongo-data
  62. networks:
  63. - idm_network
  64. # Project volumes
  65. volumes:
  66. project-mysql-data:
  67. project-mongo-orion-data:
  68. 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:

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

This is my Dockerfile:

  1. FROM node:18-alpine
  2. WORKDIR /projectapp
  3. COPY package*.json .
  4. RUN npm ci
  5. COPY . .
  6. ENV PORT = 8080
  7. EXPOSE 8080
  8. 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.

  1. version: "3.9"
  2. networks:
  3. idm_network:
  4. driver: bridge
  5. #project images
  6. services:
  7. projectapp:
  8. build:
  9. context: ./projectapp
  10. networks:
  11. - idm_network
  12. volumes:
  13. - .:/projectapp #for development phase only
  14. ports:
  15. - "8080:8080"
  16. mysql:
  17. build:
  18. context:
  19. ./mysql
  20. networks:
  21. - idm_network
  22. volumes:
  23. - project-mysql-data:/var/lib/mysql
  24. environment:
  25. - MYSQL_ROOT_PASSWORD=idm
  26. - MYSQL_ROOT_HOST=%
  27. keyrock:
  28. image: fiware/idm:latest
  29. networks:
  30. - idm_network
  31. depends_on:
  32. - mysql
  33. environment:
  34. - IDM_DB_HOST=mysql
  35. mongo-orion:
  36. image: mongo:latest
  37. volumes:
  38. - project-mongo-orion-data:/data/db
  39. networks:
  40. - idm_network
  41. command: --nojournal
  42. orion:
  43. image: fiware/orion
  44. links:
  45. - mongo-orion
  46. networks:
  47. - idm_network
  48. command: -dbhost mongo-orion
  49. mongo-data:
  50. build:
  51. context: ./mongoData
  52. volumes:
  53. - project-mongo-data:/data/db
  54. networks:
  55. - idm_network
  56. data-storage-server:
  57. build:
  58. context: ./dataStorageServer
  59. volumes:
  60. - .:/dataserver #for development phase only
  61. depends_on:
  62. - mongo-data
  63. networks:
  64. - idm_network
  65. #pep-proxy-datastorage:
  66. # image: fiware/pep-proxy:latest
  67. # networks:
  68. # - idm_network
  69. #pep-proxy-orion:
  70. #image: fiware/pep-proxy:latest
  71. #networks:
  72. # - idm_network
  73. #project volumes
  74. volumes:
  75. project-mysql-data:
  76. project-mongo-orion-data:
  77. 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:,将使您的配置简化为:

  1. version: '3.8'
  2. services:
  3. projectapp:
  4. build: ./projectapp
  5. ports:
  6. - "8080:8080"
  7. ...
  8. mongo-data:
  9. build: ./mongoData
  10. volumes: # 保留卷以用于持久数据库数据
  11. - project-mongo-data:/data/db
  12. data-storage-server:
  13. build: ./dataStorageServer
  14. depends_on:
  15. - 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

  1. version: '3.8'
  2. services:
  3. projectapp:
  4. build: ./projectapp
  5. ports:
  6. - "8080:8080"
  7. ...
  8. mongo-data:
  9. build: ./mongoData
  10. volumes: # keep volumes for persistent database data
  11. - project-mongo-data:/data/db
  12. data-storage-server:
  13. build: ./dataStorageServer
  14. depends_on:
  15. - 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

问题是我有这行代码:

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

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

英文:

Problem was I had this line:

  1. volumes:
  2. - .:/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:

确定