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, 最后就可以自由下载你需要的工具了.

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