首页 > 解决方案 > 没有这样的文件或目录 package.json

问题描述

我正在主机上使用已安装的卷当我运行时,docker-compose build && docker-compose up -d我得到:

[4/5] RUN npm install:
#7 4.268 npm ERR! code ENOENT
#7 4.268 npm ERR! syscall open
#7 4.268 npm ERR! path /var/www/html/package.json
#7 4.269 npm ERR! errno -2
#7 4.271 npm ERR! enoent ENOENT: no such file or directory, open '/var/www/html/package.json'
#7 4.271 npm ERR! enoent This is related to npm not being able to find a file.    

executor failed running [/bin/sh -c npm install]: exit code: 254
ERROR: Service 'node' failed to build : Build failed

这是docker-compose.yml

node:
    build:
        context: ./nodejs-project
        dockerfile: Dockerfile
    container_name: node
    volumes:
        - ./nodejs-project:/var/www/html 

这是Dockerfile

FROM node:alpine

RUN mkdir -p /var/www/html

WORKDIR /var/www/html

EXPOSE 3000

RUN npm run build

CMD ["npm", "run", "dev"]

我相信这与我既从 the 安装卷又从 the 制作卷以及它以某种方式覆盖它的事实docker-compose.yml有关。但我找不到解决方案WORKDIRDockerfile

标签: docker

解决方案


容器运行时存在卷挂载。

RUN命令在映像构建时执行。

如果映像已构建,则安装不存在。因此,目录/var/www/html为空。

如果我们想让文件在容器构建时出现,我们应该COPYdockerfile.

或者,我们可以npm run build在容器启动时(通过CMD)运行,而不是在镜像构建时运行。


推荐阅读