开发者问题收集

使用 imagemin-mozjpeg 与 Docker 会引发此错误“spawn 未知系统错误 -8”

2021-08-22
1947

在我的 macOS 上似乎没有任何问题,但是当使用 Docker 时我得到了这个输出:

node_1      | [08:16:41] Finished 'clean' after 23 ms
node_1      | [08:16:41] Starting 'scripts'...
node_1      | [08:16:41] Starting 'styles'...
node_1      | [08:16:41] Starting 'images'...
node_1      | [08:16:41] Starting 'html'...
node_1      | ℹ - [Scripts] Compiling...
node_1      | ℹ - [Styles] Compiling...
node_1      | ℹ - [Images] Optimizing...
node_1      | ℹ - [HTML] Compiling...
node_1      | [08:16:58] 'images' errored after 17 s
node_1      | [08:16:58] Error in plugin "gulp-imagemin"
node_1      | Message:
node_1      |     spawn Unknown system error -8
node_1      | Details:
node_1      |     errno: -8
node_1      |     code: Unknown system error -8
node_1      |     syscall: spawn
node_1      |     fileName: /usr/src/app/assets/img/about-01.jpg
node_1      |     domainEmitter: [object Object]
node_1      |     domainThrown: false
node_1      | 
node_1      | [08:16:58] 'build' errored after 17 s
node_1      | npm ERR! code ELIFECYCLE
node_1      | npm ERR! errno 1
node_1      | npm ERR! @ dev: `gulp build && gulp watch`
node_1      | npm ERR! Exit status 1
node_1      | npm ERR! 
node_1      | npm ERR! Failed at the @ dev script.
node_1      | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
node_1      | 
node_1      | npm ERR! A complete log of this run can be found in:
node_1      | npm ERR!     /root/.npm/_logs/2021-08-22T08_16_58_562Z-debug.log
node_1 exited with code 1

我的 Dockerfile:

FROM node:14-alpine3.11

WORKDIR /usr/src/app

COPY ./app/package.json /usr/src/app

RUN apk --no-cache add \
    ca-certificates \
    build-base \
    autoconf \
    automake \
    zlib \
    bash \
    libltdl \
    libtool \
    zlib-dev \
    nasm \
    && rm -rf /var/lib/apt/lists/*
    # mozjpeg: the packages above allowing to compile the binaries

RUN npm install -g gulp \
    && npm install gulp

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

gulpfile

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

const imageminMozjpeg = require('imagemin-mozjpeg');

gulp.task('images', () => {
  logger('[Images] Optimizing...');
  return gulp
    .src(`${options.paths.src.img}/**/*.*`)
    .pipe(
      imagemin(
        [
          gifsicle({ interlaced: true }),
          imageminMozjpeg({ 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",
"imagemin-mozjpeg": "^6.0.0"
  }
}

我清理了缓存,再次运行了 npm i 但没有任何帮助。之前,我修复了这个问题: mozjpeg/vendor/cjpeg` 二进制文件似乎无法正常工作 https://github.com/imagemin/imagemin-mozjpeg/issues/28

我做了一些研究,但找不到遇到同样问题的人。

docker-compose.yml

version: '3'

volumes:
  local_postgres_data: {}
  local_postgres_data_backups: {}

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/
    command: npm run dev
    ports:
      - '3000:3000'

3个回答

On my macOS [...] using Docker [...] I run only once npm i outside docker

Docker 通常运行 Linux 容器(Windows 主机上的 Windows 容器除外)。使用此设置,您无法将安装在 MacOS 系统上的 node_modules 目录注入 Linux 容器;您将收到与所见类似的错误。

如果您尝试获取实时开发环境,最简单的方法是忽略 Docker 并仅使用 Node。

brew install node
npm install
docker-compose up -d django
npm run dev

如果您想在 Docker 中运行它,则映像通常应该是自包含的;不要尝试从主机注入代码,并且最好在 Dockerfile 中进行设置(如果它们在那里有意义)。第一步是防止 Docker 将主机的 node_modules 目录复制到容器中;创建一个 .dockerignore 文件,其中包含以下行

# .dockerignore
node_modules

Dockerfile 需要 COPY 中应用程序代码、 RUN npm install ,并设置标准元数据以运行应用程序。

FROM node:14-alpine3.11

# Install OS packages before COPYing anything at all in.
# (Review this list: do you _need_ a full C toolchain and
# low-level tools like libtool?)
RUN apk add ...

WORKDIR /usr/src/app

# Install Node packages.  Make sure to COPY the lock file in
# to get reproducible versions.  Don't `npm install` extra packages;
# add them to `package.json` instead.
COPY package.json package-lock.json .
RUN npm ci

# Install the rest of the application.
COPY . .
RUN npm build

# Say how to start the container.
CMD npm run start

现在,镜像包含源代码、Node 库依赖项、(Linux)支持二进制文件和默认命令,您无需在 docker-compose.yml 中指定任何这些内容;该部分可以只是

version: '3.8'
services:
  ...
  node:
    build:
      context: ./app              # (or a subdirectory containing the Node application?)
      dockerfile: Dockerfile.node # (omit if named exactly `Dockerfile`)
    ports:
      - '3000:3000'
    # depends_on:                 # only if the _server_ makes direct calls to the
    #   - django                  # backend; not if only the browser code does
    # no volumes:, command:, image: name override

(您应该能够类似地简化 Django 应用程序的 Compose 设置: COPY 其源代码进入其镜像,在那里设置标准 CMD ,并删除几乎所有运行时选项。)

David Maze
2021-08-22

经过数小时的研究和尝试不同的设置,我设法解决了使用 Linux/Docker 时出现的 mozjpeg 错误。使用 alpine 映像时尤其会出现此问题。

我更新的 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 install --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 . .

这些设置可能会为您节省数小时的烦恼。顺便说一句,不推荐使用 Node 的 alpine 映像,因为构建时间较长,尽管它的映像尺寸很小。

祝您编码愉快!

xshapira
2021-08-26

首先安装 npm。

npm install

vue-loader 是 webpack 的一个加载器,它允许您以某种格式编写 Vue 组件。请运行该命令。

npm i vue-loader

现在运行此命令。

npm run dev
Md.Shohag Mia
2021-09-14