Estoy intentando dockerizar mi aplicación de nodo. Mi aplicación actual es un servidor express de nodejs con postgresql. ExpressJS usa node-sass-middleware para manejar los activos de sass. Cuando ejecuto node y postgresql localmente en mi máquina OSX, todo funciona bien. Cuando intento ejecutar la aplicación con docker-compose , aparece un "Error de enlace faltante"
Aquí está mi 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 Aquí está mi archivo 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: - dbCuando ejecuto docker-compose up, sigo recibiendo el siguiente 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... Pensé que al agregar RUN npm rebuild node-sass al Dockerfile, crearía el enlace correcto para el sistema operativo en el contenedor docker... Pero parece que no funciona.
¿Alguna idea?
El soporte para Node.js 7 (para Linux y OSX) parece haberse agregado en node-sass v3.7.0 . Asegúrese de usar una versión igual o más nueva que esta.
O puede actualizar su 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 3000O puede descargar el enlace localmente y luego compilar desde Dockerfile sin ninguna modificación:
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.nodeEsté atento a las diferentes versiones de los enlaces nativos precompilados en: https://github.com/sass/node-sass/releases
Me he encontrado con este problema varias veces al ejecutar mis aplicaciones en Docker. Creo que el problema surge al montar el directorio de mi aplicación local con Docker, ya que incluye la carpeta node_modules que se creó en un entorno diferente.
Mi configuración local es Mac OSX mientras Docker se ejecuta en Linux.
La solución para mí fue agregar un archivo .dockerignore en la raíz de mi aplicación y agregar node_modules según esta sugerencia https://stackoverflow.com/a/55657576/3258059
Luego necesitaba activar la instalación de hilo para que se ejecutara nuevamente en mi contenedor docker y también ejecuté el comando de reconstrucción de node-sass con el contenedor docker: docker-compose run web npm rebuild node-sass donde web es el nombre de mi contenedor docker.
Sospecho que mi Dockerfile puede tener un poco de hinchazón, pero lo agregaré en caso de que ayude a alguien:
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"]y mi 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:alpinePude resolver este problema cambiando:
RUN npm rebuild node-sasspara:
RUN npm rebuild node-sass --sass-binary-name = linux-x64-83Indicando qué distribución ejecutar. En mi caso tuve que ver que versión estaba solicitando, que en este caso es Linux x64-83 .
Aquí está la lista de versiones que pueden existir: https://github.com/sass/node-sass/releases