无法将文件替换为目录 /var/lib/docker/overlay2/if2ip5okvavl8u6jpdtpczuog/merged/app/node_modules/@ampproject/remapping。

5

我正在Windows机器上尝试使用以下Dockerfile构建一个容器化的node.js应用程序:

  # use latest version of nodejs
  FROM node:lts-alpine
  
  # install aurelia-cli to build the app & http-server to serve static contents
  RUN npm i -g http-server
  RUN npm i -g aurelia-cli
  
  # set working directory to app
  # henceforth all commands will run inside this folder
  WORKDIR /app
  
  # copy package.json related files first and install all required dependencies
  COPY package*.json ./
  RUN npm install
  
  # copy the rest of the files and folders & install dependencies
  COPY . ./
  RUN npm run build
  
  # by default http-server will serve contents on port 8080
  # so we expose this port to host machine
  EXPOSE 8080
  
  CMD [ "http-server" , "dist" ]

然而,docker build .Copy . ./ 这一行失败,并显示以下信息:cannot replace to directory /var/lib/docker/overlay2/if2ip5okvavl8u6jpdtpczuog/merged/app/node_modules/@ampproject/remapping with file

我需要做什么才能使我的容器映像构建成功?


1
那个错误信息似乎是在尝试将主机系统的 node_modules 目录复制到镜像中。这个错误本身很不寻常,但这样做会覆盖之前的 RUN docker install 步骤的结果,并且如果主机和容器是不同的操作系统,则可能会导致问题。您是否有一个 .dockerignore 文件来排除 node_modules 树? - David Maze
是的,缺少了 .dockerignore 文件,感谢您更详细地解释了潜在问题。我编辑了我的答案,将其作为解决此问题的主要建议。 - Jacob Archambault
2个回答

17
在与 Dockerfile 相同的目录下,按照此处所述,将 node_modules 添加到 .dockerignore 文件中(参考自 David Maze)。
非常不规范的方法是直接删除项目中的 node_modules 目录,然后重新运行 docker build

5
创建文件.dockerignore,并添加node_modules/,然后再次尝试构建,这应该可以解决你遇到的错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接