12

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 ?

nickgryg
  • 25,567
  • 5
  • 77
  • 79
Roy Milder
  • 886
  • 2
  • 11
  • 19

5 Answers5

18

OK, here's an answer--a way to install node-canvas v2.5 under the official node:10.16.0-alpine Docker image. As you probably know, the error you posted, "Error relocating...canvas.node" indicates your build failed. This is because canvas uses glibc but alpine uses musl. Canvas needs to link to glibc, so you need to add it to your image. Sasha Gerrand offers alpine-pkg-glibc as one way to do it. Using his installation instructions, here's how it looks in a docker file:

    #  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
        ;
HieroB
  • 3,917
  • 4
  • 17
  • 22
  • thanks for answering. I did however switch to debian-slim as our docker image. We ran in to some other issues which forced us to switch images. I will however, accept your answer :) looks like it is the solution. – Roy Milder Aug 02 '19 at 11:03
  • I got a few error during the build but it seems fine when running my app – hakuna1811 Sep 30 '19 at 14:32
16
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 . .

This worked for me. The additional packages are dependencies for sodium native.

Young Developer
  • 253
  • 3
  • 8
5

I ran into the same error installing canvas@2.6.0 on docker base with alpine and nodev8.9.4. I was able to fix it by upgrading to nodev10.15.0 and alpinev3.10. It works without having to install the alpine-pkg-glibc package. Ofcourse, you'll still have to install these packages:

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
sionelt
  • 51
  • 2
0

The "rebuild" is not worked also with me. But this modification will, you can try (please ignore some of my other packages for other libraries)

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
-1

This is not an answer--sorry--but you may need a workaround until this gets resolved. I have temporarily backed up to the node:8.1.0-alpine image. Under this image I am able to npm install canvas@1.6.7. It throws a flood of warnings but I get a working image I can deploy. Now I'm hoping someone can figure out a real answer to this issue and we can get canvas 2.5 running under node:10.16.0-alpine. Hope it helps.

HieroB
  • 3,917
  • 4
  • 17
  • 22