Search code examples
node.jsdockerapple-m1

Error building node based docker container on an M1 Macbook


tl;dr My M1 MacBook couldn't build a docker container but everyone else could!


I'm the only person on my team that has an M1 MacBook, which is causing a multitude of issues trying to build a node based docker container.

These are the key components from the Dockefile that I believe are relevant:

FROM node:22.5-slim

// ...

RUN apt-get update \
    && apt-get install -y tree git postgresql-client tini curl jq yq
    && npm i -g typescript

// ...

RUN yarn install

And I hit a lot of different issues. I can provide EXACT repro steps so I'll just show some of the various errors I hit below.


 YN0000: │ secp256k1@npm:4.0.3 STDERR gyp ERR! command "/usr/local/bin/node" "/app/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
33.91 ➤ YN0000: │ secp256k1@npm:4.0.3 STDERR gyp ERR! cwd /app/node_modules/secp256k1
33.91 ➤ YN0000: │ secp256k1@npm:4.0.3 STDERR gyp ERR! node -v v22.5.1
33.91 ➤ YN0000: │ secp256k1@npm:4.0.3 STDERR gyp ERR! node-gyp -v v10.1.0
33.91 ➤ YN0000: │ secp256k1@npm:4.0.3 STDERR gyp ERR! not ok

OR

.70 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR SyntaxError: invalid syntax
33.70 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! configure error
33.70 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! stack Error: `gyp` failed with exit code: 1
33.70 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! stack at ChildProcess.<anonymous> (/app/node_modules/node-gyp/lib/configure.js:297:18)
33.70 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! stack at ChildProcess.emit (node:events:520:28)
33.70 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:294:12)
33.71 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! System Linux 5.15.49-linuxkit-pr
33.71 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! command "/usr/local/bin/node" "/app/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
33.71 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! cwd /app/node_modules/@parcel/watcher
33.71 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! node -v v22.5.1
33.71 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! node-gyp -v v10.1.0
33.71 ➤ YN0000: │ @parcel/watcher@npm:2.0.4 STDERR gyp ERR! not ok

OR

41 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
50.41 ➤ YN0000: │ node-hid@npm:2.1.1 STDOUT make: Entering directory '/app/vendor/cosmjs/node_modules/node-hid/build'
50.41 ➤ YN0000: │ node-hid@npm:2.1.1 STDOUT   CC(target) Release/obj.target/hidapi/hidapi/libusb/hid.o
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR ../hidapi/libusb/hid.c:47:10: fatal error: libusb.h: No such file or directory
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR    47 | #include <libusb.h>
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR       |          ^~~~~~~~~~
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR compilation terminated.
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR make: *** [hidapi.target.mk:106: Release/obj.target/hidapi/hidapi/libusb/hid.o] Error 1
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDOUT make: Leaving directory '/app/vendor/cosmjs/node_modules/node-hid/build'
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! build error
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! stack Error: `make` failed with exit code: 2
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! stack     at ChildProcess.onExit (/app/vendor/cosmjs/node_modules/node-gyp/lib/build.js:203:23)
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! stack     at ChildProcess.emit (node:events:520:28)
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:294:12)
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! System Linux 5.15.49-linuxkit-pr
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! command "/usr/local/bin/node" "/app/vendor/cosmjs/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! cwd /app/vendor/cosmjs/node_modules/node-hid
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! node -v v22.5.1
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! node-gyp -v v9.4.0
50.44 ➤ YN0000: │ node-hid@npm:2.1.1 STDERR gyp ERR! not ok
50.45 ➤ YN0009: │ node-hid@npm:2.1.1 couldn't be built successfully (exit code 1, logs can be found here: /tmp/xfs-39a3cac7/build.log)
50.45 ➤ YN0000: └ Completed in 9s 985ms

Solution

  • I scoured the internet (and ChatGPT) for this so hopefully it helps someone!

    There are two steps here:

    1. Ensure your docker context is the default (to avoid any weirdness)
    2. Add more packages to your Dockerfile (to build correctly on ARM architecture)

    Step 1

    docker builder prune --all
    docker buildx use default
    

    Step 2

    RUN apt-get update \
        && apt-get install -y tree git postgresql-client tini curl jq yq \
    +   make build-essential pkg-config python3 libusb-1.0-0-dev libudev-dev \
        && npm i -g typescript