快轉到主要內容

如何在Gentoo Linux上安裝與執行Docker容器

· 民國111年壬寅年
·
切換繁體/簡體
分類 資訊科技 虛擬機與容器技術
標籤 Gentoo Docker
目錄

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
#

  1. Gentoo的Docker套件有app-containers/dockerapp-containers/docker-cli,FLAG維持預設,直接編譯安裝:
sudo emerge --ask --verbose app-containers/docker app-containers/docker-cli
  1. 將自己加入Docker群組。然而因執行Docker就等同root,所以還是讓一般用戶維持需要sudo的模式。
sudo usermod -aG docker $USER
  1. 將Docker加入開機服務
sudo rc-update add docker default
sudo rc-service docker start
  1. 為防止Docker出現dial tcp: lookup docker no such host錯誤,編輯/etc/resolv.conf,將DNS伺服器改成只剩Google的:
nameserver 8.8.8.8

3. 測試Docker執行狀況
#

  1. 試試看執行Hello World
sudo docker run --rm hello-world
  1. 應會得到以下輸出。
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/
  1. 接著再測試從Dockerfile建立容器,新增一個叫做"Dockerfile"的檔案,填入以下內容
FROM ubuntu
  1. 建立Ubuntu容器
sudo docker build -t my-ubuntu .
  1. 應會進到Ubuntu終端機,輸入exit退出。
ivon@Gentoo ~/ $ sudo docker run -it --rm --name running-ubuntu my-ubuntu
root@35525e4f1459:/#

相關文章


感謝您的閱讀。我寫作是為了誠實表達想法,而不是追逐社群互動與流量。我很樂意傾聽你在仔細閱讀我文章之後的心得。若有內容勘誤 or 技術問題 or 回饋想法,歡迎透過本站「關於」頁面的Email與我聯絡。