简体   繁体   中英

Node-sass binding problem in Docker container

There is problem with mounting volume with source code of my angular app in running Docker container.

My host OS is Win 10 64 bit.

This is my Dockerfile which is in root folder of app.

# 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 

Rows about installing and rebuild Node-sass are efforts to fix problem, probably, there should be here.

So I start building docker container

docker build -t my-cool-app .

Then running: I want to mount source code from my host machine into container:

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

App starts compilation and I get error.

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

I understand problem: node-sass builds on windows host as binary code and there is no binding to be started in Linux. As guys from here https://github.com/sass/node-sass/issues/2165 said - hey, rebuild node-sass after installation - I added such commands to Docker file but still failed.

I've also been battling this for a number of days. I've finally had some success. I needed to clean the cache and also rebuild node-sass. Below is my 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;"]

The problem is that you are copying the node_modules folder from your local environment as part of step 3. Make sure you create a .dockerignore file and add the node_modules folder. Once you do that, you won't need to do npm rebuild

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM