Ubuntu 安装docker

参考博客:Ubuntu 20.04 安装 docker 详解
参考博客:Ubuntu Docker 安装

删除现有的docker

干净的卸载掉你系统中的 docker,参考官网操作

jun@jun-VirtualBox:/var/lib$ sudo apt-get purge docker-ce docker-ce-cli containerd.io
jun@jun-VirtualBox:/var/lib$ rm -rf /var/lib/docker
jun@jun-VirtualBox:/var/lib$ sudo apt-get remove docker docker-engine docker.io containerd runc
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package docker-engine
jun@jun-VirtualBox:/var/lib$

更新软件源

jun@jun-VirtualBox:/var/lib$ sudo apt-get update

安装依赖

jun@jun-VirtualBox:/var/lib$ sudo apt install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

添加docker官方GPG密钥

jun@jun-VirtualBox:/var/lib$ curl -fgSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3817  100  3817    0     0   6889      0 --:--:-- --:--:-- --:--:--  6889
OK

验证密钥

jun@jun-VirtualBox:/var/lib$ 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) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

jun@jun-VirtualBox:/var/lib$

使用以下指令设置稳定版仓库

sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
  stable"

安装docker

  • 安装最新版本的docker engine-community 和 containerd
jun@jun-VirtualBox:/var/lib$ sudo apt-get install docker-ce docker-ce-cli containerd.io
  • 要安装特定版本的 Docker Engine-Community,请在仓库中列出可用版本,然后选择一种安装。列出您的仓库中可用的版本
jun@jun-VirtualBox:/var/lib$ apt-cache madison docker-ce
 docker-ce | 5:20.10.12~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.11~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.10~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.9~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.8~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.7~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.6~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.5~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.4~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.3~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.2~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.1~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.0~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.15~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.14~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.13~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.12~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.11~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.10~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.9~3-0~ubuntu-focal | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal/stable amd64 Packages
jun@jun-VirtualBox:/var/lib$

使用第二列中的版本字符串安装特定版本,例如 5:19.03.93-0ubuntu-focal,

sudo apt-get install docker-ce=<5:19.03.9~3-0~ubuntu-focal > docker-ce-cli=<5:19.03.9~3-0~ubuntu-focal > containerd.io

或者安装最新版本

sudo apt-get install docker-ce docker-ce-cli  containerd.io

测试docker运行

jun@jun-VirtualBox:/var/lib$ sudo docker run hello-world
jun@jun-VirtualBox:/var/lib$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:975f4b14f326b05db86e16de00144f9c12257553bba9484fed41f9b6f2257800
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容器

jun@jun-VirtualBox:/var/lib$ sudo docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                          PORTS     NAMES
9fcff7d0b92c   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             epic_tesla
a82814aee7a7   hello-world   "/hello"   8 minutes ago        Exited (0) 8 minutes ago                  exciting_chaum
jun@jun-VirtualBox:/var/lib$

查看docker服务运行状态

jun@jun-VirtualBox:/var/lib$ sudo systemctl status docker
jun@jun-VirtualBox:/var/lib$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-01-16 15:42:42 CST; 5s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 23397 (dockerd)
      Tasks: 8
     Memory: 29.0M
     CGroup: /system.slice/docker.service
             └─23397 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

1月 16 15:42:42 jun-VirtualBox dockerd[23397]: time="2022-01-16T15:42:42.434888523+08:00" level=warning msg="Your kernel does not supp>
1月 16 15:42:42 jun-VirtualBox dockerd[23397]: time="2022-01-16T15:42:42.435071473+08:00" level=warning msg="Your kernel does not supp>
1月 16 15:42:42 jun-VirtualBox dockerd[23397]: time="2022-01-16T15:42:42.435155074+08:00" level=warning msg="Your kernel does not supp>
1月 16 15:42:42 jun-VirtualBox dockerd[23397]: time="2022-01-16T15:42:42.435349663+08:00" level=info msg="Loading containers: start."
1月 16 15:42:42 jun-VirtualBox dockerd[23397]: time="2022-01-16T15:42:42.508460852+08:00" level=info msg="Default bridge (docker0) is >
1月 16 15:42:42 jun-VirtualBox dockerd[23397]: time="2022-01-16T15:42:42.536441028+08:00" level=info msg="Loading containers: done."
1月 16 15:42:42 jun-VirtualBox dockerd[23397]: time="2022-01-16T15:42:42.581529559+08:00" level=info msg="Docker daemon" commit=459d0d>
1月 16 15:42:42 jun-VirtualBox dockerd[23397]: time="2022-01-16T15:42:42.581581379+08:00" level=info msg="Daemon has completed initial>
1月 16 15:42:42 jun-VirtualBox systemd[1]: Started Docker Application Container Engine.
1月 16 15:42:42 jun-VirtualBox dockerd[23397]: time="2022-01-16T15:42:42.614719728+08:00" level=info msg="API listen on /run/docker.so>
jun@jun-VirtualBox:/var/lib$

重启docker服务

jun@jun-VirtualBox:/var/lib$ sudo systemctl restart docker
jun@jun-VirtualBox:/var/lib$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-01-16 15:44:02 CST; 1s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 23527 (dockerd)
      Tasks: 8
     Memory: 27.1M
     CGroup: /system.slice/docker.service
             └─23527 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

服务相关命令

$ sudo systemctl enable docker #开机自启动docker服务
$ sudo systemctl status docker #查看服务docker运行状态
$ sudo systemctl start docker #启动docker服务
$ sudo systemctl stop docker #停止docker服务
$ sudo systemctl restart docker #重启docker服务

docker使用参考docker 使用随记

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容