Can't build mozjpeg on Alpine Linux

1.3k views Asked by At

I spent hours trying different node images and install different packages but nothing helped. On the host system I don't seem to have any issue when running npm run dev, but when using Docker (Alpine Linux) I get this output:

#33 72.29 npm WARN deprecated [email protected]: Please see https://github.com/lydell/urix#deprecated
#33 74.72 npm WARN deprecated [email protected]: flatten is deprecated in favor of utility frameworks such as lodash.
#33 76.10 npm WARN deprecated [email protected]: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates.
#33 76.76 npm WARN deprecated [email protected]: https://github.com/lydell/resolve-url#deprecated
#33 83.24 npm WARN deprecated [email protected]: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
#33 87.83 npm WARN deprecated [email protected]: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
#33 88.47 npm WARN deprecated [email protected]: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
#33 93.98 npm WARN deprecated [email protected]: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
#33 94.60 npm WARN deprecated [email protected]: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
#33 100.7 npm WARN deprecated [email protected]: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
#33 101.5 npm WARN deprecated [email protected]: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
#33 101.7 npm WARN deprecated [email protected]: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
#33 102.5 npm WARN deprecated [email protected]: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
#33 120.3 npm ERR! code 1
#33 120.3 npm ERR! path /usr/src/node_modules/mozjpeg
#33 120.3 npm ERR! command failed
#33 120.3 npm ERR! command sh -c node lib/install.js
#33 120.3 npm ERR! ⚠ The `/usr/src/node_modules/mozjpeg/vendor/cjpeg` binary doesn't seem to work correctly
#33 120.3 npm ERR!   ⚠ mozjpeg pre-build test failed
#33 120.3 npm ERR!   ℹ compiling from source
#33 120.3 npm ERR! /usr/src/node_modules/mozjpeg/lib/install.js:22
#33 120.3 npm ERR!              var builder = new BinBuild()
#33 120.3 npm ERR!                            ^
#33 120.3 npm ERR! 
#33 120.3 npm ERR! TypeError: BinBuild is not a constructor
#33 120.3 npm ERR!     at /usr/src/node_modules/mozjpeg/lib/install.js:22:17
#33 120.3 npm ERR!     at BinWrapper.<anonymous> (/usr/src/node_modules/mozjpeg/node_modules/bin-wrapper/index.js:156:4)
#33 120.3 npm ERR!     at ChildProcess.<anonymous> (/usr/src/node_modules/mozjpeg/node_modules/bin-check/index.js:26:4)
#33 120.3 npm ERR!     at ChildProcess.emit (node:events:394:28)
#33 120.3 npm ERR!     at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
#33 120.4 
#33 120.4 npm ERR! A complete log of this run can be found in:
#33 120.4 npm ERR!     /root/.npm/_logs/2021-08-25T07_40_00_055Z-debug.log
------
failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c apk add --update --no-cache     bash     autoconf     gcc     g++     make     automake     nasm     libtool     zlib     libc6-compat     libpng     libpng-dev     mesa-dev     libxi     build-base     && rm -rf /var/cache/apk/*     && npm ci --no-optional && npm cache clean --force     npm install -g gulp     && npm install gulp]: exit code: 1

My Dockerfile:

FROM node:16-alpine

RUN npm i npm@latest -g

WORKDIR /usr/src

COPY ./app/package*.json ./

RUN apk add --update --no-cache \
    bash \
    autoconf \
    gcc \
    g++ \
    make \
    automake \
    nasm \
    libtool \
    zlib \
    libc6-compat \
    libpng \
    libpng-dev \
    mesa-dev \
    libxi \
    build-base \
    && rm -rf /var/cache/apk/* \
    && npm ci --no-optional && npm cache clean --force \
    npm install -g gulp \
    && npm install gulp

ENV PATH /usr/src/node_modules/.bin/:$PATH

WORKDIR /usr/src/app

COPY . .


gulpfile

import imagemin, { gifsicle, mozjpeg, optipng, svgo } from 'gulp-imagemin';

gulp.task('images', () => {
  logger('[Images] Optimizing...');
  return gulp
    .src(`${options.paths.src.img}/**/*.*`)
    .pipe(
      imagemin(
        [
          gifsicle({ interlaced: true }),
          mozjpeg({ quality: 80, progressive: true }),
          optipng({ optimizationLevel: 5 }),
          svgo({ plugins: [{ removeViewBox: true }, { cleanupIDs: false }] }),
        ],
        {
          silent: false,
        }
      )
    )
    .pipe(
      isProd
        ? gulp.dest(options.paths.build.img)
        : gulp.dest(options.paths.dist.img)
    )
    .on('end', () => logger('[Images] Success!', figures.tick, 'GREEN'))
    .on('error', () => logger('[Images] Failed', figures.cross, 'RED'));
});

package.json

{
"devDependencies": {
"gulp-imagemin": "^8.0.0",
  }
}

docker-compose.yml

version: '3'

volumes:
  local_postgres_data: {}
  local_postgres_data_backups: {}
  notused:

services:
  postgres:
    build:
      context: .
      dockerfile: ./app/docker/postgres/Dockerfile
    image: webapp_prodcution_postgres
    volumes:
      - local_postgres_data:/var/lib/postgresql/data:Z
      - local_postgres_data_backups:/backups:z
    env_file:
      - ./app/.envs/.local/.env

  django:
    image: webapp_local_django
    build:
      context: .
      dockerfile: ./app/docker/django/Dockerfile
    volumes:
      - ./app/:/usr/src/app/
    command: /usr/src/app/docker/django/start_dev
    ports:
      - 8000:8000
    env_file:
      - ./app/.envs/.local/.env
    depends_on:
      - postgres

  node:
    image: webapp_local_node
    build:
      context: .
      dockerfile: ./app/docker/node/Dockerfile
    depends_on:
      - django
    volumes:
      - ./app/:/usr/src/app/
      - notused:/usr/src/app/node_modules/
    command: bash -c "rm -rf /usr/src/app/node_modules/* && npm run dev"
    ports:
      - '3000:3000'

1

There are 1 answers

0
xshapira On

I will answer my own question. Apparently when I tried to fix npm vulnerabilities, under resolutions I specified "bin-build", which rose the error: TypeError: BinBuild is not a constructor.

Regardless, the main issue was how to make imagemin work smoothly on Linux. The following settings are working perfectly fine:

My updated Dockerfile

FROM node:12-buster-slim

RUN npm i npm@latest -g

WORKDIR /usr/src

COPY ./app/package*.json ./

RUN apt-get update && apt-get install -y --no-install-recommends \
    autoconf \
    automake \
    g++ \
    libpng-dev \
    make\
    nasm \
    -y wget \
    && wget -q -O /tmp/libpng12.deb http://mirrors.kernel.org/ubuntu/pool/main/libp/libpng/libpng12-0_1.2.54-1ubuntu1_amd64.deb \
    && dpkg -i /tmp/libpng12.deb \
    && rm /tmp/libpng12.deb \
    && npm ci --no-optional && npm cache clean --force \
    npm install -g gulp \
    && npm install gulp

ENV PATH /usr/src/node_modules/.bin/:$PATH

WORKDIR /usr/src/app

COPY . .