Ubuntu、elementary OS安装及卸载Docker

Docker Engine-Community 支持 x86_64 (或 amd64)、armhf、arm64、s390x (IBM Z) 和 ppc64le (IBM Power) 架构。

可以用一下命令查看系统信息:

$ more /etc/os-release

卸载旧版本

Docker 的旧版本被称为 docker,docker.io 或 docker-engine 。可以用命令

$ which docker

查看是否安装了Docker,如果已安装,使用以下命令卸载:

$ sudo apt-get remove docker docker-engine docker.io containerd runc

目录/ var/lib/docker/中的内容(包括映像,容器,卷和网络)将会保留下来。 Docker Engine-Community软件包现在称为docker-ce。

安装Docker Engine 社区版

安装docker-ce主要有两种方式:使用Docker进行安装和下载deb包手动安装,当然也可以通过Docker官网提供的脚本进行安装,这里不做赘述,感兴趣的可以去官网查看:Install using the convenience script 、 github仓库

本文介绍一下使用仓库进行安装的方法,这也是docker官方推荐的方式:

设置仓库

  1. 更新 apt 包索引:
$ sudo apt-get update
  1. 安装能够让apt使用HTTPS的包:
$ sudo apt-get install\apt-transport-https\ca-certificates\curl\gnupg-agent\software-properties-common
  1. 添加 Docker 的官方 GPG 密钥:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

添加后通过搜索后8位字符可以查看现在是否拥有了指纹 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 的密匙:

$ sudo apt-key fingerprint 0EBFCD88    
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) 
sub   rsa4096 2017-02-22 [S]

如果返回与上面类似的内容则说明已经添加成功

  1. 设置稳定版仓库(源)
    要安装nightly版或test版(或者两种一起)只需在下面的命令后面加上nightly或test即可:
$ # x86_64 / amd64架构,自己的电脑一般是这个
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"


$ # armhf架构
$ sudo add-apt-repository \
   "deb [arch=armhf] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"


$ #  arm64架构
$ sudo add-apt-repository \
   "deb [arch=arm64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"


$ #  IBM Power (ppc64le)
$ sudo add-apt-repository \
   "deb [arch=ppc64el] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"


$ #  IBM Z (s390x)
$ sudo add-apt-repository \
   "deb [arch=s390x] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

上面命令中的 $(lsb_release -cs) 返回Ubuntu发行版的名称,对于Ubuntu衍生版,如Linux Mint、elementary OS等需要改为其父版Ubuntu发行版名称,使用以下命令查看系统版本信息:

$ more /etc/os-release 
NAME="elementary OS"
VERSION="5.1.2 Hera"
ID=elementary
ID_LIKE=ubuntu
PRETTY_NAME="elementary OS 5.1.2 Hera"
LOGO=distributor-logo
VERSION_ID="5.1.2"
HOME_URL="https://elementary.io/"
SUPPORT_URL="https://elementary.io/support"
BUG_REPORT_URL="https://github.com/elementary/os/issues/new"
PRIVACY_POLICY_URL="https://elementary.io/privacy-policy"
VERSION_CODENAME=hera
UBUNTU_CODENAME=bionic

倒数第二行 VERSION_CODENAME=hera 是当前系统版本,我的是elementary OS的hera版本,最后一行是父版Ubuntu的版本:bionic,因此我们要把$(lsb_release -cs)替换为bionic:

$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   bionic \
   stable"

其实对于其他Linux版本比如Debian及其衍生版deepin,只需要修改上面的版本名ubuntu和版本号bionic为相应的内容就可以,以deepin为例,在终端执行以下命令查看debian版本:

$ cat /etc/debian_version
9.0

可以看到该deepin版本(Deepin15.11)是基于Debian9.0的,Debian每个版本号都有一个代号:

版本 代号
10 buster
9 stretch
8 jessie
7 wheezy
6.0 squeeze

其他版本可以去Debian官网查看。不过目前docker仅支持Debian9和Debian10(更早的版本不是已经被淘汰就是即将被淘汰)。所以要在deepin上安装docker,我就要把命令改为:

$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   stretch \
   stable"

安装Docker engine - Community

  1. 更新 apt 包索引:

$ sudo apt-get update

  1. 安装最新版本的docker ce和containerd
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. 安装指定版本的docker ce
    第二步默认安装的是最新版本的docker ce,如果要安装其他版本,可以通过以下命令。
    a. 列出仓库中的可选版本:
$ apt-cache madison docker-ce
 docker-ce | 5:19.03.8~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.7~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.6~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.5~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.4~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.3~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
...

b. 使用第二列中的版本字符串安装特定版本,例如5:19.03.8~3-0~ubuntu-bionic:

$ sudo apt-get install docker-ce=5:19.03.8~3-0~ubuntu-bionic docker-ce-cli=5:19.03.8~3-0~ubuntu-bionic containerd.io
  1. 确认docker是否安装成功
$ sudo docker run hello-world

这个命令下载测试镜像并在容器中运行它。容器运行时,它会打印如下信息并退出:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
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:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. 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/

卸载Docker

  1. 卸载 Docker Engine - Community软件包
$ sudo apt-get purge docker-ce
  1. 删除镜像,容器和卷(可选)
    上一步的卸载命令并不会删除我们使用过程中下载及生成的镜像、容器等,要删除的话可以使用以下命令:
$ sudo rm -rf /var/lib/docker

你可能感兴趣的:(Ubuntu、elementary OS安装及卸载Docker)