Mini PC + Windows + WSL2 + Docker + homeassistant

First, let me say the conclusion: WSL2 turns on the mirror network, then install Docker in WSL2, and finally install the Docker version of homeassistant.

Do not use Docker Desktop For Windows!

Some people may say, why not just install HAOS, or use linux + Docker + homeassistant?

Because my mini PC is directly connected to the TV, it is also used for HTPC, back up photos to NTFS formatted hard drives via SMB, and other functions, that is All in boom.

Tutorial:

  1. Install WSL2 in Windows.

  2. Turn on the WSL2 mirror network. This operation is to allow homeassistant to use the network of the Windows host to access other devices in the LAN.
    Refer to the official document: https://learn.microsoft.com/en-us/windows/wsl/wsl-config#wslconfig.

Create a new file .wslconfig in the C:\Users\<your name>\ directory, such as my configuration,

[wsl2]
processors=2
memory=2GB
swap=2GB
networkingMode=mirrored
dnsTunneling=true

[experimental]
autoMemoryReclaim=dropcache
hostAddressLoopback=true

Shut down WSL2 and wait for 8 seconds.

wsl --shutdown
  1. Enter WSL2 and install Docker, refer to the official document (ubuntu): https://docs.docker.com/engine/install/ubuntu/.

  2. In order to persist data to Windows, enter the Windows Documents directory in WSL2 and install homeassistant,

cd /mnt/c/Users/<your name>/Documents/
mkdir homeassistant
cd homeassistant

According to the official document, install homeassistant container and finally start homeassistant.

  1. Set WSL2 to start at startup, go to the C:\Users\<your name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup directory, create a new wsl2.vbs,
CreateObject("Wscript.Shell").run "wsl -d Ubuntu",vbhide

Other pitfalls:

Q1: PostgreSQL cannot save initialization data to Windows when installing immich.

Edit /etc/wsl.config in WSL2, add automount, then restart WSL2.

For example,

[boot]
systemd=true

[automount]
options = "metadata"

Q2: Unable to access immich.

You can only use a compromise method, listen to the port through nginx, then proxy the request.

For example, nginx.conf,

server {
    listen 3001;
    server_name _;

    location / {
        proxy_pass http://172.18.0.5:3001;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
    }
}

Note: 172.18.0.5 is the ip of immich in the WSL2 Docker bridge.


For Chinese:

中文版:

迷你主机 + Windows + WSL2 + Docker + homeassistant

先说结论:WSL2 开启镜像网络,然后在 WSL2 里安装 Docker,最后安装 Docker 版 homeassistant。

不要使用 Docker Desktop For Windows!

可能有人会说,为什么不直接安装 HAOS,或者使用 linux + Docker + homeassistant ?

因为我的迷你主机直接连着电视,用于 HTPC (用浏览器看爱优腾,不用开电视会员),通过 SMB 备份照片到 NTFS 格式的硬盘,安装网盘客户端、 12306 bypass 等功能,也就是 All in boom。

教程:

  1. Windows 中安装 WSL2。

  2. 开启 WSL2 镜像网络。这个操作是为了让 homeassistant 可以使用 Windows 主机的网段,访问局域网内其他设备。
    参考官方文档。

C:\Users\<your name>\ 目录下新建文件 .wslconfig,例如我的配置,

[wsl2]
processors=2
memory=2GB
swap=2GB
networkingMode=mirrored
dnsTunneling=true

[experimental]
autoMemoryReclaim=dropcache
hostAddressLoopback=true

关闭 WSL2,并等待 8 秒,

wsl --shutdown
  1. 进入 WSL2,并安装 Docker,参考官方文档(ubuntu)。

  2. 为了持久化数据到 Windows,在 WSL2 中进入主机目录,安装 homeassistant,

cd /mnt/c/Users/<your name>/Documents/
mkdir homeassistant
cd homeassistant

按照官方文档安装 Docker 版 homeassistant,最后启动 homeassistant。

  1. 设置开机启动 WSL2,进入 C:\Users\<your name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 目录,新建 wsl2.vbs,
CreateObject("Wscript.Shell").run "wsl -d Ubuntu",vbhide

其他踩坑:

Q1:安装 immich 时 PostgreSQL 无法初始化。

WSL2 里编辑 /etc/wsl.config,添加 automount,然后重启 WSL2。

例如,

[boot]
systemd=true

[automount]
options = "metadata"

Q2:无法访问 immich。

只能使用妥协的办法,通过 nginx 监听端口,然后转发请求。

例如 nginx.conf,

server {
    listen       3001;
    server_name  _;

    location / {
        proxy_pass         http://172.18.0.5:3001;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
    }
}

其中 172.18.0.5 是 immich 在 WSL2 Docker 网桥中的 ip。