IT学习
Linux环境下安装docker
CentOS7系统重置root账户密码
Nginx学习
Windows电脑修改桌面默认位置
本文档使用 MrDoc 发布
-
+
首页
Linux环境下安装docker
>i **Linux版本** > > Ubuntu 20.04.2 LTS # 一、安装过程 ## 1、卸载旧版本 Docker 的旧版本被称为 docker,docker.io 或 docker-engine 。 如果已安装,请卸载它们: ``` Linux shell sudo apt-get remove docker docker-engine docker.io containerd runc ``` ## 2、设置仓库 在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后才可以从仓库安装和更新 Docker 。 ### 2.1更新 apt 包索引 ``` Linux sudo apt-get update ``` ### 2.2安装 apt 依赖包,用于通过HTTPS来获取仓库: ``` Linux sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common ``` ### 2.3添加 Docker 的官方 GPG 密钥: ``` Linux sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc ``` 在这一步骤时,报错如下: >w **报错** > > curl: (23) failed writing body (0 != 3817) 怀疑是将下载的秘钥文件写入时发生错误,于是先创建对应目录`/etc/apt/keyrings`和文件`docker.asc`,再将命令中的`-o`替换为`>`,即把命令替换为`sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg > /etc/apt/keyrings/docker.asc`成功解决报错。 ### 2.4设置稳定版仓库 ``` Linux echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null ``` ## 3、安装 Docker Engine-Community ### 3.1更新 apt 包索引 ``` Linux sudo apt-get update ``` ### 3.2安装最新版本的 Docker Engine-Community 和 containerd ``` Linux sudo apt-get install docker-ce docker-ce-cli containerd.io ``` ### 3.3安装特定版本 要安装特定版本的 Docker Engine-Community,请在仓库中列出可用版本,然后选择一种安装。 #### 3.3.1列出仓库中可用的版本: ``` Linux apt-cache madison docker-ce ``` ``` 版本信息 docker-ce | 5:18.09.1~3-0~ubuntu-xenial | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu xenial/stable amd64 Packages docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu xenial/stable amd64 Packages docker-ce | 18.06.1~ce~3-0~ubuntu | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu xenial/stable amd64 Packages docker-ce | 18.06.0~ce~3-0~ubuntu | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu xenial/stable amd64 Packages ``` #### 3.3.2使用第二列中的版本字符串安装特定版本 例如 `5:18.09.1~3-0~ubuntu-xenial`。 ``` Linux sudo apt-get install docker-ce=5:18.09.1~3-0~ubuntu-xenial docker-ce-cli=5:18.09.1~3-0~ubuntu-xenial containerd.io ``` # 二、测试是否成功 ## 1、先设置docker国内源地址 默认是没有配置文件的,需要我们手动创建配置文件`/etc/docker/daemon.json` ``` Linux sudo vim /etc/docker/daemon.json ``` 输入国内源地址 ``` { "registry-mirrors": [ "https://docker.hpcloud.cloud", "https://docker.m.daocloud.io", "https://docker.unsee.tech", "https://docker.1panel.live", "http://mirrors.ustc.edu.cn", "https://docker.chenby.cn", "http://mirror.azure.cn", "https://dockerpull.org", "https://dockerhub.icu", "https://hub.rat.dev", "https://proxy.1panel.live", "https://docker.1panel.top", "https://docker.m.daocloud.io", "https://docker.1ms.run", "https://docker.ketches.cn" ] } ``` ## 2、重启docker服务,使配置生效 ``` Linux sudo systemctl daemon-reload sudo systemctl restart docker ``` 使用命令查看docker配置信息 ``` Linux sudo docker info ``` 如果输出显示配置的镜像源地址,说明设置成功。 ## 3、启动测试程序,验证安装成功 ``` Linux sudo docker run hello-world ``` 输出如下时,说明安装成功 ``` Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world e6590344b1a5: Pull complete Digest: sha256:7e1a4e2d11e2ac7a8c3f768d4166c2defeb09d2a750b010412b6ea13de1efb19 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/ ``` # 三、参考链接 1. [Ubuntu Docker 安装-菜鸟教程](https://www.runoob.com/docker/ubuntu-docker-install.html) 2. [2025最新Docker国内可用镜像源仓库地址分享-脚本之家](https://www.jb51.net/server/336073lax.htm)
wangrong
2025年4月5日 03:39
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码