【发布时间】:2021-08-09 15:21:06
【问题描述】:
我在 Windows 10 上使用带有实时重新加载的 Docker 进行本地开发,其中包含一个 React 前端、一个 Node.js 后端和一个 Nginx 服务器。我可以启动这三个服务并检测更改,但客户端无法编译每个时间不管我做什么。这是错误代码:
Failed to compile.
client_1 | ./src/main.scss (./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-5-1!./node_modules/postcss-loader/src??postcss!./node_modules/resolve-url-loader??ref--6-oneOf-5-3!./node_modules/sass-loader/dist/cjs.js??ref--6-oneOf-5-4!./src/main.scss)
client_1 | Error: Missing binding /client/node_modules/node-sass/vendor/linux_musl-x64-64/binding.node
client_1 | Node Sass could not find a binding for your current environment: Linux/musl 64-bit with Node.js 10.x
client_1 |
client_1 | Found bindings for the following environments:
client_1 | - Windows 64-bit with Node.js 12.x
client_1 |
client_1 | This usually happens because your environment has changed since running `npm install`.
client_1 | Run `npm rebuild node-sass` to download the binding for your current environment.
我首先在本地安装软件包,然后创建容器并使用 docker-compose 构建图像。
client/Dockerfile.dev
FROM node:lts
WORKDIR /client
EXPOSE 3000
CMD ["npm", "start"]
docker-compose.dev.yml(为简洁起见,去掉了其他服务)
version: "3"
services:
client:
build:
context: client
dockerfile: Dockerfile.dev
image: myapp-client
environment:
- NODE_ENV=development
volumes:
- ./client:/
- ./client:/node_modules
ports:
- "3000:3000"
tty: true
我试过这些 Node.js 图像无济于事:
node:10
node:latest
node:lts
node:10.16.3
node:8.1.0-alpine
我已经阅读了关于在RUN npm install 之后在我的 Dockerfile 中使用 RUN npm rebuild node-sass 的信息,但这仅适用于生产环境,因为对于本地开发,我正在从 package.json 中的脚本安装我的包
【问题讨论】:
-
您是否将您的 host 节点模块复制到 container 中?错误信息似乎很清楚:Windows 版本的 Node-sass 已安装,但需要 Linux 版本。复制到包文件中,然后安装 inside 容器。
-
我对 Docker 很陌生,所以我可能错了,但我不认为我在复制任何东西。在生产中,我会在 Dockerfile 上有类似
COPY package*.json /client/ RUN npm install COPY . /client/的东西,但这里没有。我知道我可以添加这些指令,并且 Docker 会在 Linux 环境中安装 node-sass,并使用(可能)正确的二进制文件。但是这样我每次重新启动容器时都必须重新安装每个包,而不是在我从 GitHub 克隆项目并运行另一个以更快地进入开发模式时运行安装脚本。 -
您不必这样做,因为图层已缓存。所以,如果
package.json没有被修改,你就不会在每次构建时都安装它。 -
您没有使用 COPY 命令,不,您正在将本地目录挂载到容器中。但这是同样的问题 - 您正在 Linux 容器中加载 Windows deps。如果您不希望它们出现在映像中,您可以在
node_modules路径上挂载一个 不同的 主机目录并将 Linux deps 安装到该目录中。