容器化實務操作,以 Taiko-Web 為例
本篇我們要把一個稍微陳舊的項目 Docker 化,方便沒有 Linux 伺服器的使用者們也可以快速運行服務。
此處我們選用的服務是 2011 就推出,但在 2020 以降才開始紅起來,但也因此被萬代版權方盯上,後來被全網封殺掉的 bui 製作的 Taiko Web。
但開源的力量是無比強大,我早已 fork 了一份備份出來,所以依然保有其原始碼。開源社群的迷人之處就在這裡,你說是吧。
該項目是一個大部分使用 JavaScript 刻出來的網頁版太鼓達人模擬器,其透過前端技術就可以讓用戶流暢地遊玩,十分方便,令人印象深刻。但今天我們不討論其服務內容,本篇我們將著重在將一個運行在 Linux 上的服務給容器化——以便用戶快速自行架設服務,或甚至跨平台運行——的過程。
各位可以一邊對照原作者的 manual setup,一邊看我們 dockerfile 的對應做法,方便理解。那我們開始咯。
安裝過程與重新打包
Installing the requirements
原始的安裝方式為:
If you have not already done so, install the above-listed software on your server. The following commands were ran on a Debian 10 system, you may need to alter them depending on your OS.
1
2
3 su
apt update
apt install git python3-pip nginxIt is recommended to install these optional packages as well.
1 apt install ffmpeg redis supervisor python3-virtualenv
我們的做法依樣畫葫蘆,就是安裝依賴:
1 | FROM debian:bookworm |
但可以注意到我們不需要 sudo
,因為在 docker 的環境裡面我們本來就是 root。
Installing MongoDB
原始的安裝方式為:
Consult the MongoDB installation tutorial for your operating system to install MongoDB Community Edition on your system.
簡單來說就是 MongoDB 並不能用 apt-get
的方式來安裝,所以大部分會建議自己去官方那邊慢慢照著指引安裝。近來這種狀況好像越來越常見了,我個人是不太樂見這樣的走勢啦。
那此時我們如何應對呢?我們這樣做:
1 | RUN curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \ |
因為我們 docker 的環境指定了 debian:bookworm,所以我們從 MongoDB 官方那邊找到對應的安裝方式,把指令全部移植過來即是。
Obtaining taiko-web
原始做法:
Next, clone the taiko-web repository into a directory of your choosing.
1
2
3 su -c 'mkdir -p /srv/taiko-web'
su -c 'chown $USER /srv/taiko-web'
git clone https://github.com/bui/taiko-web.git /srv/taiko-webAll the commands you run from now on must be ran inside the taiko-web directory, so change your working directory.
1 cd /srv/taiko-webRun the following commands to install requirements and set default configurations.
1
2
3
4 pip3 install -r requirements.txt
tools/get_version.sh
cp tools/hooks/* .git/hooks/
cp config.example.py config.pyEdit config.py in a text editor to configure taiko-web for your system.
我們的做法:
1 | RUN mkdir -p /srv/taiko-web /var/log/taiko-web /data/db |
幾點差異:
- 因為在 docker 中我們是 root,所以不用
chown
來更改權限。 - bui 大大的 repo 已經被封殺了,所以改從我那邊 clone 檔案。
- 我們在 pip3 install 中加了
--break-system-packages
的 flag,因為反正我們是專用的環境,沒有在系統管乾不乾淨的,所以套件直接裝在系統層級,不走 venvs。(而因為 PEP668,你若不加這個 flag 他會報錯跟你提醒)
nginx setup
原始做法:
Let’s set up nginx now. Copy the example virtual host file
tools/nginx.conf
to/etc/nginx/conf.d/taiko-web.conf
, changing theserver_name
androot
statements as required.
1
2 >su -
>cp /srv/taiko-web/tools/nginx.conf /etc/nginx/conf.d/taiko-web.confNext, open
/etc/nginx/nginx.conf
and comment out the line which displays the default nginx page.
1 >include /etc/nginx/sites-enabled/*;becomes
1 >#include /etc/nginx/sites-enabled/*;Then, open
/etc/nginx/mime.types
and add this line near the bottom:
1 >application/wasm wasm;Once you’ve configured the files, reload nginx (as root). Assuming everything was configured correctly, you shouldn’t get any errors.
1 >nginx -s reloadIf you try to access your simulator now, you should get a 502 error. That’s because we haven’t started the app server yet, so let’s do that!
我們的做法:
1 | RUN cp /srv/taiko-web/tools/nginx.conf /etc/nginx/conf.d/taiko-web.conf && \ |
本來手動修改 config 的流程,我們用 sed
來自動修改。
taiko-web setup
原始:
We’ll first create virtual environments for Python and install the required modules.
1
2
3
4 virtualenv -p python3 /srv/taiko-web/.venv
source /srv/taiko-web/.venv/bin/activate
pip install -r /srv/taiko-web/requirements.txt
deactivateIt is recommended that you use a process manager such as Supervisor to keep taiko-web running at all times.
Copy the example configuration file from
tools/supervisor.conf
to/etc/supervisor/conf.d/taiko-web.conf
, adjusting it as needed.
1
2
3 su
mkdir -p /var/log/taiko-web
cp tools/supervisor.conf /etc/supervisor/conf.d/taiko-web.confThen, restart Supervisor (as root).
1
2 su -
service supervisor restartAnd you can check the status of supervisor with this command, to see if anything went wrong (also as root).
1 supervisorctl statusWe’re almost there! taiko-web has been installed and when you start the database you will be able to run it too, but there will be no default songs. Let’s fix that!
可以注意到這裡開了一個 virtualenv,但我們 docker 環境不管那麼多,所以不理他,我們直接運行:
1 | RUN printf "[supervisord]\nnodaemon=true\n\ |
打包結果與部署
綜合以上,我們的 dockerfile 就長這樣:
1 | FROM debian:bookworm |
部署的方式就非常簡單了,只要在有 docker 的環境中,將以上檔案存檔為 Dockerfile
(記得大寫),並在該資料夾中執行 docker build -t taiko-web .
,上述的步驟就全部幫你做好,打包成一個 image,並且只要你是 x86 架構的電腦,就都能成。
至於你問 ARM 架構為什麼不行?主要是 MongoDB 的問題,因為 docker 指定了 debian,所以 Mongo 就給你 x86 的版本的檔案。那是不是 Mongo 開另一個 container 就可以?是這樣的沒錯,因為 MongoDB 有提供 ARM 架構的 image,這部分可以留給讀者作為練習。(欸)
最後,我們 docker run -d -p 80:80 --name taiko-web taiko-web
來運行這個 image,伺服器就此上線。此時就可以用該設備訪問 http://localhost
,或是用同內網的設備使用瀏覽器訪問 http://運行docker的設備內網ip位置
。
以上!