Ubuntu 20.04 安装 Docker

环境

目前 Docker 支持的 Ubuntu 版本有:

  • Ubuntu Jammy 22.04 (LTS)
  • Ubuntu Impish 21.10
  • Ubuntu Focal 20.04 (LTS)
  • Ubuntu Bionic 18.04 (LTS)

当前的 Ubuntu 的版本为 20.04.3

root@node-11:~# cat /etc/issue
Ubuntu 20.04.3 LTS \n \l

当前的安装用户为:

root@node-11:~# whoami
root

若要使用其他用户安装,该用户需要有 sudo 权限。

Docker Engine 支持 x86_64 (or amd64),armhf,arm64,和 s390x 架构。

在线安装

更新软件列表

apt update

为防止当前环境有旧版本的Docker,首先执行卸载命令:

apt remove -y docker docker-engine docker.io containerd runc

如果卸载命令报告没有没有包要进行删除,表明当前环境未安装 Docker。

root@node-11:~# apt remove docker docker-engine docker.io containerd runc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'docker' is not installed, so not removed
Package 'containerd' is not installed, so not removed
Package 'runc' is not installed, so not removed
Package 'docker.io' is not installed, so not removed
Package 'docker-engine' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 187 not upgraded.

安装需要用到的工具(通常完整版的 Ubuntu 20.04 系统都会自带):

apt install -y ca-certificates curl gnupg lsb-release

添加 Docker 官方的 GPG 密钥

mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

添加官方的 Docker 仓库

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

查看仓库文件

root@node-11:~# cat /etc/apt/sources.list.d/docker.list 
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu   focal stable

更新apt包列表:

apt update

查看软件列表中的 Docker 版本:

apt-cache madison docker-ce
 docker-ce | 5:20.10.19~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.18~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.18~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.17~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.17~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.16~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.16~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.15~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.15~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.14~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.14~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.13~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.13~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.12~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 ... 省略其他版本 ...

可以看到目前官方最新的版本为20.10.19,另外由于我配置了阿里云的Ubuntu源,可以看到阿里云仓库中的最新版本为20.10.18
安装最新版本:

apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

安装指定版本:

apt install -y docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker-compose-plugin

例如安装 20.10.18版本:

apt install -y docker-ce=5:20.10.18~3-0~ubuntu-focal docker-ce-cli=5:20.10.18~3-0~ubuntu-focal containerd.io docker-compose-plugin

查看Docker版本信息:

 docker version
Client: Docker Engine - Community           # 客户端
 Version:           20.10.18                # 版本
 API version:       1.41                    # 对应的 Docker API 版本
 Go version:        go1.18.6                # Golang 版本
 Git commit:        b40c2f6
 Built:             Thu Sep  8 23:11:45 2022
 OS/Arch:           linux/amd64             # 操作系统/架构
 Context:           default
 Experimental:      true

Server: Docker Engine - Community               # 服务端
 Engine:
  Version:          20.10.18
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.6
  Git commit:       e42327a
  Built:            Thu Sep  8 23:09:37 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.8
  GitCommit:        9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

查看Docker信息:

docker info

可以看到当前服务器和Docker引擎的一些信息。

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.9.1-docker)
  compose: Docker Compose (Docker Inc., v2.11.2)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.18
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.4.0-128-generic
 Operating System: Ubuntu 20.04.3 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.907GiB
 Name: node-11
 ID: CXOW:ID47:TE7J:KEYA:KT6U:BLKH:RX3I:QLBX:VV7C:6FKF:JWTC:PANY
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No swap limit support

运行一个测试容器

 docker run hello-world

由于hello-world镜像本地没有,首先会到Dockers Hub拉取镜像,由于该容器仅用于测试,所以最后该命令会输出一段文字即运行完成。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:18a657d0cc1c7d0678a3fbea8b7eb4918bba25968d3e1b0adebfa71caddbc346
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/

运行一个Ubuntu容器:

 docker run -itd --name ubuntu-test ubuntu /bin/bash
  • -i: 采用交互式操作。
  • -t: 使用终端
  • -d: 容器后台运行
  • --name: 指定容器的名称为 ubuntu-test
  • ubuntu:容器使用的镜像
  • /bin/bash:交互式 Shell /bin/bash。
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
cf92e523b49e: Pull complete 
Digest: sha256:35fb073f9e56eb84041b0745cb714eff0f7b225ea9e024f703cab56aaa5c7720
Status: Downloaded newer image for ubuntu:latest
d8a6ab8add1abf62bd754bd09cd835aa5f22979649f71d2d6363f63e7813f29f

查看容器

# 运行中的容器
docker ps
# 所有容器
docker ps -a

查看镜像

docker images
docker image ls
root@node-11:~# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
d8a6ab8add1a   ubuntu    "/bin/bash"   9 minutes ago   Up 9 minutes             ubuntu-test
root@node-11:~# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES
d8a6ab8add1a   ubuntu        "/bin/bash"   9 minutes ago    Up 9 minutes                          ubuntu-test
b0135c2ae5ea   hello-world   "/hello"      16 minutes ago   Exited (0) 15 minutes ago             sleepy_germain
root@node-11:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        latest    216c552ea5ba   2 weeks ago     77.8MB
hello-world   latest    feb5d9fea6a5   13 months ago   13.3kB
root@node-11:~# docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        latest    216c552ea5ba   2 weeks ago     77.8MB
hello-world   latest    feb5d9fea6a5   13 months ago   13.3kB
posted @ 2022-10-15 20:59  JoeBlackZ  阅读(881)  评论(0)    收藏  举报