我有一個現在可以在 Ubuntu 上運行的應用程式,但最終它應該可以在 Raspberry Pi 上運行。使用 docker-compose up 打開整個應用程式。它里面有三個容器,其中一個是用 Python 撰寫的 Flask Api。現在我需要為我的 Flask Api 添加兩個端點。一種用于系統關閉,一種用于系統重啟。
我想撰寫一些 python 代碼或 bash 腳本來執行此操作,但這里的問題是一切都在 docker 上作業。因此,例如簡單的 linux 命令shutdown -h now不起作用,因為 docker 告訴您未找到關閉。是否可以從 docker 中運行的應用程式關閉 ubuntu/樹莓派?
uj5u.com熱心網友回復:
在基于 Debian 的系統上(假設您在樹莓派上運行 raspbian)shutdown并且reboot只是指向systemctl.
因此,您需要能夠systemctl從 docker 中在主機上運行命令。這可以通過幾個卷安裝來實作。以下是該docker-compose檔案的示例片段:
ShutdownContainer:
image: ShutdownImage:latest
links:
- "rebootContainer:latest"
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup
- /bin/systemctl:/bin/systemctl
- /run/systemd/system:/run/systemd/system
- /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket
- /sys/fs/cgroup:/sys/fs/cgroup
command:
- "startYourWebserver"
映射這些卷后,您應該能夠運行systemctl shutdown或systemctl reboot從 docker 容器中運行。可能需要以特權模式運行容器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/523740.html
