在 WSL2 Debian 10上安装原生 Docker
以下内容结合docker官网文档及网上错误解决方案,在wsl2 Debian 10 上测试通过。
删除旧版本的docker
旧版本的docker名称可能叫docker, docker.io, 或者docker-engine, 如果你之前安装过旧的版本,需要首先卸载:
sudo apt remove docker docker-engine docker.io containerd runc
[sudo] password for david:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'docker-engine' is not installed, so not removed
E: Unable to locate package containerd
我们现在安装的Docker是社区版的,被称为docker-ce. 有很多种方法可以在 Debian 上安装 Docker-ce, 这里我们直接用Docker仓库(Docker repository)进行安装。
首先需要更新源,并安装依赖包:
sudo apt update
sudo apt install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
添加 Docker 的官方 GPG 密钥:
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
指定docker仓库性质为稳定版:
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
安装docker:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
运行测试出错
sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.
排查过程:
sudo service docker start
grep: /etc/fstab: No such file or directory
[ ok ] Starting Docker: docker.
sudo service docker status [FAIL]
Docker is not running ... failed!
网上找到解决方案,方法如下:
sudo touch /etc/fstab
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
问题解决,运行hello world状况正常:
sudo service docker start
[ ok ] Starting Docker: docker.
sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
- The Docker client contacted the Docker daemon.
- The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64) - The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading. - 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/