撰写节点容器抛出:npm ERR! enoent ENOENT: 没有这样的文件或目录

compose node container throws: npm ERR! enoent ENOENT: no such file or directory

我在 docker 中遇到了这个错误:

npm ERR! enoent ENOENT: no such file or directory, open 
'/home/dan/repos/test_11/wp-content/themes/twentytwentyone/package.json'

这是我的 docker-compose.yml 文件:

version: "3.9"
services:
  npm:
    image: node:16.13.2
    container_name: dan_npm
    volumes:
      - "./app:/home/dan/repos/test_11/wp-content/themes/twentytwentyone"
    working_dir: "/home/dan/repos/test_11/wp-content/themes/twentytwentyone"
    entrypoint: [ 'npm', 'start' ]

我错过了什么?

Image from project folder tree

您的问题是您将 /app 和其中的所有内容都挂载在容器内路径的末尾。所以你在重复路径。

通过这样做。

volumes:
  # mounts app and everything inside at the location after the colon
  - ./app:/home/dan/repos/test_11/wp-content/themes/twentytwentyone

容器内的最终路径将是这个。

/home/dan/repos/test_11/wp-content/themes/twentytwentyone/wp-content/themes/twentytwentyone/

我建议安装这样的东西。

working_dir: /twentytwentyone
volumes:
  # mount only the relevant folder (the last one) inside /app 
  # to some location, not as deeply nested for convinience
  - ./app/wp-content/themes/twentytwentyone:/twentytwentyone