服务器Ubuntu 22.04 快速安装、更新、卸载 Docker

本文详细介绍了在Ubuntu22.04系统上快速安装、更新DockerEngine,以及卸载旧版本的方法,包括通过Apt仓库使用命令行安装、验证安装、升级和卸载操作,以及注意事项如删除相关数据。

1. 进入官网,获取最新安装信息

Install Docker Engine on Ubuntu

2. Uninstall old versions(卸载旧版本)

:::: code-group
::: code-group-item 输入

for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done

:::
::: code-group-item 输出

正在读取软件包列表... 完成

正在分析软件包的依赖关系树... 完成

正在读取状态信息... 完成                 

软件包 docker.io 未安装,所以不会被卸载
。。。

软件包 docker-doc 未安装,所以不会被卸载
。。。

软件包 docker-compose 未安装,所以不会被卸载
。。。
软件包 podman-docker 未安装,所以不会被卸载
。。。
软件包 containerd 未安装,所以不会被卸载
。。。

软件包 runc 未安装,所以不会被卸载

升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 8 个软件包未被升级。

:::
::::

3. Installation methods(选择安装方式)

可以选择:

桌面版安装

Apt仓库(命令行)安装

手动(离线)安装

脚本安装

4. Install using the Apt repository(Apt仓库 “命令行” 安装 步骤)

4.1 Set up Docker’s Apt repository(设置Apt仓库)

:::: code-group
::: code-group-item 输入

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] 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

:::
::: code-group-item 输出

too much

:::
::::

4.2 Install the Docker packages.(选择安装包)

:::: code-group
::: code-group-item 安装最新版

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

:::
::: code-group-item 指定版本

# To install a specific version of Docker Engine, start by listing the available versions in the repository:
# List the available versions:
apt-cache madison docker-ce | awk '{ print $3 }'

5:24.0.0-1~ubuntu.22.04~jammy
5:23.0.6-1~ubuntu.22.04~jammy
# Select the desired version and install:
VERSION_STRING=5:24.0.0-1~ubuntu.22.04~jammy
sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin
...

:::
::::

4.3 Verify that the Docker Engine installation is successful by running the hello-world image.(验证是否安装成功)

:::: code-group
::: code-group-item 输入

sudo docker run hello-world

:::
::: code-group-item 输出

Unable to find image 'hello-world:latest' locally

latest: Pulling from library/hello-world

719385e32844: Pull complete 

Digest: sha256:4f53e2564790c8e7856ec08e384732aa38dc43c52f02952483e3f003afbf23db

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/

:::
::::

5. Upgrade Docker Engine(更新Docker)

