Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added docker development environment #26669

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ Zigbee2MQTT uses TypeScript (partially for now). Therefore after making changes
Before running any of the commands, you'll first need to run `pnpm install --include=dev`.
Before submitting changes run `pnpm run test:coverage`, `pnpm run pretty:check` and `pnpm run eslint`

#### Developing in docker

You can use the development container with the corresponding `compose.dev.yml` to quickly spin up your development environment.

Before starting you need to run `docker compose -f docker/compose.dev.yml run zigbee2mqtt pnpm install --include=dev` to get the node_modules.

Also make sure to create your `configuration.yaml` under `data/configuration.yaml`

Then run `docker compose -f docker/compose.dev.yml up -d` from the repository-root to start zigbee2mqtt, it will then be accessible from `http://localhost:8080`.

Any commands can be prefixed with `docker compose -f docker/compose.dev.yml run zigbee2mqtt` to automatically run inside the container. For example to run the prettier linter:

```shell
docker compose -f docker/compose.dev.yml run zigbee2mqtt pnpm pretty:write
```

##### To add adapter

Open and edit the `compose.dev.yml` and input your device to map.

```yaml
services:
zigbee2mqtt:
# Other fields not shown
devices:
- /dev/serial/by-id/usb-ZEPHYR_Zigbee_NCP_DEADBEEF0000-if0:/dev/ttyACM0 # Note that you need to add /dev/ttyACM0 to config
```

## Supported devices

See [Supported devices](https://www.zigbee2mqtt.io/supported-devices) to check whether your device is supported. There is quite an extensive list, including devices from vendors like [Xiaomi](https://www.zigbee2mqtt.io/supported-devices/#v=Xiaomi), [Ikea](https://www.zigbee2mqtt.io/supported-devices/#v=IKEA), [Philips](https://www.zigbee2mqtt.io/supported-devices/#v=Philips), [OSRAM](https://www.zigbee2mqtt.io/supported-devices/#v=OSRAM) and more.
Expand Down
33 changes: 33 additions & 0 deletions docker/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
ARG TARGETPLATFORM

# Need to use Alpine 3.18.4 which uses Node 18 for arm/v6 and arm/v7, otherwise the build hangs.
# See https://github.com/nodejs/docker-node/issues/2077
FROM alpine:3.18.4 AS linux-arm-alpine
FROM alpine:3.21 AS linux-arm64-alpine
FROM alpine:3.21 AS linux-amd64-alpine
FROM alpine:3.21 AS linux-riscv64-alpine
FROM alpine:3.21 AS linux-386-alpine

FROM linux-${TARGETARCH}-alpine AS base

#ENV NODE_ENV=production
WORKDIR /app
RUN apk add --no-cache tzdata eudev tini nodejs

# Dependencies and build

COPY package.json pnpm-lock.yaml ./
# Make and such are needed to compile serialport for riscv64
RUN apk add make gcc g++ python3 linux-headers npm git && \
npm install -g [email protected] && \
pnpm install --include=dev && \
# serialport has outdated prebuilds that appear to fail on some archs, force build on target platform
rm -rf `find ./node_modules/.pnpm/ -wholename "*/@serialport/bindings-cpp/prebuilds" -type d` && \
pnpm rebuild @serialport/bindings-cpp

COPY docker/docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

COPY . /app
RUN pnpm build
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require rebuilding the container every time you change the container, which is not handy. I think it would be best to run npm run build:watch and an auto restart or something.

Copy link
Author

@captainlettuce captainlettuce Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the container is ran with the accompanying compose-file the repository root is bind-mounted into /app so no rebuild necessary afaict.

You can still run npm run build:watch in another terminal, or with docker compose -f docker/compose.dev.yml exec zigbee2mqtt npm run build:watch for running inside the container.

A proper live-reload with the pnpm start command would probably be preferable but I'm not that comfortable with node.js unfortunately

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we can just re-use https://github.com/Koenkk/zigbee2mqtt/blob/master/docker/Dockerfile, as this separate Dockerfile add some extra maintenance. I think this can be done by specifying the target in the docker-compose.dev.yaml. Then the user just has to run pnpm start to start Z2M

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that seems doable, I'll update when i get some time

CMD pnpm start
15 changes: 15 additions & 0 deletions docker/compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
zigbee2mqtt:
build:
context: ../
dockerfile: docker/Dockerfile.dev
volumes:
- ../:/app
# devices:
# - /dev/serial/by-id/<YOUR-DEVICE-ID>:/dev/ttyACM0
ports:
- 127.0.0.1:8080:8080
mqtt:
image: eclipse-mosquitto:2.0
container_name: mqtt
command: 'mosquitto -c /mosquitto-no-auth.conf'