【2025】ubuntu22.04 docker安装全过程

服务器是阿里云ubuntu22.04版本,因为centos已不再更新维护,所以改换ubuntu。

参考菜鸟教程Ubuntu Docker 安装 | 菜鸟教程

采用手动安装,自动安装报错,shell脚本不适合。

1、卸载旧版本

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

当前称为 Docker Engine-Community 软件包 docker-ce,所以后面如果卸载,也可以使用这条命令,软件包改为docker-ce。

2、使用 Docker 仓库进行安装

更新 apt 包索引。

sudo apt-get update

安装 apt 依赖包(跟docker无关,docker会用到的工具),用于通过HTTPS来获取仓库:

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

添加 Docker 的官方 GPG 密钥:

curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc

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

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
sudo apt-get update

安装最新版本的 Docker Engine-Community 和 containerd :

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

或者转到下一步安装特定版本,我改安装指定版本的,选择上一个版本最后的稳定版。要安装特定版本的 Docker Engine-Community,请在仓库中列出可用版本,然后选择一种安装。列出您的仓库中可用的版本:

$ 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
  ...

使用第二列中的版本字符串安装特定版本,例如 5:18.09.1~3-0~ubuntu-xenial。

sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

测试 Docker 是否安装成功,输入以下指令,打印出以下信息则安装成功:

$ sudo docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete                                                                                                                                  Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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/

3、安装过程中,出现包过时需要重启的选项,不确定需要重启哪些服务,索性重新启动了服务器。

4、测试依然不通过,显示是连接不上仓库hub,安装卸载了一次,依然不行,docker官网也无法连接了,判断是国外的库不让连接了,配置国内镜像仓库:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://docker.m.daocloud.io",
        "https://dockerproxy.com",
        "https://docker.nju.edu.cn"
    ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

参考博文:https://cloud.tencent.com/developer/article/2301228?policyId=1004

汇总国内可用镜像

DaoCloud 镜像站

加速地址:https://docker.m.daocloud.io

支持:Docker Hub、GCR、K8S、GHCR、Quay、NVCR 等

对外免费:是

网易云

加速地址:https://hub-mirror.c.163.com

支持:Docker Hub

对外免费:是

Docker 镜像代理

加速地址:https://dockerproxy.com

支持:Docker Hub、GCR、K8S、GHCR

对外免费:是

百度云

加速地址:https://mirror.baidubce.com

支持:Docker Hub

对外免费:是

南京大学镜像站

加速地址:https://docker.nju.edu.cn

支持:Docker Hub、GCR、GHCR、Quay、NVCR 等

对外免费:是

上海交大镜像站

加速地址:https://docker.mirrors.sjtug.sjtu.edu.cn/

支持:Docker Hub、GCR 等

限制:无

阿里云

加速地址:https://<your_code>.mirror.aliyuncs.com

支持:Docker Hub

限制:需要登录账号获取CODE

科大镜像站

加速地址:https://docker.mirrors.ustc.edu.cn

支持:Docker Hub、GCR、Quay

限制:仅供内部访问

Azure中国镜像

加速地址:https://dockerhub.azk8s.cn

支持:Docker Hub、GCR、Quay

限制:仅供内部访问

5、测试成功!

6、附上删除docker操作,除上面删除旧版命令外,还有如下方法:

删除安装包:

sudo apt-get purge docker-ce

删除镜像、容器、配置文件等内容:

sudo rm -rf /var/lib/docker

至此结束,docker的便利性不言而喻,与k8s结合使用,搭配流量监控,后续再更。

### 安装 DockerUbuntu 22.04 为了在 Ubuntu 22.04 上成功安装 Docker,需先更新现有的软件包列表并设置 Docker 的官方仓库。完成这些前置操作之后,可以继续安装 Docker 及其必要的依赖项。 #### 更新现有软件包列表 建议首先执行以下命令来确保拥有最新的软件包信息: ```bash sudo apt update && sudo apt upgrade -y ``` #### 添加 Docker APT 源公钥 通过下面的命令添加 Docker 开发团队维护的 GPG 密钥到系统中以验证下载文件的真实性: ```bash curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg ``` #### 设置稳定版存储库 接下来配置用于获取 Docker 软件包的源地址,并指定只使用稳定版本发布渠道: ```bash 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/null ``` #### 执行实际安装过程 现在再次刷新本地索引并将所需组件一并安装下来: ```bash sudo apt-get update sudo apt install docker-ce docker-ce-cli containerd.io -y ``` 这会自动处理所有必需品以及推荐选项[^1][^2]。 #### 验证安装成果 最后一步是确认服务已经启动并且能够正常工作: ```bash sudo systemctl status docker.service ``` 如果一切顺利的话应该能看到绿色的 active (running) 提示字样;另外还可以尝试运行 hello-world 测试镜像进一步检验环境是否搭建完毕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值