[心得] 手機跑docker

看板MobileComm作者 (內容農場殺手)時間2年前 (2021/12/19 14:18), 2年前編輯推噓27(2706)
留言33則, 27人參與, 2年前最新討論串1/1
We need to go deeper... 基本上,Android手機沒辦法直接跑docker,因為kernel根本就不支援。雖然如此,我們還 有QEMU虛擬機呀,可惜KVM也沒辦法用。 網誌好讀版: https://bit.ly/3FeHQTN 架構:Termux模擬器建立QEMU虛擬機,虛擬機裡面安裝Docker,然後再通訊埠轉發。 以簡單的Apache伺服器為例,看能否在手機的瀏覽器,看到虛擬機的docker所建立的網頁。 - 手機: Sony Xperia 5 II,Android 11,8GB RAM。 網頁內容: https://i.imgur.com/JaeWjno.png
## 建立虛擬機&安裝Docker 1. 開啟Termux (建議安裝[F-droid](https://f-droid.org/en/packages/com.termux/)版 本的),輸入指令: ```bash pkg install qemu-utils qemu-common qemu-system-x86_64-headless ``` 2. 下載內含virt的Alpine Linux作業系統iso ```bash mkdir alpine && cd $_ wget http://dl-cdn.alpinelinux.org/alpine/v3.12/releases/x86_64/alpine-virt-3.12 .3-x86_64.iso ``` 3. 建立4G的虛擬硬碟 ```bash qemu-img create -f qcow2 alpine.img 4G ``` 4. 開機 ```bash qemu-system-x86_64 -machine q35 -m 1024 -smp cpus=2 -cpu qemu64 \ -drive if=pflash,format=raw,read-only,file=$PREFIX/share/qemu/edk2-x86_64-code .fd \ -netdev user,id=n1,hostfwd=tcp::2222-:22 -device virtio-net,netdev=n1 \ -cdrom alpine-virt-3.12.3-x86_64.iso \ -nographic alpine.img ``` 5. 使用`root`帳號登入,啟用網路卡。 ```bash setup-interfaces #Available interfaces are: eth0. #Enter '?' for help on bridges, bonding and vlans. #Which one do you want to initialize? (or '?' or 'done') [eth0] #Ip address for eth0? (or 'dhcp', 'none', '?') [dhcp] #Do you want to do any manual network configuration? [no] ifup eth0 ``` 6. 使用oofnikj準備好的answerfile安裝系統 ```bash wget https://gist.githubusercontent.com/oofnikj/e79aef095cd08756f7f26ed244355d62 /raw/answerfile ``` 7. 讓開機時能輸出訊息 ```bash sed -i -E 's/(local kernel_opts)=.*/\1="console=ttyS0"/' /sbin/setup-disk ``` 8. 安裝系統至硬碟,期間會要求建立root帳號的密碼。 ```bash setup-alpine -f answerfile ``` 9. 先用`poweroff`指令關機,以後就用這條指令啟動虛擬機(可自行寫成shell): ```bash qemu-system-x86_64 -machine q35 -m 1024 -smp cpus=2 -cpu qemu64 \ -drive if=pflash,format=raw,read-only,file=$PREFIX/share/qemu/edk2-x86_64-code .fd \ -netdev user,id=n1,hostfwd=tcp::2222-:22,hostfwd=tcp::8081-:80 -device virtio- net,netdev=n1 \ -nographic alpine.img #m是分配1GB記憶體,cpu是2核CPU,hostfwd則是把外部8081通訊埠轉發到虛擬機的80通訊 埠。 ``` 10. 安裝docker,並設定開機自動啟動服務。 ```bash apk update && apk add docker service docker start rc-update add docker ``` ## 安裝Apache伺服器 1. Pull Apache伺服器 ```bash docker pull httpd ``` 2. 執行Apache,將虛擬機的80通訊埠轉發到Docker容器的80通訊埠,並將docker容器的檔 案路徑對應到系統的`/root/website`目錄。 ```bash docker run -d -p 80:80 --name myapache -v /root/website/:/usr/local/apache2/htdo cs/ httpd ``` 3. 建立一個測試網頁 ```bash mkdir website && cd website vi index.html ``` 4. 網頁內填入: ```html <!DOCTYPE html> <html> <body> <h1>My Apache Server</h1> <img src="https://c.tenor.com/61yCyJVoyr8AAAAd/%E6%A1%B6%E7%A5%9E-%E6%89%93%E5%B 7%A5.gif"> </body> </html> ``` 5. 手機開啟Chrome瀏覽器,輸入`localhost:8081/index.html`,成功看到Docker的網頁。 https://i.imgur.com/IdIe6Qg.png
## 總結 虛擬機+Docker跑Apache Server似乎還行,不過若執行Minecraft伺服器這類的,性能的損 耗就很明顯了。 如果可以省去虛擬化這一層,那麼docker的性能就可以大幅提升,也不用等Alpine Linux花 30秒開機。 附帶一提,iOS有基於Alpine Linux的iSH終端模擬器,理論上也可以如法炮製跑docker。 ## 參考資料 - [Docker on Termux in a VM](https://gist.github.com/oofnikj/e79aef095cd08756f7f 26ed244355d62) - [Docker - 第十三章 | 安裝Apache Server](https://morosedog.gitlab.io/docker-201 90601-docker13/) -- https://i.imgur.com/qBnCgUO.jpg
https://i.imgur.com/klpjZcQ.jpg
https://i.imgur.com/yLTmoHs.jpg
https://i.imgur.com/WepO17T.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.75.34.115 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/MobileComm/M.1639894727.A.EC5.html

