Help with Docker build on MacOS

Hi @pierre-gilles

I have it running quite well now. I’m checking a bit on the Zigbee2MQTT service and using the USB device at /dev/ttyACM0. I triggered the API /api/v1/service/usb/port, but I got a 404 Not Found error.

After checking, I found that UsbController failed to run due to the following error:

SERVICE_LOAD_ERROR Error: /src/server/services/usb/node_modules/@serialport/bindings/build/Release/bindings.node: invalid ELF header
    at Module._extensions..node (node:internal/modules/cjs/loader:1460:18)
    at Module.load (node:internal/modules/cjs/loader:1203:32)
    at Module._load (node:internal/modules/cjs/loader:1019:12)
    at Module.require (node:internal/modules/cjs

I researched a bit and found that this issue seems related to the build architecture. However, I built it on a Mac M1 and used the following command to build:

ARG TARGET
ARG VERSION
ARG BUILD_DATE

FROM --platform=linux/arm64 ${TARGET}/node:18-slim

LABEL \
  org.label-schema.build-date=$BUILD_DATE \
  org.label-schema.version=$VERSION

COPY qemu-* /usr/bin/

# System dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
         tzdata \
         nmap \
         ffmpeg \
         sqlite3 \
         openssl \
         gzip \
         udev \
         bluez \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /tmp

# Install Gladys
RUN mkdir /src
WORKDIR /src
ADD . /src
# COPY ./static /src/server/static
WORKDIR /src/server

# Install build dependencies and application dependencies
RUN apt-get update && apt-get install -y \
         build-essential \
         python3 \
         python3-pip \
         git \
         libffi-dev && \
         apt-get clean

RUN npm install --unsafe-perm --production

RUN npm cache clean --force \
         && apt-get autoremove -y build-essential python3 python3-pip git libffi-dev \
         && apt-get purge -y --auto-remove \
         && rm -rf /var/lib/apt/lists/*

ENV NODE_ENV production
ENV SERVER_PORT 80

# Export listening port
EXPOSE 80

CMD ["node", "index.js"]

Please give me advice and a solution to fix this issue. Thank you very much!

Hi @vule :slight_smile:

I made your topic public, it’s best that everyone can see your post (I’m not the only one who can answer to you!)

Why do you build on your Mac ? I’m never building on mine, just using Github builds :smiley:

See this tutorial :

If you really want to build on Mac, you need to make sure that you are doing cross compilation correctly.

Do you confirm your are building your Docker image with buildx ? not docker build ?

Make sure the node_modules from your Mac are not copied inside the container.

What’s happening here is that your node_modules from your Mac are copied inside the container and it can’t work because those are compiled for MacOS and inside the container it’s linux :slight_smile:

ChatGPT is good help with these kind of errors btw ^^

1 Like