跳转至

Ubuntu安装Docker

概要: 本文的安装方法适用于Ubuntu 18.04及更高版本。更低版本的Ubuntu安装Docker请参考官网指引。

创建时间: 2023.08.03 00:38:58

更新时间: 2023.08.03 00:41:48

移除已有Docker环境

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

设置软件源

安装必要软件包

Bash
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release

导入Docker官方GPG Key

Bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

设定更新通道

提示

以下默认使用stable通道,以x86架构为例。

Bash
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

安装Docker

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

验证Docker

Bash
sudo docker run hello-world
输出如下
Bash
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.

升级Docker

Bash
sudo apt-get update

移除Docker环境

卸载软件包

Bash
sudo apt-get purge docker-ce docker-ce-cli containerd.io

删除镜像与容器

Bash
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

参考资料