中秋烤肉節要烤(Baking)一個自己合用的firmware,讓我聯想到黑猩猩也會的技能,把樹枝削尖成長矛去打獵了,但是人類就是要烤一下,因為這樣比較Man,頭也比較硬。
我自己的方法就是從Github上取下Dockerfile,然後從裡面開始編譯,聽起來很簡單,但是實做是曠日廢時的try & error,汗與血淚。
我的Dockerfile也有在github公開,因此我就快轉到從Dockerfile開始
icekimo@HPE8300-SFF:/tmp$ cd ~/Documents/OpWrt/docker-openwrt-buildroot/18.06/archerC7v5
icekimo@HPE8300-SFF:~/Documents/OpWrt/docker-openwrt-buildroot/18.06/archerC7v5$ docker build -t buildopenwrt .
好死不死,馬上就掉入第一個坑
Step 8/11 : RUN git clone git://git.openwrt.org/openwrt/openwrt.git -b openwrt-18.06
---> Running in a5d1a6387eaf
Cloning into 'openwrt'...
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
The command '/bin/sh -c git clone git://git.openwrt.org/openwrt/openwrt.git -b openwrt-18.06' returned a non-zero code: 128
白話的說,git因為clone時遇到意外的字元EOF就提前GG了。這是人品問題,你可以重複跑上一天,然後還是死在同樣的地方,而最氣人的是它只有大約150MB。我們來看一下案發現場。
icekimo@HPE8300-SFF:~/Documents/OpWrt/docker-openwrt-buildroot/18.06/archerC7v5$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 659316ae2426 42 minutes ago 548MB
ubuntu 18.04 a2a15febcdf3 5 days ago 64.2MB
#因為死在半路,所以最後container也被退出了。
icekimo@HPE8300-SFF:~/Documents/OpWrt/docker-openwrt-buildroot/18.06/archerC7v5$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#沒關係,如果你討厭看到Loser,你可以進行prune
icekimo@HPE8300-SFF:~/Documents/OpWrt/docker-openwrt-buildroot/18.06/archerC7v5$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
a5d1a6387eafe17b3acbc921cb30008502b419be085e0b85a93dece21972b75c
Total reclaimed space: 0B
但是真正的大師應該看透背後的原因,但我不是猜測是sh搞的鬼。在此提供一個作弊的方法,你只要先去/tmp做一次git clone,然後docker裡面的git就會順利過關。聽起來神對吧?就像賽跑前,你先去跑一次,然後比賽時就會正常發揮實力的道理(我知道很扯,但它常發生) ,牆國人以為是牆壁的關係,但那就題外話了。
icekimo@HPE8300-SFF:/tmp$ git clone git://git.openwrt.org/openwrt/openwrt.git -b openwrt-18.06
Cloning into 'openwrt'...
remote: Enumerating objects: 475691, done.
remote: Counting objects: 100% (475691/475691), done.
remote: Compressing objects: 100% (131125/131125), done.
remote: Total 475691 (delta 329327), reused 468697 (delta 324445)
Receiving objects: 100% (475691/475691), 147.77 MiB | 557.00 KiB/s, done.
Resolving deltas: 100% (329327/329327), done.
接下來重跑,訊息就變成
Step 8/11 : RUN git clone git://git.openwrt.org/openwrt/openwrt.git -b openwrt-18.06
---> Running in 3050533a875f
Cloning into 'openwrt'...
Removing intermediate container 3050533a875f
---> b07dfa3bf60f
Step 9/11 : RUN ~/openwrt/scripts/feeds update -a && ~/openwrt/scripts/feeds install shadow sudo bash rsync nginx openssh tmux vim git git-gitweb gitolite fossil autossh fwknop fwknopd python3 php7 python screen squid acme clamav ddns-scripts luci luci-app-fwknopd luci-app-ddns django samba36-server samba36-net luci-app-samba luci-app-wol libpam libssh2 libgnutls libidn2
很神奇吧?It works, and I don't know why. 接下來趁機講解一個狀況。後來我修改Dockerfile,將最後兩個RUN合併,減少docker image的層數,而重新執行的過程(註解說明)是
icekimo@HPE8300-SFF:~/Documents/OpWrt/docker-openwrt-buildroot/18.06/archerC7v5$ docker build -t buildopenwrt .
Sending build context to Docker daemon 148.5kB
Step 1/10 : FROM ubuntu:18.04
---> a2a15febcdf3
Step 2/10 : LABEL description="Create Openwrt Build Docker"
---> Using cache
---> 1c65dff5a20e
# 將系統基礎編譯工具安裝完畢
Step 3/10 : RUN apt-get update && apt-get install -y sudo time git-core subversion build-essential gcc-multilib quilt rsync libncurses5-dev zlib1g-dev gawk flex gettext wget unzip python vim && apt-get clean
---> Using cache
---> 8574e9d23e03
# 製作環境使用者
Step 4/10 : RUN useradd -m openwrt && echo 'openwrt ALL=NOPASSWD: ALL' > /etc/sudoers.d/openwrt
---> Using cache
---> f1184daf1072
Step 5/10 : USER openwrt
---> Using cache
---> 3fa145ad696b
Step 6/10 : WORKDIR /home/openwrt
---> Using cache
---> e8030b68d0e0
Step 7/10 : ENV HOME="/home/openwrt"
---> Using cache
---> 659316ae2426
# 開始下載原始碼
Step 8/10 : RUN git clone git://git.openwrt.org/openwrt/openwrt.git -b openwrt-18.06
---> Using cache
---> b07dfa3bf60f
# 開始下載偏愛的套件
Step 9/10 : RUN ~/openwrt/scripts/feeds update -a && ~/openwrt/scripts/feeds install shadow sudo bash rsync nginx openssh tmux vim git git-gitweb gitolite fossil autossh fwknop fwknopd python3 php7 python screen squid acme clamav ddns-scripts luci luci-app-fwknopd luci-app-ddns django samba36-server samba36-net luci-app-samba luci-app-wol libpam libssh2 libgnutls libidn2
---> Using cache
---> aec3a72927b9
# 置入上次編譯成功的設定檔,並提前下載完套件原始碼
Step 10/10 : RUN cd /home/openwrt/openwrt && wget https://raw.githubusercontent.com/August-Icekimo/docker-openwrt-buildroot/master/18.06/archerC7v5/ac7v5.config -O .config && make download
---> Running in a3ed0ceb749e
--2019-08-21 07:22:02-- https://raw.githubusercontent.com/August-Icekimo/docker-openwrt-buildroot/master/18.06/archerC7v5/ac7v5.config
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 144391 (141K) [text/plain]
Saving to: '.config'
0K .......... .......... .......... .......... .......... 35% 341K 0s
50K .......... .......... .......... .......... .......... 70% 1.55M 0s
100K .......... .......... .......... .......... . 100% 925K=0.2s
2019-08-21 07:22:03 (634 KB/s) - '.config' saved [144391/144391]
time: target/linux/prereq#0.43#0.02#0.46
Checking 'rsync'... ok.
WARNING: your configuration is out of sync. Please run make menuconfig, oldconfig or defconfig!
make[1] tools/flock/compile
make[2] -C tools/flock compile
make[1] tools/download
make[2] -C tools/gmp download
<......>
make[2] -C package/utils/util-linux download
make[1] target/download
make[2] -C target/linux download
Removing intermediate container a3ed0ceb749e
---> 0587f8906d87
Successfully built 0587f8906d87
Successfully tagged buildopenwrt:latest
終於,藉由對docker技術堅定不移的信心,我們看到了成功的曙光,趕快打開烤箱看看。
icekimo@HPE8300-SFF:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
buildopenwrt latest 0587f8906d87 17 minutes ago 1.5GB
ubuntu 18.04 a2a15febcdf3 6 days ago 64.2MB
是的,裡面image已經做好了,雖然肥了點,不過油滋滋的也是一種美味(畢竟1.5GB的源碼最後烤出來的大小會讓各位嚇一跳)
icekimo@HPE8300-SFF:~$ docker ps # 先看看目前是空的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
icekimo@HPE8300-SFF:~$ docker images # 再看看image的名字和編號
REPOSITORY TAG IMAGE ID CREATED SIZE
buildopenwrt latest 0587f8906d87 17 minutes ago 1.5GB
ubuntu 18.04 a2a15febcdf3 6 days ago 64.2MB
icekimo@HPE8300-SFF:~$ docker run -it -d --name bakingC7V5 buildopenwrt # 決定了,container取名叫做bakingC7V5
01ea25dd8a2945199c93590df10f33dfc29d3a28d49b287b7ccc4f9dcb55e0f3
icekimo@HPE8300-SFF:~$ docker ps # 看看跑得快不快
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01ea25dd8a29 buildopenwrt "/bin/bash" 8 seconds ago Up 7 seconds bakingC7V5
因為這階段編譯環境需要一點手動介入去改設定,所以前面docker run用-d參數讓這個container持續活著,然後我們打開另一道門進去做menuconfig
icekimo@HPE8300-SFF:~$ docker exec -it 01ea /bin/bash
openwrt@01ea25dd8a29:~$ cd openwrt ; make menuconfig