wsl使用docker運行ubuntu -- 解決apt-get update的問題

前面已經介紹過wsl如何安裝docker了, 今天再來介紹一下在wsl中用docker如何運行ubuntu鏡像以及如何解決其apt-get update失敗的問題.

docker運行ubuntu, 這個其實很簡單, 沒有什麼特別需要注意的地方, 就是正常的docker使用方法, 網上教程很多.

docker要起一個容器, 當然要先有一個鏡像, 鏡像可以從遠程倉庫pull, 也可以是別人給你的一個文件, 不同的來源有不同的啓動方法, 這個後面再說, 因爲這裏主要想說的問題會影響到本地倉庫.

說一下問題: wsl中運行ubuntu容器之後, 裏面有很多命令, 比如vim, ifconfig, ping等都不支持, 所以首先要做的就是apt update, 這樣我們後面才能安裝這些命令, 但就是apt update, 在這裏出了問題, 一直提示update失敗, 按照網上的方法修改了sources.list, 也apt clean了, 但依然失敗, 折騰了將近一天, 最後終於在這裏找到了解決辦法.

運行失敗的打印信息如下, 問題的原因是wsl的文件系統和普通的linux有所不同, docker使用storage driver(存儲驅動程序)來管理image和container的數據, 但是docker的這些storage driver需要後端文件系統的支持, 在這裏, 就是說wsl的文件系統(我沒有查到它的文件系統類型, 通常使用docker info可以查到的, 但是wsl卻沒有)和storage driver不匹配, 導致了問題的發生, 可以通過修改storage driver(會影響文件利用率和啓動速度)解決這個問題, 但是修改storage driver會導致docker之前的鏡像和容器不可訪問, 所以咱們要先解決一下這個問題再pull鏡像和起容器.

root@cdddfd79c76b:/# apt update
Get:1 http://mirrors.aliyun.com/ubuntu bionic InRelease [242 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease [74.6 kB]
Err:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
  At least one invalid signature was encountered.
Err:2 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease
  At least one invalid signature was encountered.
Err:3 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease
  At least one invalid signature was encountered.
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Err:4 http://security.ubuntu.com/ubuntu bionic-security InRelease
  At least one invalid signature was encountered.
Reading package lists... Done
W: GPG error: http://mirrors.aliyun.com/ubuntu bionic InRelease: At least one invalid signature was encountered.
E: The repository 'http://mirrors.aliyun.com/ubuntu bionic InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: GPG error: http://mirrors.aliyun.com/ubuntu bionic-updates InRelease: At least one invalid signature was encountered.
E: The repository 'http://mirrors.aliyun.com/ubuntu bionic-updates InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: GPG error: http://mirrors.aliyun.com/ubuntu bionic-backports InRelease: At least one invalid signature was encountered.
E: The repository 'http://mirrors.aliyun.com/ubuntu bionic-backports InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: GPG error: http://security.ubuntu.com/ubuntu bionic-security InRelease: At least one invalid signature was encountered.
E: The repository 'http://security.ubuntu.com/ubuntu bionic-security InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

具體的解決辦法是: 在/etc/docker/daemon.json中添加下面一行(vfs是支持全部文件系統的, 因爲我不知道wsl是哪種文件系統, 所以用了vfs).

"storage-driver": "vfs"

比如我現在的daemon.json內容如下.

lain@DESKTOP-V537K6U:/mnt/e/vmware$ cat /etc/docker/daemon.json
{
        "registry-mirrors": ["https://6kx4zyno.mirror.aliyuncs.com"],
"storage-driver": "vfs"
}

修改完之後重啓一下docker服務sudo service docker restart, 你再通過鏡像起容器就可以了.問題到此結束了, 後面是簡單說明怎麼從鏡像起容器, 不關心的可以到此結束了.
前面說鏡像有兩種獲取方式, 起容器的方法有所不同.
先說第一種, 從遠程倉庫pull. 因爲大家都懂的原因, 在國內使用docker需要切換一下國內源, 這裏要pull一個鏡像, 最好先配置一下國內的加速器, 具體教程請百度; 配置完之後就可以使用docker pull ubuntu來拉取官方的標準ubuntu鏡像了, 這樣是默認pull最新版, 當然你也可以pull不同的版本, 比如docker pull ubuntu:16.04, pull之後可以用docker images來查看本地的倉庫以確認pull是否成功, 然後docker run -it ubuntu就可以創建容器並運行了, 命令參數請自行學習.

再說一下第二種, 從tar文件啓動. 我們拿到一個tar文件, 首先就是將其導入到本地倉庫中, 可以通過import或者load命令導入/加載, 具體看tar是通過export還是save生成的, 比如我這裏 docker import mubuntu.tar ubuntu:test, 同樣的我們可以通過docker images查看本地倉庫以確認導入是否成功, 最後docker run -it ubuntu運行.

運行Ubuntu容器之後, 可以先切換一下國內源, 然後apt update, 最後就可以自由下載你需要的工具了.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章