前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker 最佳实战:Ubuntu 22.04 LTS 在线安装 Docker

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

作者头像
运维有术
发布2024-07-06 08:04:55
3100
发布2024-07-06 08:04:55
举报
文章被收录于专栏:运维有术运维有术

你好,欢迎来到运维有术

今天分享的内容是 Docker 最佳实战「2024」 系列文档中的 Ubuntu 22.04 LTS 在线安装 Docker

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

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

主机名

IP

CPU(核)

内存(GB)

系统盘(GB)

数据盘(GB)

用途

docker-node-1

192.168.9.81

4

16

40

100

Docker 节点 1

合计

1

4

16

40

100

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

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

1. 前置条件

Ubuntu 安装 Docker 有两种方式:

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

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

1.1 操作系统基础配置

  • 挂载一块独立的数据盘到 /data(建议)
  • 创建 Docker 数据目录
代码语言:javascript
复制
mkdir /data/docker

1.2 替换系统软件源

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

  • 备份系统默认源
代码语言:javascript
复制
cp /etc/apt/sources.list /etc/apt/sources.list.bak
  • 用下面的内容替换原始文件
代码语言:javascript
复制
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
  • 更新索引信息
代码语言:javascript
复制
apt-get update

2. 安装 Docker

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

2.1 安装依赖

代码语言:javascript
复制
apt-get install ca-certificates curl gnupg

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

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

代码语言:javascript
复制
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  tee /etc/apt/sources.list.d/docker.list > /dev/null

2.3 安装最新版的 Docker

代码语言:javascript
复制
apt-get update
apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

2.4 自定义配置文件

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

代码语言:javascript
复制
cat > /etc/docker/daemon.json <<EOF
{
  "data-root": "/data/docker",
  "exec-opts": [
    "native.cgroupdriver=systemd"
  ],
  "log-level": "info",
  "log-opts": {
    "max-size": "100m",
    "max-file": "5"
  },
  "storage-driver": "overlay2"
}
EOF

2.5 启动 Docker

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

代码语言:javascript
复制
systemctl restart docker
systemctl enable docker --now

2.6 验证 Docker

  • 查看 Docker 信息
代码语言:javascript
复制
 docker info
  • 使用镜像 hello-world 创建测试容器
代码语言:javascript
复制
 docker run --rm hello-world
  • 正确执行输出结果如下:
代码语言:javascript
复制
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
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. 自动化 Shell 脚本

文章中所有操作步骤,已全部编排为自动化脚本,包含以下内容(因篇幅限制,不在此文档中展示):

  • Shell 脚本自动部署 Docker
  • Ansible 脚本自动部署 Docker
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-07-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 运维有术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 前置条件
    • 1.1 操作系统基础配置
      • 1.2 替换系统软件源
      • 2. 安装 Docker
        • 2.1 安装依赖
          • 2.2 信任 Docker 的 GPG 公钥并添加仓库
            • 2.3 安装最新版的 Docker
              • 2.4 自定义配置文件
                • 2.5 启动 Docker
                  • 2.6 验证 Docker
                  • 3. 自动化 Shell 脚本
                  相关产品与服务
                  容器服务
                  腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档