问题发生在node-sass和Docker上

时间:2017-01-30 18:17:48

标签: node.js docker sass docker-compose node-sass

我正在尝试将我的节点应用程序停靠。我当前的应用程序是一个带有postgresql的nodejs express服务器。 ExpressJS使用node-sass-middleware来处理sass资产。当我在我的OSX机器上本地运行node和postgresql时,一切正常。当我尝试使用docker-compose运行应用时,我收到“遗失绑定错误”

这是我的Dockerfile:

FROM node:7.2.1
RUN apt-get update -qq && apt-get install -y build-essential
RUN apt-get install -y libpq-dev postgresql-client
ENV APP_HOME /my_app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
ADD package.json .
RUN npm install
RUN npm rebuild node-sass
ADD . .
CMD [ "npm", "start" ]
EXPOSE 3000

这是我的docker-compose.yml文件:

version: '2'
services:
    db:
      image: postgres:9.6.1
      ports:
        - '5432:5432'
    web:
      build: . # use the Dockerfile next to this file
      volumes:
        - .:/my_app
      ports:
        - "3000:3000"
      depends_on:
        - db

当我运行docker-compose时,我仍然会收到以下错误:

web_1  | [nodemon] 1.11.0
web_1  | [nodemon] to restart at any time, enter `rs`
web_1  | [nodemon] watching: *.*
web_1  | [nodemon] starting `node ./bin/www`
web_1  | /my_app/node_modules/node-sass/lib/binding.js:15
web_1  |       throw new Error(errors.missingBinary());
web_1  |       ^
web_1  |
web_1  | Error: Missing binding /my_app/node_modules/node-sass/vendor/linux-x64-51/binding.node
web_1  | Node Sass could not find a binding for your current environment: Linux 64-bit with Node.js 7.x
web_1  |
web_1  | Found bindings for the following environments:
web_1  |   - OS X 64-bit with Node.js 7.x
web_1  |
web_1  | This usually happens because your environment has changed since running `npm install`.
web_1  | Run `npm rebuild node-sass` to build the binding for your current environment.
web_1  |     at module.exports (/my_app/node_modules/node-sass/lib/binding.js:15:13)
web_1  |     at Object.<anonymous> (/my_app/node_modules/node-sass/lib/index.js:14:35)
web_1  |     at Module._compile (module.js:571:32)
web_1  |     at Object.Module._extensions..js (module.js:580:10)
web_1  |     at Module.load (module.js:488:32)
web_1  |     at tryModuleLoad (module.js:447:12)
web_1  |     at Function.Module._load (module.js:439:3)
web_1  |     at Module.require (module.js:498:17)
web_1  |     at require (internal/module.js:20:19)
web_1  |     at Object.<anonymous> (/my_app/node_modules/node-sass-middleware/middleware.js:3:12)
web_1  |     at Module._compile (module.js:571:32)
web_1  |     at Object.Module._extensions..js (module.js:580:10)
web_1  |     at Module.load (module.js:488:32)
web_1  |     at tryModuleLoad (module.js:447:12)
web_1  |     at Function.Module._load (module.js:439:3)
web_1  |     at Module.require (module.js:498:17)
web_1  | [nodemon] app crashed - waiting for file changes before starting...

我通过将RUN npm rebuild node-sass添加到Dockerfile中,它将为docker容器中的操作系统构建正确的绑定...但它似乎不起作用。

有什么想法吗?

4 个答案:

答案 0 :(得分:5)

Node.js 7(对于Linux和OSX)的支持似乎已添加到node-sass v3.7.0中。确保使用的版本等于或高于此版本。

您可以更新Dockerfile:

FROM node:7.2.1
RUN apt-get update -qq && apt-get install -y build-essential
RUN apt-get install -y libpq-dev postgresql-client
ENV APP_HOME /my_app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
ADD package.json .

# Add the two entries below
RUN mkdir -p node_modules/node-sass/vendor/linux-x64-51
RUN curl -L https://github.com/sass/node-sass/releases/download/v4.5.0/linux-x64-51_binding.node -o node_modules/node-sass/vendor/linux-x64-51/binding.node

RUN npm install
RUN npm rebuild node-sass
ADD . .
CMD [ "npm", "start" ]
EXPOSE 3000

或者你可以在本地下载绑定,然后在没有任何修改的情况下从Dockerfile构建:

cd /path/to/node_app/node_modules
mkdir -p node-sass/vendor/linux-x64-51
curl -L https://github.com/sass/node-sass/releases/download/v4.5.0/linux-x64-51_binding.node -o node-sass/vendor/linux-x64-51/binding.node

请注意以下版本的预编译本机绑定的不同版本:https://github.com/sass/node-sass/releases

答案 1 :(得分:0)

在Docker中运行我的应用程序时,我几次遇到此问题。 我相信用Docker挂载本地应用程序目录时会出现问题,因为其中包括在不同环境中构建的node_modules文件夹。

当Docker在Linux中运行时,我的本地设置是Mac OSX。

对我来说,解决方案是在应用程序的根目录中添加.dockerignore文件,并根据建议https://stackoverflow.com/a/55657576/3258059添加node_modules

然后,我需要触发纱线安装以再次在我的docker容器中运行,并且还使用docker容器运行docker-compose run web npm rebuild node-sass的node-sass rebuild命令,其中web是我的docker容器的名称。

我怀疑我的Dockerfile可能有点膨胀,但如果有帮助,我会补充一下:

FROM ruby:2.3.7
ENV BUNDLER_VERSION=1.17.3
RUN gem install bundler -v "$BUNDLER_VERSION" --no-document
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -  
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -   
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list   
RUN apt-get update -qq && apt-get install -y nodejs yarn build-essential libpq-dev postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
ENV RAILS_ENV docker    
ENV WEBPACKER_DEV_SERVER_PUBLIC localhost:3035  
ENV WEBPACKER_DEV_SERVER_HOST localhost 
COPY package.json *yarn* ./
RUN bundle install
RUN yarn install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

和我的docker-compose.yml

version: '3'
services:
  db:
    image: postgres
    ports: 
      - "5433:5432"
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    environment: 
      - RAILS_ENV=development
      - NODE_ENV=development
      - WEBPACKER_DEV_SERVER_PUBLIC=localhost:3035
      - WEBPACKER_DEV_SERVER_HOST=localhost
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
      #- elasticsearch
  redis:
    image: redis:alpine

答案 2 :(得分:0)

我能够通过更改来解决这个问题:

RUN npm rebuild node-sass

到:

RUN npm rebuild node-sass --sass-binary-name = linux-x64-83

指出要运行的发行版。就我而言,我必须查看我请求的是哪个版本,在本例中是 Linux x64-83

以下是可能存在的版本列表: https://github.com/sass/node-sass/releases

答案 3 :(得分:0)

我为 docker、yarn 和 react 做了这个

# pull the base image
FROM node:lts-alpine

# set the working direction
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package.json ./

COPY yarn.lock ./

# rebuild node-sass
RUN yarn add node-sass

RUN yarn

# add app
COPY . ./

# start app
CMD ["yarn", "start"]