(2015-02-07 update: Fig 正式納入 Docker 體系旗下,用法差異見此)
前言
Docker 是作業系統層級的軟體虛擬化,有著封裝、快速啟動、低系統負載的特性。
對於個別指令或服務,可直接以 Docker 指令啟動容器 (container);但碰到多項服務共構的軟體專案,逐行輸入指令顯然沒有效率。於是採用 Fig 記錄整個專案所需的 container,包含連接埠、資料儲存等組態設定,爾後一行指令即可建立並啟動 container。
以下將以 Selenium 2 為例,示範 Fig 如何建立專案所需的多個 container。
Selenium
Selenium 發展至 2.0,由以往中央控管的驅動方式轉為鬆散耦合的多點執行,角色可分為 hub 與 node;前者負責控管及轉發任務、後者則執行測試工作。
若欲建立同時支援 Chrome 及 Firefox 的測試環境,則需採用以下 container:
- Selenium Grid Hub (selenium/hub)
- Selenium Grid Node (selenium/node-chrome)
- Selenium Grid Node (selenium/node-firefox)
並於命令列或以腳本執行以下指令:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
sudo docker run -d -p 4444:4444 --name selenium-hub selenium/hub | |
sudo docker run -d --link selenium-hub:hub selenium/node-chrome | |
sudo docker run -d --link selenium-hub:hub selenium/node-firefox |
Fig
若改用 Fig,則以 YAML 檔結構化記錄組態
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hub: | |
image: selenium/hub | |
ports: | |
- "4444:4444" | |
chrome: | |
image: selenium/node-chrome | |
links: | |
- hub:hub | |
firefox: | |
image: selenium/node-firefox | |
links: | |
- hub:hub |
指定啟動後於背景執行:
1 | fig up -d |
採用 Fig 的好處除了依設定檔啟用多個軟體容器,還支援簡易的 container 管理,如:
- 同時停止專案內多個軟體容器:
1
fig stop
- 同時刪除專案內多個軟體容器:
1
fig rm
- 增加或減少專案內個別容器數:
1
fig scale chrome=2 firefox=3
參考資料