Docker 最佳实战:Ubuntu 22.04 LTS 在线安装 Docker

本文将详细介绍如何在操作系统 Ubuntu 22.04.3 LTS 中,在线安装 Docker。

实战服务器配置 (架构 1:1 复刻小规模生产环境,配置略有不同)

主机名IPCPU(核)内存(GB)系统盘(GB)数据盘(GB)用途
docker-node-1192.168.9.8141640100Docker 节点 1
合计141640100

实战环境涉及软件版本信息

  • 操作系统:Ubuntu 22.04.3 LTS
  • Docker:27.0.3

1. 前置条件

Ubuntu 安装 Docker 有两种方式:

  • 使用 Docker 软件源,在线安装
  • 使用 Docker 二进制安装包,离线安装

本文实战演示如何使用 Docker 软件源,在线安装 Docker。整个安装过程还是比较简单的,如果安装失败基本上都是网络原因造成的。

1.1 操作系统基础配置

  • 挂载一块独立的数据盘到 /data(建议)
  • 创建 Docker 数据目录
  1. mkdir /data/docker

1.2 替换系统软件源

为了加快安装速度,本文使用清华源作为系统和 Docker 的软件源仓库。

  • 备份系统默认源
  1. cp /etc/apt/sources.list /etc/apt/sources.list.bak
  • 用下面的内容替换原始文件
  1. # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
  2. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
  3. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
  4. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
  5. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
  6. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
  7. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
  8. # 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
  9. deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
  10. # deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
  11. # 预发布软件源,不建议启用
  12. # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
  13. # # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
  • 更新索引信息
  1. apt-get update

2. 安装 Docker

以下所有操作均在 root 用户下完成,并假定操作系统是新安装的干净环境。

2.1 安装依赖

  1. apt-get install ca-certificates curl gnupg

2.2 信任 Docker 的 GPG 公钥并添加仓库

我们使用清华大学开源软件镜像站的软件仓库  mirrors.tuna.tsinghua.edu.cn,作为安装源。

  1. install -0755 -/etc/apt/keyrings
  2. curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  3. sudo chmod a+/etc/apt/keyrings/docker.gpg
  4. echo \
  5.   "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
  6.   "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  7.   tee /etc/apt/sources.list.d/docker.list > /dev/null

2.3 安装最新版的 Docker

  1. apt-get update
  2. apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

2.4 自定义配置文件

创建配置文件 /etc/docker/daemon.json

  1. cat > /etc/docker/daemon.json <<EOF
  2. {
  3.   "data-root": "/data/docker",
  4.   "exec-opts": [
  5.     "native.cgroupdriver=systemd"
  6.   ],
  7.   "log-level": "info",
  8.   "log-opts": {
  9.     "max-size": "100m",
  10.     "max-file": "5"
  11.   },
  12.   "storage-driver": "overlay2"
  13. }
  14. EOF

2.5 启动 Docker

启动 Docker 服务,并设置开机自启。

  1. systemctl restart docker
  2. systemctl enable docker --now

2.6 验证 Docker

  • 查看 Docker 信息
  1.  docker info
  • 使用镜像 hello-world 创建测试容器
  1.  docker run --rm hello-world
  • 正确执行输出结果如下:
  1. $ docker run --rm hello-world
  2. Unable to find image 'hello-world:latest' locally
  3. latest: Pulling from library/hello-world
  4. c1ec31eb5944: Pull complete 
  5. Digest: sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
  6. Status: Downloaded newer image for hello-world:latest
  7. Hello from Docker!
  8. This message shows that your installation appears to be working correctly.
  9. To generate this message, Docker took the following steps:
  10.  1. The Docker client contacted the Docker daemon.
  11.  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  12.     (amd64)
  13.  3. The Docker daemon created a new container from that image which runs the
  14.     executable that produces the output you are currently reading.
  15.  4. The Docker daemon streamed that output to the Docker client, which sent it
  16.     to your terminal.
  17. To try something more ambitious, you can run an Ubuntu container with:
  18.  $ docker run -it ubuntu bash
  19. Share images, automate workflows, and more with a free Docker ID:
  20.  https://hub.docker.com/
  21. For more examples and ideas, visit:
  22.  https://docs.docker.com/get-started/
声明: 本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
Docker

nerdctl:一个酷似docker又胜于docker的命令行工具

2024-12-17 14:41:35

Linux

Linux下qemu-kvm虚拟化安装配置

2024-2-29 17:35:50

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索