Docker 容器中的 Node-sass 绑定问题

Node-sass binding problem in Docker container

我的 angular 应用程序源代码在 运行 Docker 容器中安装卷时出现问题。

我的主机OS是Win 10 64位。

这是我的 Docker 文件,它位于应用程序的根文件夹中。

# base image
FROM node:10

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install -g @angular/cli@1.7.1
RUN npm install node-sass@latest
RUN npm rebuild node-sass

# add app
COPY . /usr/src/app

# start app
CMD ng serve --host 0.0.0.0 

关于安装和重建 Node-sass 的行是修复问题的努力,可能应该在这里。

所以我开始构建 docker 容器

docker build -t my-cool-app .

然后运行:我想将源代码从我的主机挂载到容器中:

docker run -it -v ${PWD}:/usr/src/app -v ${PWD}/node_modules -p 4200:4200  my-cool-app

应用程序开始编译,但出现错误。

Module build failed (from ./node_modules/sass-loader/lib/loader.js): Error: Missing binding /usr/src/app/node_modules/node-sass/vendor/linux-x64-64/binding.node Node Sass could not find a binding for your current environment: Linux 64-bit with Node.js 10.x

Found bindings for the following environments: - Windows 64-bit with Node.js 10.x

我理解问题:node-sass 以二进制代码的形式构建在 windows 主机上,并且没有在 Linux 中启动的绑定。正如这里 https://github.com/sass/node-sass/issues/2165 的人所说 - 嘿,安装后重建节点 - sass - 我将此类命令添加到 Docker 文件但仍然失败。

我也为此奋斗了好几天。我终于取得了一些成功。 我需要清理缓存并重建节点-sass。 下面是我的 Dockerfile:

FROM node:10.13-alpine as build     
WORKDIR /usr/src/app
COPY ["pap-ui/package.json", "/usr/src/app/"]
RUN npm install @angular/cli@7.3.8 -g
RUN npm cache clean --force
RUN npm install --save-dev

COPY . /usr/src/app

WORKDIR /usr/src/app/pap-ui
RUN npm rebuild node-sass --force
RUN npm run build

# Build a small nginx image with static website
FROM nginx:alpine
RUN rm -rf /usr/share/nginx/html/*
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/pap-ui/dist/pap-ui /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

问题是,作为步骤 3 的一部分,您正在从本地环境复制 node_modules 文件夹。确保创建 。 dockerignore 文件并添加 node_modules 文件夹。一旦你这样做了,你就不需要做 npm rebuild