6

I'm trying to install node canvas (https://github.com/Automattic/node-canvas) on Alpine within docker.

These are (parts of) my Dockerfile:

# Use node/alpine image for final build
FROM keymetrics/pm2:latest-alpine as app

# install dependencies for canvas
RUN apk --no-cache --virtual .build-deps add \
        python \
        make \
        g++ \
        gcc \
    && apk --no-cache --virtual .canvas-build-deps add \
        build-base \
        cairo-dev \
        jpeg-dev \
        pango-dev \
        giflib-dev \
        pixman-dev \
        pangomm-dev \
        libjpeg-turbo-dev \
        freetype-dev \
    && apk --no-cache add \
        pixman \
        cairo \
        pango \
        giflib
RUN apk add --update  --repository http://dl-3.alpinelinux.org/alpine/edge/testing libmount ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family fontconfig

# Install dependencies
RUN npm install --prod
RUN npm rebuild canvas --build-from-source

When I try to boot my docker container the following error appears:

Error: Error relocating /var/www/app/node_modules/canvas/build/Release/canvas.node: FcConfigGetCurrent: symbol not found
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:775:18)
    at Module.load (internal/modules/cjs/loader.js:626:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:566:12)
    at Function.Module._load (internal/modules/cjs/loader.js:558:3)
    at Module.require (internal/modules/cjs/loader.js:663:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/var/www/app/node_modules/canvas/lib/bindings.js:3:18)
    at Module._compile (internal/modules/cjs/loader.js:734:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:745:10)
    at Module.load (internal/modules/cjs/loader.js:626:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:566:12)
    at Function.Module._load (internal/modules/cjs/loader.js:558:3)
    at Module.require (internal/modules/cjs/loader.js:663:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/var/www/app/node_modules/canvas/lib/canvas.js:9:18)
    at Module._compile (internal/modules/cjs/loader.js:734:30)

I'm guessing that it has something to do with the fact that Alpine uses musl instead of glibc but I thought that rebuilding canvas from source npm rebuild canvas --build-from-source would be enough.

I've already tried most suggestions from https://github.com/Automattic/node-canvas/issues but none is working for me.

Any suggestions ?

4

5 回答 5

14

OK,这里有一个答案——官方node:10.16.0-alpine Docker镜像下安装node-canvas v2.5的方法。您可能知道,您发布的错误“错误重定位...canvas.node”表示您的构建失败。这是因为 canvas 使用glibc而 alpine 使用musl。Canvas 需要链接到glibc,因此您需要将其添加到您的图像中。Sasha Gerrand 提供了alpine-pkg-glibc作为一种方法。使用他的安装说明,它在 docker 文件中的外观如下:

    #  geo_core layer
    #  build on a node image, in turn built on alpine linux, Docker's official linux pulled from hub.docker.com
    FROM node:10.16.0-alpine

    #  add libraries; sudo so non-root user added downstream can get sudo
    RUN apk add --no-cache \
        sudo \
        curl \
        build-base \
        g++ \
        libpng \
        libpng-dev \
        jpeg-dev \
        pango-dev \
        cairo-dev \
        giflib-dev \
        python \
        ;

    #  add glibc and install canvas
    RUN apk --no-cache add ca-certificates wget  && \
        wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
        wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.29-r0/glibc-2.29-r0.apk && \
        apk add glibc-2.29-r0.apk && \
        npm install canvas@2.5.0
        ;
于 2019-08-01T23:44:15.680 回答
5
FROM node:12-alpine

WORKDIR /app

RUN apk add --update --no-cache \
    make \
    g++ \
    jpeg-dev \
    cairo-dev \
    giflib-dev \
    pango-dev \
    libtool \
    autoconf \
    automake

COPY package.json ./

RUN npm install

COPY . .

这对我有用。附加包是钠原生的依赖项。

于 2021-03-18T13:55:05.833 回答
4

我在使用 alpine 和 nodev8.9.4 的 docker base 上安装 canvas@2.6.0 时遇到了同样的错误。我能够通过升级到nodev10.15.0alpinev3.10来修复它。它无需安装 alpine-pkg-glibc 软件包即可工作。当然,您仍然需要安装这些软件包:

apk add --no-cache \
    python \
    g++ \
    build-base \
    cairo-dev \
    jpeg-dev \
    pango-dev \
    musl-dev \
    giflib-dev \
    pixman-dev \
    pangomm-dev \
    libjpeg-turbo-dev \
    freetype-dev \
    && npm install canvas@2.6.0
于 2019-09-06T16:29:25.267 回答
0

这不是一个答案——抱歉——但您可能需要一个解决方法,直到这个问题得到解决。我暂时备份到了node:8.1.0-alpine镜像。在这张图片下,我可以npm install canvas@1.6.7。它引发了大量警告,但我得到了一个可以部署的工作图像。现在我希望有人能找到这个问题的真正答案,我们可以让 canvas 2.5 在 node:10.16.0-alpine 下运行。希望能帮助到你。

于 2019-07-30T15:48:18.120 回答
0

“重建”也不适用于我。但是这个修改会,你可以试试(请忽略我其他库的一些其他包)

FROM node:8.16.0-alpine
....

RUN apk --update --no-cache --virtual build-dependencies add \
    python python-dev py-pip \ 
    build-base make g++ pkgconfig autoconf automake bash \ 
    imagemagick cairo-dev jpeg-dev pango-dev giflib-dev pixman-dev pangomm-dev libjpeg-turbo-dev freetype-dev \ 
    libc6-compat \  
  && apk --no-cache add \
    pixman cairo pango giflib libjpeg \
....
# Remove the canvas in package.json
RUN npm install canvas@2.0.0-alpha.12 --build-from-source
RUN npm install --prod
于 2019-08-01T14:39:21.957 回答