-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dockerfile
executable file
·109 lines (89 loc) · 4.01 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#######################################################################################################################
# Nexe packaging of binary
#######################################################################################################################
FROM lansible/nexe:4.0.0-rc.2 as builder
# https://github.com/docker/buildx#building-multi-platform-images
ARG TARGETPLATFORM
# https://github.com/Koenkk/zigbee2mqtt/releases
ENV VERSION=1.33.1
# Add unprivileged user
RUN echo "zigbee2mqtt:x:1000:1000:zigbee2mqtt:/:" > /etc_passwd
# Add to dailout as secondary group (20)
RUN echo "dailout:x:20:zigbee2mqtt" > /etc_group
# eudev: needed for udevadm binary
RUN apk --no-cache add \
eudev
RUN git clone --depth 1 --single-branch --branch ${VERSION} https://github.com/Koenkk/zigbee2mqtt.git /zigbee2mqtt
WORKDIR /zigbee2mqtt
# Install all modules
# Save hash file otherwise will start building on startup
# Run build to make all html files
# Serialport needs to be rebuild for Alpine https://serialport.io/docs/9.x.x/guide-installation#alpine-linux
RUN CORES=$(grep -c '^processor' /proc/cpuinfo); \
export MAKEFLAGS="-j$((CORES+1)) -l${CORES} CFLAGS=-fuse-ld=mold"; \
npm ci --no-audit --omit=optional --no-update-notifier && \
npm run build && \
npm ci --no-audit --omit=optional --omit=dev --no-update-notifier && \
echo $(git rev-parse --short HEAD) > dist/.hash
# Remove all unneeded prebuilds
RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
export TARGETPLATFORM="linux/x64"; \
fi && \
export PLATFORM=${TARGETPLATFORM/\//-}; \
find . -name *.node -path *prebuilds/* -not -path *${PLATFORM}* -name *.node -delete && \
find . -name *.glibc.node -path *prebuilds/* -delete
# Package the binary
# zigbee2mqtt dist contains typescript compile
# .hash need explicit resource otherwise not matched
# frontend/dist contains the frontend compiled stuff
# devices and lib are both needed at runtime
# Create /data to copy into final stage
RUN nexe --build \
--resource dist/ \
--resource dist/.hash \
--resource lib/ \
--resource node_modules/zigbee2mqtt-frontend/dist \
--resource node_modules/zigbee-herdsman-converters/devices \
--resource node_modules/zigbee-herdsman-converters/lib \
--resource node_modules/deep-object-diff \
--input index.js \
--output zigbee2mqtt && \
mkdir /data
#######################################################################################################################
# Final scratch image
#######################################################################################################################
FROM scratch
# Set env vars for persitance
ENV ZIGBEE2MQTT_CONFIG=/config/configuration.yaml \
ZIGBEE2MQTT_DATA=/data
# Add description
LABEL org.label-schema.description="Zigbee2MQTT as single binary in a scratch container"
# Copy the unprivileged user
COPY --from=builder /etc_passwd /etc/passwd
COPY --from=builder /etc_group /etc/group
# Serialport is using the udevadm binary
COPY --from=builder /bin/udevadm /bin/udevadm
# Copy needed libs(libstdc++.so, libgcc_s.so) for nodejs since it is partially static
# Copy linker to be able to use them (lib/ld-musl)
# Can't be fullly static since @serialport uses a C++ node addon
# https://github.com/serialport/node-serialport/blob/master/packages/bindings/lib/linux.js#L2
COPY --from=builder /lib/ld-musl-*.so.1 /lib/
COPY --from=builder \
/usr/lib/libstdc++.so.6 \
/usr/lib/libgcc_s.so.1 \
/usr/lib/
# Copy zigbee2mqtt binary
COPY --from=builder /zigbee2mqtt/zigbee2mqtt /zigbee2mqtt/zigbee2mqtt
# Add bindings.node for serialport
COPY --from=builder \
/zigbee2mqtt/node_modules/@serialport/bindings-cpp/prebuilds/ \
/zigbee2mqtt/node_modules/@serialport/bindings-cpp/prebuilds/
# Create default data directory
# Will fail at runtime due missing the mkdir binary
COPY --from=builder /data /data
# Add example config, also create the /config dir
COPY examples/compose/config/configuration.yaml ${ZIGBEE2MQTT_CONFIG}
EXPOSE 8080
USER zigbee2mqtt
WORKDIR /zigbee2mqtt
ENTRYPOINT ["./zigbee2mqtt"]