快轉到主要內容

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

Linux系統 虛擬機與容器技術 Gentoo Docker
🗓️ 民國111年 壬寅年
✍ 切換正體/簡體字
目錄

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:/#

相關文章

如何在ARM架構的裝置執行x86架構的Docker Image
Linux系統 虛擬機與容器技術 Docker QEMU User Mode
Ubuntu Linux如何安裝Windows 10虛擬機 (QEMU/KVM)
Linux系統 虛擬機與容器技術 Ubuntu QEMU Windows
Ubuntu安裝QEMU/KVM和Virt Manager虛擬機管理員
Linux系統 虛擬機與容器技術 Libvirt Ubuntu QEMU

留言板

此處提供二種留言板。點選按鈕,選擇您覺得方便的留言板。要討論程式碼請用Giscus,匿名討論請用Disqus。

這是Giscus留言板,需要Github帳號才能留言。支援markdown語法,若要上傳圖片請貼Imgur連結。您的留言會在Github Discussions向所有人公開。

這是Disqus留言板,您可能會看到Disqus強制投放的廣告。有時留言可能會被系統判定需審核,導致延遲顯示,請見諒。