前言
最近在安裝Ubuntu 22.04的虛擬機器上,進行Docker的安裝,但是發現在安裝新版Docker 28.0.0的版本時候,docker之背景服務會運行失敗。
本文章就是要進行解析並找到目前可以解決的方法。
安裝Docker
首先,要重現這個問題,則先需要安裝Docker,從Ubuntu 22.04之後,匯入套件所需要的GPG金鑰方式有變,需要利用「gpg」來完成,相關執行安裝的指令與步驟如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $ sudo apt-get update $ sudo apt-get install ca-certificates curl gnupg lsb-release $ curl -fsSL https: //download .docker.com /linux/ubuntu/gpg | sudo gpg \ --dearmor -o /usr/share/keyrings/docker-archive-keyring .gpg $ echo \ "deb [arch=$(dpkg --print-architecture) signed-by= /usr/share/keyrings/docker-archive-keyring .gpg] https: //download .docker.com /linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources .list.d /docker .list > /dev/null $ sudo apt-get update $ sudo apt-get install docker-ce docker-ce-cli containerd.io |
從上述所依序執行的指令可以得知,首先先安裝匯入套件與設定來源所需要的套件,接著再透過gpg指令進行匯入Docker套件來源的GPG金鑰,之後再設定套件來源位址到source.list.d的目錄下。
最後更新各個套件來源之後,安裝Docker相關的套件。
在安裝完成Docker之套件之後,可以透過「systemctl」指令來進行檢查目前Docker所運行之狀態:
1 | $ sudo systemctl status docker.service --no-pager |
透過上面的指令,則會看到Docker背景服務運行失敗:
1 2 3 4 5 6 7 8 9 10 11 12 13 | × docker.service - Docker Application Container Engine Loaded: loaded ( /lib/systemd/system/docker .service; enabled; vendor preset: enabled) Active: failed (Result: exit -code) since Sat 2025-02-22 22:36:34 CST; 13s ago TriggeredBy: × docker.socket Docs: https: //docs .docker.com Process: 435922 ExecStart= /usr/bin/dockerd -H fd: // --containerd= /run/containerd/containerd .sock (code=exited, status=1 /FAILURE ) Main PID: 435922 (code=exited, status=1 /FAILURE ) Feb 22 22:36:34 ubuntu-2204 systemd[1]: docker.service: Scheduled restart job, restart counter is at 3. Feb 22 22:36:34 ubuntu-2204 systemd[1]: Stopped Docker Application Container Engine. Feb 22 22:36:34 ubuntu-2204 systemd[1]: docker.service: Start request repeated too quickly. Feb 22 22:36:34 ubuntu-2204 systemd[1]: docker.service: Failed with result 'exit-code' . Feb 22 22:36:34 ubuntu-2204 systemd[1]: Failed to start Docker Application Container Engine. |
接著,我們需要檢查目前Docker在運行背景服務遇到什麼問題,故我們可以先將該Docker服務停止並使用「dockerd」指令進行運行服務時候之除錯,相關執行的指令如下:
1 2 3 | $ sudo systemctl stop docker.service $ sudo dockerd --debug |
執行完上述的指令之後,會看到下列輸出的錯誤訊息:
1 2 3 4 | INFO[2025-02-22T22:39:18.396361368+08:00] stopping event stream following graceful shutdown error= "<nil>" module=libcontainerd namespace=moby DEBU[2025-02-22T22:39:18.396600096+08:00] Cleaning up old mountid : start. DEBU[2025-02-22T22:39:18.397661676+08:00] Cleaning up old mountid : done . failed to start daemon: Error initializing network controller: error creating default "bridge" network: add inter-network communication rule: (iptables failed: ip6tables --wait -t filter -A DOCKER-ISOLATION-STAGE-1 -i docker0 ! -o docker0 -j DOCKER-ISOLATION-STAGE-2: ip6tables v1.8.7 (nf_tables): Could not fetch rule set generation id : Invalid argument |
從上面的錯誤訊息來看,可以得知Docker啟動時使用「moby」加速軟體容器化在執行時出現問題,與IPv6有關,故為了要讓Docker能夠順利在背景中運行,則需要設定關閉IPv6設定。
Docker之設定檔與路徑可以在此「/etc/docker/daemon.json」路徑中建立該JSON設定檔,相關的設定內容如下:
1 2 3 4 | { "ipv6" : false , "ip6tables" : false } |
設定完成之後,再使用「systemctl」指令將Docker服務進行啟動,啟動之後再查看Docker服務在背景的狀態,相關執行的指令如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $ sudo systemctl start docker.service $ sudo systemctl status docker.service --no-pager ● docker.service - Docker Application Container Engine Loaded: loaded ( /lib/systemd/system/docker .service; enabled; vendor preset: enabled) Active: active (running) since Sat 2025-02-22 22:46:16 CST; 18s ago TriggeredBy: ● docker.socket Docs: https: //docs .docker.com Main PID: 439931 (dockerd) Tasks: 11 Memory: 21.2M CGroup: /system .slice /docker .service └─439931 /usr/bin/dockerd -H fd: // --containerd= /run/containerd/containerd .sock Feb 22 22:46:16 ubuntu-2204 dockerd[439931]: time = "2025-02-22T22:46:16.513024032+08:00" level=warning…erlay2 Feb 22 22:46:16 ubuntu-2204 dockerd[439931]: time = "2025-02-22T22:46:16.513186044+08:00" level=warning…pport" Feb 22 22:46:16 ubuntu-2204 dockerd[439931]: time = "2025-02-22T22:46:16.513213740+08:00" level=warning…pport" Feb 22 22:46:16 ubuntu-2204 dockerd[439931]: time = "2025-02-22T22:46:16.513239972+08:00" level=warning…pport" Feb 22 22:46:16 ubuntu-2204 dockerd[439931]: time = "2025-02-22T22:46:16.513291824+08:00" level=info ms…28.0.0 Feb 22 22:46:16 ubuntu-2204 dockerd[439931]: time = "2025-02-22T22:46:16.521985344+08:00" level=info ms…ldkit" Feb 22 22:46:16 ubuntu-2204 dockerd[439931]: time = "2025-02-22T22:46:16.587099660+08:00" level=info ms…ation" Feb 22 22:46:16 ubuntu-2204 dockerd[439931]: time = "2025-02-22T22:46:16.608493944+08:00" level=info ms…ation" Feb 22 22:46:16 ubuntu-2204 dockerd[439931]: time = "2025-02-22T22:46:16.608759876+08:00" level=info ms….sock" Feb 22 22:46:16 ubuntu-2204 systemd[1]: Started Docker Application Container Engine. Hint: Some lines were ellipsized, use -l to show in full. |
從上述執行的指令與輸出的訊息可以得知,Docker服務有重新的在背景順利的運行了。
結論
目前這個在Moby專案中有建立一個issue且是列為bug,故這個問題應該要等到下個Docker版本才會解決了,目前解決的方法就只能停用IPv6之設定。
參考資料
- Moby專案Bug議題描述,https://github.com/moby/moby/issues/49504
- Docker在Ubuntu 22.04安裝的方法,https://ubuntu.com/tutorials/how-to-run-docker-inside-lxd-containers#3-install-docker
- 使用「dockerd」指令進行除錯的方法,https://stackoverflow.com/questions/55906503/docker-how-to-fix-job-for-docker-service-failed-because-the-control-process-ex