Docker是很熱門的容器技術,比虛擬機輕量,適合用於各種開發用途。大部分Linux發行版都支援Docker,而Gentoo只要Linux核心有開啟正確選項,同樣能執行Docker。
只不過Gentoo Wiki有給出提醒:若容器會導致核心錯誤(kernel panic),會連帶讓宿主機也發生核心錯誤。
1. 調整核心選項#
官方預先編譯的sys-kernel/gentoo-kernel-bin
預設已經支援Docker。
若是手動調整核心,參考 Gentoo Wiki開啟Docker相關的功能。
接著用以下指令確認核心與Docker的相容性:
/usr/share/docker/contrib/check-config.sh
2. 安裝Docker#
- Gentoo的Docker套件有
app-containers/docker
和app-containers/docker-cli
,FLAG維持預設,直接編譯安裝:
sudo emerge --ask --verbose app-containers/docker app-containers/docker-cli
- 將自己加入Docker群組。然而因執行Docker就等同root,所以還是讓一般用戶維持需要sudo的模式。
sudo usermod -aG docker $USER
- 將Docker加入開機服務
sudo rc-update add docker default
sudo rc-service docker start
- 為防止Docker出現
dial tcp: lookup docker no such host
錯誤,編輯/etc/resolv.conf
,將DNS伺服器改成只剩Google的:
nameserver 8.8.8.8
3. 測試Docker執行狀況#
- 試試看執行Hello World
sudo docker run --rm hello-world
- 應會得到以下輸出。
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
- 接著再測試從Dockerfile建立容器,新增一個叫做"Dockerfile"的檔案,填入以下內容
FROM ubuntu
- 建立Ubuntu容器
sudo docker build -t my-ubuntu .
- 應會進到Ubuntu終端機,輸入
exit
退出。
ivon@Gentoo ~/ $ sudo docker run -it --rm --name running-ubuntu my-ubuntu
root@35525e4f1459:/#