0 前言
Docker 官網的安裝卸載教程其實是很詳細的,但是在 WSL2 Ubuntu-20.04 環境中安裝卸載 Docker 時,還是會有小錯誤蹦出來干擾一下,所以在此記錄出來,希望能幫到大家
1 安裝
首先貼出來官方教程——Ubuntu 環境安裝 Docker 官方教程
我是通過倉庫安裝,但因為 Docker 官方倉庫是外網,賊慢,所以我用了阿里雲鏡像倉庫,這個在下文會提到。
1、 卸載舊版本
haha@DESKTOP:~$ sudo apt-get remove docker docker-engine docker.io containerd runc
2、 更新 apt package索引,並安裝一些支持 apt 通過 HTTPS 使用倉庫的包
haha@DESKTOP:~$ sudo apt-get update
haha@DESKTOP:~$ sudo apt-get install \
> apt-transport-https \
> ca-certificates \
> curl \
> gnupg \
> lsb-release
3、 添加 Docker 的官方 GPG 密鑰
haha@DESKTOP:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
4、 添加倉庫
官方命令如下
haha@DESKTOP:~$ echo \
> "deb [arch=amd64 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
我們替換其中的官方倉庫地址為阿里雲鏡像地址
haha@DESKTOP:~$ echo \
> "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] http://mirrors.aliyun.com/docker-ce/linux/ubuntu \
> $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
5、 安裝 Docker 引擎
haha@DESKTOP:~$ sudo apt-get update
haha@DESKTOP:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
docker-ce-rootless-extras docker-scan-plugin pigz slirp4netns
Suggested packages:
aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras docker-scan-plugin pigz slirp4netns
0 upgraded, 7 newly installed, 0 to remove and 246 not upgraded.
Need to get 104 MB of archives.
After this operation, 448 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 containerd.io amd64 1.4.9-1 [24.7 MB]
Get:2 http://archive.ubuntu.com/ubuntu focal/universe amd64 pigz amd64 2.4-1 [57.4 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/universe amd64 slirp4netns amd64 0.4.3-1 [74.3 kB]
Get:4 http://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 docker-ce-cli amd64 5:20.10.7~3-0~ubuntu-focal [41.4 MB]
Get:5 http://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 docker-ce amd64 5:20.10.7~3-0~ubuntu-focal [24.8 MB]
Get:6 http://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 docker-ce-rootless-extras amd64 5:20.10.7~3-0~ubuntu-focal [9063 kB]
Get:7 http://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 docker-scan-plugin amd64 0.8.0~ubuntu-focal [3889 kB]
Fetched 104 MB in 12s (8627 kB/s)
Selecting previously unselected package pigz.
(Reading database ... 39964 files and directories currently installed.)
Preparing to unpack .../0-pigz_2.4-1_amd64.deb ...
Unpacking pigz (2.4-1) ...
Selecting previously unselected package containerd.io.
Preparing to unpack .../1-containerd.io_1.4.9-1_amd64.deb ...
Unpacking containerd.io (1.4.9-1) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../2-docker-ce-cli_5%3a20.10.7~3-0~ubuntu-focal_amd64.deb ...
Unpacking docker-ce-cli (5:20.10.7~3-0~ubuntu-focal) ...
^[[CSelecting previously unselected package docker-ce.
Preparing to unpack .../3-docker-ce_5%3a20.10.7~3-0~ubuntu-focal_amd64.deb ...
Unpacking docker-ce (5:20.10.7~3-0~ubuntu-focal) ...
Selecting previously unselected package docker-ce-rootless-extras.
Preparing to unpack .../4-docker-ce-rootless-extras_5%3a20.10.7~3-0~ubuntu-focal_amd64.deb ...
Unpacking docker-ce-rootless-extras (5:20.10.7~3-0~ubuntu-focal) ...
Selecting previously unselected package docker-scan-plugin.
Preparing to unpack .../5-docker-scan-plugin_0.8.0~ubuntu-focal_amd64.deb ...
Unpacking docker-scan-plugin (0.8.0~ubuntu-focal) ...
Selecting previously unselected package slirp4netns.
Preparing to unpack .../6-slirp4netns_0.4.3-1_amd64.deb ...
Unpacking slirp4netns (0.4.3-1) ...
Setting up slirp4netns (0.4.3-1) ...
Setting up docker-scan-plugin (0.8.0~ubuntu-focal) ...
Setting up containerd.io (1.4.9-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Setting up docker-ce-cli (5:20.10.7~3-0~ubuntu-focal) ...
Setting up pigz (2.4-1) ...
Setting up docker-ce-rootless-extras (5:20.10.7~3-0~ubuntu-focal) ...
Setting up docker-ce (5:20.10.7~3-0~ubuntu-focal) ...
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
invoke-rc.d: could not determine current runlevel
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3) ...
6、 查看 Docker 版本
haha@DESKTOP:~$ sudo docker version
Client: Docker Engine - Community
Version: 20.10.7
API version: 1.41
Go version: go1.13.15
Git commit: f0df350
Built: Wed Jun 2 11:56:38 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
7、 啟動 Docker 服務
我們在很多教程都可以看到,對於unbuntu系統啟動服務時都是使用systemctl命令,但是在 WSL2 Ubuntu-20.04來講,並沒有使用 systemd 管理Linux上的服務,使用的是經典的SysV init(sysvinit)系統。具體可以看這篇文章——修復 "System has not been booted with systemd as init system "的錯誤
haha@DESKTOP:~$ sudo systemctl start docker
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
我們通過如下命令來啟動服務
haha@DESKTOP:~$ sudo service docker start
* Starting Docker: docker [ OK ]
8、 運行 hello-world 鏡像
haha@DESKTOP:~$ sudo docker run hello-world
9、 查看鏡像
haha@DESKTOP:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 4 months ago 13.3kB
2 卸載 Docker
1、 卸載
sudo apt-get purge docker-ce docker-ce-cli containerd.io
Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
2、 刪除目錄
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd