2022-06-12 16:09:01 -07:00
|
|
|
# wake-on-lan-service
|
|
|
|
|
|
|
|
Remotely wake up your servers ... as-a-service!
|
|
|
|
|
|
|
|
**The problem:** Docker prevents bridge-networked containers from sending
|
2022-06-12 20:06:09 -07:00
|
|
|
wake-on-LAN packets outside of the Docker network. That means standard Docker
|
2022-06-12 16:09:01 -07:00
|
|
|
containers can't wake up remote machines. As a work-around, you can put them
|
|
|
|
on the Docker host network, but that has security and interoperability
|
|
|
|
implications—you may not want to expose your containers that way.
|
|
|
|
|
|
|
|
**The solution:** wake-on-lan-service runs on Docker's host network and sends
|
2022-06-12 20:06:09 -07:00
|
|
|
wake-on-LAN packets *on behalf of* other containers running on a standard
|
2022-06-12 16:09:01 -07:00
|
|
|
bridge network. This allows them to wake up remote machines—without themselves
|
|
|
|
having to be on the host network.
|
|
|
|
|
|
|
|
You might find this service useful for use cases like running [Home
|
|
|
|
Assistant](https://www.home-assistant.io/) in a Docker container without
|
|
|
|
putting it on the host network—while still allowing it to wake up remote
|
|
|
|
machines.
|
|
|
|
|
|
|
|
Source at https://projects.torsion.org/witten/wake-on-lan-service
|
|
|
|
|
|
|
|
|
|
|
|
## How it works
|
|
|
|
|
|
|
|
This service listens on a port for TCP requests containing a MAC address to
|
2022-06-12 20:06:09 -07:00
|
|
|
send a wake-on-LAN packet to. It expects plain TCP requests, not HTTP.
|
2022-06-12 16:09:01 -07:00
|
|
|
|
|
|
|
|
|
|
|
## Environment variables
|
|
|
|
|
2022-06-12 20:06:09 -07:00
|
|
|
* `PORT`: The TCP port to listen on for wake-on-LAN requests, defaults to
|
2022-06-12 18:26:05 -07:00
|
|
|
18888. Don't forget to open this port in your firewall.
|
2022-06-12 16:09:01 -07:00
|
|
|
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
### Running the service
|
|
|
|
|
2022-06-12 16:21:33 -07:00
|
|
|
```bash
|
2022-06-12 16:09:01 -07:00
|
|
|
$ sudo docker run --detach --name wake-on-lan-service --host network \
|
|
|
|
--env PORT=18888
|
2023-02-24 15:48:16 -08:00
|
|
|
projects.torsion.org/witten/wake-on-lan-service
|
2022-06-12 16:09:01 -07:00
|
|
|
```
|
|
|
|
|
2022-06-12 16:21:33 -07:00
|
|
|
Or, with Docker Compose:
|
|
|
|
|
2022-06-12 16:23:56 -07:00
|
|
|
```yaml
|
2022-06-12 16:21:33 -07:00
|
|
|
services:
|
|
|
|
wake-on-lan-service:
|
2023-02-24 15:48:16 -08:00
|
|
|
image: projects.torsion.org/witten/wake-on-lan-service
|
2022-06-12 16:21:33 -07:00
|
|
|
restart: always
|
|
|
|
network_mode: host
|
|
|
|
environment:
|
|
|
|
PORT: 18888
|
|
|
|
```
|
|
|
|
|
2022-06-12 16:09:01 -07:00
|
|
|
### Using the service
|
|
|
|
|
|
|
|
From within another container running on a Docker bridge network (so, no need
|
|
|
|
to run on the host network), execute:
|
|
|
|
|
2022-06-12 16:21:33 -07:00
|
|
|
```bash
|
2022-06-12 18:26:05 -07:00
|
|
|
echo 00:00:0a:bb:28:fc | nc -N host.docker.internal 18888
|
2022-06-12 16:09:01 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
This example uses the OpenBSD netcat variant to send a TCP packet containing
|
|
|
|
the requested MAC address to wake. The packet is sent to port 18888 on the
|
2022-06-14 20:29:28 -07:00
|
|
|
host, where this container should be listening.
|
2022-06-12 16:09:01 -07:00
|
|
|
|
2022-06-14 20:29:28 -07:00
|
|
|
For this to work, you may need to configure your client container (the one
|
|
|
|
running the `echo` command) to support the special `host.docker.internal`
|
|
|
|
host. E.g., `docker run --add-host host.docker.internal:host-gateway ...`. Or,
|
|
|
|
with Docker Compose:
|
2022-06-12 16:21:33 -07:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
extra_hosts:
|
2022-06-12 18:26:05 -07:00
|
|
|
- "host.docker.internal:host-gateway"
|
2022-06-12 16:21:33 -07:00
|
|
|
```
|
2022-06-12 16:09:01 -07:00
|
|
|
|
|
|
|
Alternatively, on Linux, you can try using the IP `172.17.0.1`.
|
|
|
|
|
|
|
|
|
2022-06-12 18:26:05 -07:00
|
|
|
### Home Assistant
|
|
|
|
|
2022-06-12 18:32:15 -07:00
|
|
|
If you happen to be using Home Assistant in Docker, here's how you might
|
2022-06-12 20:06:09 -07:00
|
|
|
request wake-on-LAN of a remote server via wake-on-lan-service. This example
|
2022-06-12 18:32:15 -07:00
|
|
|
is of an automation action in your Home Assistant configuration:
|
2022-06-12 18:26:05 -07:00
|
|
|
|
2022-06-12 18:34:17 -07:00
|
|
|
```yaml
|
2022-06-12 18:26:05 -07:00
|
|
|
automation:
|
|
|
|
- alias: my automation
|
|
|
|
trigger: ...
|
|
|
|
action:
|
|
|
|
- service: shell_command.wake_my_server
|
|
|
|
|
|
|
|
shell_command:
|
2022-06-12 18:32:15 -07:00
|
|
|
wake_my_server: "echo 00:00:0a:bb:28:fc | nc host.docker.internal 18888"
|
2022-06-12 18:26:05 -07:00
|
|
|
```
|
|
|
|
|
2022-06-12 18:32:15 -07:00
|
|
|
(Home Assistant's Docker image includes netcat from busybox instead of OpenBSD
|
|
|
|
netcat, so omit the `-N` flag.)
|
|
|
|
|
2022-06-12 18:26:05 -07:00
|
|
|
|
2022-06-12 16:09:01 -07:00
|
|
|
## Security
|
|
|
|
|
|
|
|
Note that no authorization is performed on the service request, so be aware
|
|
|
|
that anyone with network access to this service can wake arbitrary hosts by
|
|
|
|
MAC address.
|