To upgrade Docker Engine, follow step 2 of the installation instructions, choosing the new version you want to install.(指定版本安装,或 再次安装最新版)(见 4. 安装步骤

6. Uninstall Docker Engine(卸载Docker)

6.1 Uninstall the Docker Engine, CLI, containerd, and Docker Compose packages:(卸载包)

:::: code-group
::: code-group-item 输入

sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

:::
::: code-group-item 输出

正在读取软件包列表... 完成

正在分析软件包的依赖关系树... 完成

正在读取状态信息... 完成                 

下列软件包是自动安装的并且现在不需要了:

  pigz slirp4netns

使用'sudo apt autoremove'来卸载它(它们)。

下列软件包将被【卸载】:

  containerd.io* docker-buildx-plugin* docker-ce* docker-ce-cli*

  docker-ce-rootless-extras* docker-compose-plugin*

升级了 0 个软件包,新安装了 0 个软件包,要卸载 6 个软件包,有 5 个软件包未被升级。

解压缩后将会空出 408 MB 的空间。

您希望继续执行吗? [Y/n] y
。。。
正在清除 docker-ce (5:24.0.6-1~ubuntu.22.04~jammy) 的配置文件 ...

正在清除 containerd.io (1.6.24-1) 的配置文件 ...

:::
::::

6.2 Images, containers, volumes, or custom configuration files on your host aren’t automatically removed. To delete all images, containers, and volumes:(删除数据)

:::: code-group
::: code-group-item 输入

sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

:::
::: code-group-item 输出


:::
::::

附录

<think>好的,用户现在想在Ubuntu 22.04安装Docker并通过Docker部署Grafana。之前我已经给过在CentOS上安装Docker和Grafana的指南,现在需要调整到Ubuntu系统。首先,我需要回顾用户提供的引用内容,里面有多个关于Ubuntu安装Docker的步骤,可能存在重复或不同方法,需要整合这些信息。 用户给出的引用[1]到[4]都是关于在Ubuntu 22.04安装DockerDocker Compose的不同方法。例如,引用[1]和[4]都提到了添加GPG密钥和设置仓库,但步骤稍有不同。需要确定一个标准且可靠的安装流程,避免冲突。比如,用户可能在之前的对话中使用过CentOS的命令,但现在切换到Ubuntu,需要注意包管理工具的不同(yum换成apt)。 另外,用户提到要通过Docker安装Grafana,之前的回答已经涉及使用Docker运行Grafana的步骤,但需要调整可能存在的路径或命令差异,比如Ubuntu下的目录权限问题。同时,用户可能关心数据持久化、端口配置以及如何验证安装是否成功。 需要确保Docker安装步骤正确无误,包括卸载旧版本、安装依赖、添加GPG密钥、设置仓库、安装Docker引擎以及验证。然后,部署Grafana部分需要包括拉取镜像、创建存储目录、运行容器、访问和验证步骤。可能还需要考虑使用Docker Compose来管理容器,但用户当前问题可能更关注基础安装。 还需要注意引用标记的正确使用,根据用户提供的引用内容,引用[1]和[4]可能涉及安装Docker的不同方法,需要选择最合适的步骤,并正确标注来源。例如,引用[1]中的步骤可能更详细,适合作为主要参考,而引用[3]提到了阿里源,可能作为配置镜像加速的选项。 最后,生成的相关问题需要与当前主题相关,比如如何配置数据源、使用Docker Compose管理,或者如何升级Grafana版本等。确保问题对用户后续操作有帮助。</think>### 分步指南:Ubuntu 22.04 安装 Docker 并部署 Grafana --- #### **1. 卸载旧版 Docker(如存在)** ```bash sudo apt remove docker docker-engine docker.io containerd runc sudo apt purge docker-ce docker-ce-cli containerd.io sudo rm -rf /var/lib/docker ``` > 确保完全清理残留配置[^1][^4] --- #### **2. 安装 Docker 环境** ##### **2.1 更新系统并安装工具链** ```bash sudo apt update sudo apt install -y ca-certificates curl gnupg lsb-release ``` ##### **2.2 添加 Docker 官方 GPG 密钥** ```bash sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg ``` ##### **2.3 配置 Docker APT 源** ```bash echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null ``` > 如需国内加速,可替换为阿里云源[^3]: > `https://mirrors.aliyun.com/docker-ce/linux/ubuntu` ##### **2.4 安装 Docker 引擎** ```bash sudo apt update sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin ``` ##### **2.5 验证安装** ```bash sudo docker run hello-world # 出现 "Hello from Docker!" 表示成功[^1][^4] ``` --- #### **3. 部署 Grafana 容器** ##### **3.1 拉取官方镜像** ```bash docker pull grafana/grafana:latest ``` ##### **3.2 创建持久化目录** ```bash sudo mkdir -p /opt/grafana/{data,conf} sudo chmod -R 777 /opt/grafana # 解决容器权限问题 ``` ##### **3.3 启动容器** ```bash docker run -d \ --name grafana \ -p 3000:3000 \ -v /opt/grafana/data:/var/lib/grafana \ -v /opt/grafana/conf:/etc/grafana \ grafana/grafana ``` - `-v /opt/grafana/data`:数据持久化存储 - `-v /opt/grafana/conf`:自定义配置文件目录(可选)[^5] ##### **3.4 验证容器状态** ```bash docker ps | grep grafana # 应显示运行中的容器 ``` --- #### **4. 访问 Grafana** 1. 浏览器打开 `http://<服务器IP>:3000` 2. 使用默认账号 `admin/admin` 登录 3. 首次登录需重置密码 --- ### **高级配置** #### **使用 Docker Compose 管理** 创建 `docker-compose.yml`: ```yaml version: '3' services: grafana: image: grafana/grafana ports: - "3000:3000" volumes: - /opt/grafana/data:/var/lib/grafana - /opt/grafana/conf:/etc/grafana restart: always ``` 启动服务: ```bash docker compose up -d ``` > 需先安装 `docker-compose-plugin`[^2][^4] --- #### **常见问题处理** 1. **端口冲突**:修改 `-p 8080:3000` 更换映射端口 2. **数据恢复**:直接备份 `/opt/grafana/data` 目录 3. **插件安装**:在容器内执行 `grafana-cli plugins install <插件名>`[^5] --- ### 相关问题 1. 如何通过 Docker Compose 实现 Grafana 与 Prometheus 的联动部署? 2. Grafana 如何配置 SMTP 邮件通知功能? 3. 如何升级 Docker 版 Grafana 到最新版本? --- [^1]: Ubuntu 22.04 Docker 安装验证方法 [^2]: Docker Compose 多容器管理实践 [^3]: 国内镜像源加速配置方案 [^4]: 容器持久化存储最佳实践 [^5]: Grafana 官方容器部署文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值