12/19 14:28, 2年前 , 1F
推實驗精神
12/19 14:28, 1F

12/19 14:34, 2年前 , 2F
強!
12/19 14:34, 2F

12/19 14:40, 2年前 , 3F
以後手機也可以當伺服器了(?
12/19 14:40, 3F

12/19 14:47, 2年前 , 4F
12/19 14:47, 4F

12/19 14:51, 2年前 , 5F
一直都可以啊
12/19 14:51, 5F

12/19 15:18, 2年前 , 6F
要注意log4j阿
12/19 15:18, 6F
我執行的是C++的基岩版,所以才會用到docker,不過謝謝提醒 ※ 編輯: ivon852 (42.77.129.10 臺灣), 12/19/2021 15:32:14 ※ 編輯: ivon852 (42.77.129.10 臺灣), 12/19/2021 15:34:44

12/19 15:55, 2年前 , 7F
好猛!!
12/19 15:55, 7F

12/19 15:57, 2年前 , 8F
先用Termux模擬器再用QEMU下再建立容器..效能折損很大吧
12/19 15:57, 8F

12/19 17:03, 2年前 , 9F
上次測試,慢到要有點耐心
12/19 17:03, 9F

12/19 17:55, 2年前 , 10F
收藏
12/19 17:55, 10F

12/19 18:46, 2年前 , 11F
好玩但一點都不實用的功能 XD
12/19 18:46, 11F

12/19 18:50, 2年前 , 12F
有機會看到手機app全都在容器內跑嗎 XD
12/19 18:50, 12F

12/19 18:53, 2年前 , 13F
那必須再裝一個Docker-Android來跑。但支援手機款式很少
12/19 18:53, 13F

12/19 18:53, 2年前 , 14F
應該會慢到難以想像
12/19 18:53, 14F

12/19 19:10, 2年前 , 15F
我只知道docking
12/19 19:10, 15F

12/19 19:21, 2年前 , 16F
推 長知識了
12/19 19:21, 16F

12/19 19:50, 2年前 , 17F
12/19 19:50, 17F

12/19 19:50, 2年前 , 18F
真屌
12/19 19:50, 18F

12/19 20:06, 2年前 , 19F
好屌
12/19 20:06, 19F

12/19 20:37, 2年前 , 20F
原生的:
12/19 20:37, 20F

12/19 20:37, 2年前 , 21F

12/19 20:37, 2年前 , 22F
f3951cb62d74bd770dce27
12/19 20:37, 22F
改kernel難度更大,但是...也不是不可能

12/19 20:51, 2年前 , 23F
太hardcore了!
12/19 20:51, 23F
※ 編輯: ivon852 (111.255.34.70 臺灣), 12/19/2021 21:27:30

12/19 22:12, 2年前 , 24F
好厲害樓主是自學程式嗎?
12/19 22:12, 24F
沒什麼,這個僅是按照外國高手的教學步驟實驗的。 ※ 編輯: ivon852 (111.255.34.70 臺灣), 12/19/2021 22:36:31

12/19 22:51, 2年前 , 25F
12/19 22:51, 25F

12/19 23:27, 2年前 , 26F
12/19 23:27, 26F

12/19 23:43, 2年前 , 27F
12/19 23:43, 27F

12/20 13:03, 2年前 , 28F
會改內核就可以了,不過找不到相關的中文教學,有誰可以
12/20 13:03, 28F

12/20 13:03, 2年前 , 29F
教一下怎麼改內核嗎
12/20 13:03, 29F
改kernel等同要從原始碼重新編譯kernel

12/20 13:06, 2年前 , 30F
手機可以跑docker! 下次把yolo的docker包進來看看
12/20 13:06, 30F

12/20 19:07, 2年前 , 31F
樓上等等,不是有tf.js,難道你要用Mali來做訓練
12/20 19:07, 31F

12/20 19:09, 2年前 , 32F
之後該不會還可以多台手機K8s
12/20 19:09, 32F
※ 編輯: ivon852 (180.176.66.2 臺灣), 12/21/2021 00:18:01 ※ 編輯: ivon852 (180.176.66.2 臺灣), 12/21/2021 00:19:54

12/21 09:21, 2年前 , 33F
12/21 09:21, 33F
文章代碼(AID): #1Xlix7x5 (MobileComm)