Docker 是一个开源的应用程序容器,可让您快速构建、测试和部署应用程序。本文向大家介绍如何在 Ubuntu 20.04 服务器上安装 Docker。
Docker 可以从 Ubuntu 20.04 存储库安装,但不能保证是最新版本。本文中我们将从 Docker 官方存储库中安装最新的 Docker 版本。
在 Ubuntu 20.04 上安装 Docker
在 Ubuntu 上安装 Docker 非常简单,只要启用 Docker 存储库,导入存储库 GPG 密钥并安装软件包。
首先,更新存储库索引,并安装支持 HTTPS 协议所需的依赖项 :
sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
使用下面的 curl
命令导入存储库的 GPG 密钥:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
将 Docker 的 APT 存储库添加到系统中:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
现在 Docker 存储库已启用,可以安装库中任何可用的 Docker 版本。
要安装最新版本的 Docker,请运行以下命令。如果要安装特定版本的 Docker ,请跳过此步骤。
sudo apt updatesudo apt install docker-ce docker-ce-cli containerd.io
要安装特定版本的 Docker,可先列出存储库中可用的 Docker 版本:
sudo apt update
apt list -a docker-ce
可用的 Docker 版本打印在第二列中。
Output
docker-ce/focal 5:19.03.9~3-0~ubuntu-focal amd64
通过在包名称后添加 =<VERSION>
来安装特定版本:
sudo apt install docker-ce=<VERSION> docker-ce-cli=<VERSION> containerd.io
安装完成后,Docker 服务将自动启动。可以通过执行以下命令来验证它:
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 Thu 2020-05-21 14:47:34 UTC; 42s ago
...
当新版本的 Docker 发布时,可以使用下面的命令更新包。
sudo apt update && sudo apt upgrade
如果不想 Docker 包被更新,请将其标记为保留:
sudo apt-mark hold docker-ce
以非 root 用户身份执行 Docker 命令
默认情况下,只有 root 用户和具有 sudo 权限的用户才能执行 Docker 命令。要让非 root 用户执行 Docker 命令,需要将用户添加到 docker 组。执行下面的命令:
sudo usermod -aG docker $USER
$USER
为实际的用户名 。
注销并重新登录,以便刷新组成员身份。
验证安装
为了验证 Docker 是否已成功安装,可以在不添加 sudo 前缀的情况下执行 docker
命令。我们运行 hello-world 容器测试:
docker container run hello-world
通过以下输出可以确认,已经在 Ubuntu 系统上安装了 Docker 。
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d623fcb2250e8bec85b38
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:
The Docker client contacted the Docker daemon.
The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
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
在卸载 Docker 之前,最好删除所有容器、镜像、卷和网络 。
运行以下命令,停止所有正在运行的容器,并删除所有 docker 对象:
docker container stop $(docker container ls -aq)
docker system prune -a --volumes
现在就可以使用 apt
卸载 Docker :
sudo apt purge docker-cesudo apt autoremove
总结
通过本文,牛奇网向您展示了如何在 Ubuntu 20.04 系统安装 Docker。希望能对大家有所帮助。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/how-to-install-docker-on-ubuntu-20-04/