Ubuntu系统20.04版本下安装docker(2024-12-27)
离线安装
Select your Ubuntu version in the list.
Go to
pool/stable/and select the applicable architecture (amd64,armhf,arm64, ors390x).Download the following
debfiles for the Docker Engine, CLI, containerd, and Docker Compose packages:containerd.io_<version>_<arch>.debdocker-ce_<version>_<arch>.debdocker-ce-cli_<version>_<arch>.debdocker-buildx-plugin_<version>_<arch>.debdocker-compose-plugin_<version>_<arch>.deb
Install the
.debpackages. Update the paths in the following example to where you downloaded the Docker packages.console$ sudo dpkg -i ./containerd.io_<version>_<arch>.deb \ ./docker-ce_<version>_<arch>.deb \ ./docker-ce-cli_<version>_<arch>.deb \ ./docker-buildx-plugin_<version>_<arch>.deb \ ./docker-compose-plugin_<version>_<arch>.debThe Docker daemon starts automatically.
Verify that the installation is successful by running the
hello-worldimage:console$ sudo service docker start $ sudo docker run hello-worldThis command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.
apt仓库安装
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
# 安装docker包
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
#验证安装是否成功,运行hello-world镜像
sudo docker run hello-world在线安装
在Ubuntu虚拟机中安装Docker,您可以遵循以下步骤。请确保您的虚拟机有互联网连接,以便下载必要的软件包。
1. 更新APT包索引
打开终端并运行以下命令来更新现有的包索引:
sudo apt-get update2. 安装所需的软件包
安装一些必要的软件包,以允许apt通过HTTPS使用存储库:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common3. 添加Docker的官方GPG密钥
这一步是验证下载的软件包的完整性所必需的:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpgcurl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg4. 设置稳定的存储库
将Docker存储库添加到APT源列表:
echo "deb [arch=$(dpkg --print-architecture) 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/nullecho "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null5. 再次更新APT包索引
添加了新的存储库之后,再次更新APT包索引:
sudo apt-get update6. 安装最新版本的Docker引擎
现在可以安装Docker了:
sudo apt-get install docker-ce docker-ce-cli containerd.io7. 验证安装
确认Docker已正确安装并且能够正常运行:
sudo docker run hello-world这条命令会下载一个测试镜像并在容器中运行它,如果一切顺利,您应该会看到一条欢迎信息,说明Docker已经成功安装。
8. (可选) 将当前用户添加到docker组
为了避免每次运行docker命令时都需要使用sudo,您可以将自己添加到docker用户组中(需要注销再重新登录或重启系统才能生效):
sudo usermod -aG docker ${USER}完成上述步骤后,您已经在Ubuntu虚拟机上成功安装了Docker。如果您有任何问题或遇到错误,请随时提问!
9.注意事项
上边的步骤中的官方地址可以换成软件源镜像地址
用镜像安装对应的软件
# 清华源镜像地址
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/
# 官网地址
https://download.docker.com/linux/ubuntu
# 代替命令
sudo curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
# 代替命令
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null卸载
Uninstall the Docker Engine, CLI, containerd, and Docker Compose packages:
console$ sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extrasImages, containers, volumes, or custom configuration files on your host aren't automatically removed. To delete all images, containers, and volumes:
console$ sudo rm -rf /var/lib/docker $ sudo rm -rf /var/lib/containerdRemove source list and keyrings
console$ sudo rm /etc/apt/sources.list.d/docker.list $ sudo rm /etc/apt/keyrings/docker.asc
You have to delete any edited configuration files manually.
apt-get --no-install-recommends install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin