
我希望這是一件容易的事。我正在使用 Docker 在 Raspberry Pi 4 上運行 Homeassistant 和 Node-Red 容器。
基於本指南他們建議製作一個 DockerFile 來安裝 Node-Red 節點以支援 Homeassistant。安裝是透過npm
Node-Red 容器內的命令進行處理的。
我在一個目錄中創建了docker-compose.yaml
和Dockerfile
(文件內容如下)。
docker-compose up
為了啟動容器,我在文件所在的目錄中使用命令docker-compose.yaml
。我還製作了一個 systemd 服務,它可以完成相同的事情,沒有任何問題。
我的問題是DockerFile
當我運行時沒有得到處理docker-compose up
有沒有辦法在每次容器啟動/重新啟動時docker-compose
取得變更?DockerFile
[docker-compose.yaml]
version: "3.6"
services:
node-red:
build: .
container_name: node-red
environment:
TZ: /etc/localtime
image: nodered/node-red
restart: unless-stopped
ports:
- "1880:1880"
volumes:
- "/[folders]/node-red/data:/data"
[DockerFile]
FROM nodered/node-red
RUN npm install node-red-contrib-actionflows \
# https://flows.nodered.org/node/node-red-contrib-actionflows
node-red-contrib-home-assistant-websocket \
# https://flows.nodered.org/node/node-red-contrib-home-assistant-websocket
node-red-contrib-stoptimer \
# https://flows.nodered.org/node/node-red-contrib-stoptimer
node-red-contrib-time-range-switch \
# A simple Node-RED node that routes messages depending on the time. If the current time falls within the range specified in the node configuration, the message is routed to output 1. Otherwise the message is routed to output 2.
node-red-contrib-timecheck \
# Is it that time yet? This node compares a given time to the current time.
node-red-node-timeswitch
# node to provide a simple timeswitch node to schedule daily on/off events
答案1
我剛剛做了一些快速測試,我相信您這裡有兩個問題,一個很簡單,另一個實際上非常微妙。
第一個問題是,如果鏡像已經存在,Compose 不會自動重建服務的鏡像。當您第一次執行服務時,Compose 將使用 dockerfile 建置映像,但在後續執行中,它將僅重新使用已經存在的映像。文件實際上對此不清楚,但是docker-compose up
從範例檔案運行時的命令輸出更有幫助:
...
WARNING: Image for service node-red was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
...
我相信,為了獲得您正在尋找的行為,您應該修改您的 systemd 服務以使用該--build
標誌,該標誌將觸發重建,無論是否存在具有相同標籤的圖像。
我相信您遇到的第二個問題(這也是基於我使用範例配置的修改版本運行的快速測試)是您在每個建置上覆蓋基本映像標籤。
根據build
指令的 Compose 檔案參考部分(第三塊位於這個連結)當為服務指定了build
和時image
(就像您的服務一樣),docker 將根據建置指令建置映像,然後使用 image 指令的值對其進行標記。因為image
docker-compose 檔案中的指令和FROM
dockerfile 中的指令使用相同的標籤,所以您告訴 Docker 使用與 dockerfile 從中提取的基本映像相同的名稱來標記本機建置的映像。
這意味著容器的每次重建(在第一次建置之後)實際上將使用其自身的先前版本作為基礎容器,而不是使用nodered/node-red
上游的版本。
image
要解決此問題,只需將compose 檔案中的指令值變更為其他值即可;例如local-custom-node-red