问题描述
我正在尝试 dockerise 我的节点应用程序.我当前的应用程序是带有 postgresql 的 nodejs express 服务器.ExpressJS 使用 node-sass-middleware 来处理 sass 资产.当我在 OSX 机器上本地运行 node 和 postgresql 时,一切正常.当我尝试使用 docker-compose
运行应用程序时,出现缺少绑定错误"
I'm attempting to dockerise my node application. My current application is a nodejs express server with postgresql. ExpressJS uses node-sass-middleware to handle the sass assets. When I run node and postgresql locally on my OSX machine everything works fine. When I try to run the app with docker-compose
I get a "Missing Binding error"
这是我的 Dockerfile:
Here is my 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 up 时,我仍然收到以下错误:
When I run docker-compose up I still get the following error:
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 容器中的操作系统构建正确的绑定......但它似乎不起作用.
I though by adding RUN npm rebuild node-sass
to the Dockerfile, it would build the correct binding for the OS in the docker container... But it does not seem to work.
有什么想法吗?
推荐答案
node-sass v3.7.0
似乎已经添加了对 Node.js 7(适用于 Linux 和 OSX)的支持.请确保您使用的版本等于或高于此版本.
The support for Node.js 7 (for Linux and OSX) seems to have been added in node-sass v3.7.0
. Make sure you use a version equal to or newer than this.
或者你可以更新你的 Dockerfile:
Either you can update your 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 构建,无需任何修改:
Or you can download the binding locally and then build from the Dockerfile without any modification:
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
Keep an eye out for different versions for the pre-compiled native bindings at: https://github.com/sass/node-sass/releases
这篇关于node-sass 和 Docker 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!