Ubuntu学习笔记-安装docker容器

目录

  • 一、概述
    • [1.1 系统信息](#1.1 系统信息)
    • [1.2 Docker](#1.2 Docker)
  • 二、安装Docker
    • [2.1 安装准备](#2.1 安装准备)
    • [2.2 安装依赖工具用于通过http获取docker](#2.2 安装依赖工具用于通过http获取docker)
    • [2.3 添加 Docker 官方 GPG 密钥和软件源](#2.3 添加 Docker 官方 GPG 密钥和软件源)
      • [2.3.1 创建存放密钥的目录并下载 GPG 密钥](#2.3.1 创建存放密钥的目录并下载 GPG 密钥)
      • [2.3.2 安装 Docker Engine](#2.3.2 安装 Docker Engine)
      • [2.3.3 配置使用国内镜像源](#2.3.3 配置使用国内镜像源)
    • [2.4 测试](#2.4 测试)

天极

诗号: 执古纪,御今传,指天运地无极门,太上识道尊。

身背天极剑,全身散发着仙风道骨的气息,乃道门久未入世的先天。语气稳重中带着肃穆感,不容人造次。赏善分明的性格下,对待部属一视同仁,且就算是自家之人犯错,亦绝不宽待,因而受到道门之人的敬重。与师弟「地限」一同创建道镇伏魔崖,并开立「太上府」,于道界极富盛名。

一、概述

1.1 系统信息

bash 复制代码
zero@PC-LJM:~$ uname -a
Linux PC-LJM 6.18.33.2-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Thu Jun 18 21:54:43 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux

1.2 Docker

Docker 相当于一个轻量级的"虚拟机",但它的运行方式更高效、更灵活。它的核心价值在于环境标准化------让应用能在任何地方(你的笔记本、公司的服务器、云端的电脑)以完全相同的方式运行。

🧱 Docker 的核心概念

要理解 Docker,需要掌握这三个基本概念:

概念 说明 类比
镜像 (Image) 一个只读的模板,包含了运行某个应用所需的代码、运行环境、依赖库、配置文件等所有东西。 类似于安装光盘或系统快照,你用它来"安装"出容器。
容器 (Container) 由镜像运行起来的独立进程。它是一个轻量级的、可执行的沙箱环境。 就是用"安装光盘"装出来的一个独立的"小电脑"。你可以启动、停止、删除它。
仓库 (Repository) 存放和共享镜像的地方,最著名的是官方仓库 Docker Hub。 类似于应用商店或代码仓库(如GitHub),你可以从那里下载别人制作好的镜像。

二、安装Docker

2.1 安装准备

查看下docker版本,查询不到信息就表示没有安装。

bash 复制代码
zero@PC-LJM:~$ docker --version

首先清除相关旧的docker相关的包,避免干扰。

bash 复制代码
zero@PC-LJM:~$ for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
[sudo] password for zero:
Reading package lists... Done
Building dependency tree... Done
# 此处省略许多状态信息
0 upgraded, 0 newly installed, 0 to remove and 36 not upgraded.
zero@PC-LJM:~$

2.2 安装依赖工具用于通过http获取docker

bash 复制代码
zero@PC-LJM:~$ sudo apt-get update		# 更新库
zero@PC-LJM:~$ sudo apt-get install ca-certificates curl
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
ca-certificates is already the newest version (20260601~24.04.1).
ca-certificates set to manually installed.
curl is already the newest version (8.5.0-2ubuntu10.11).
curl set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 36 not upgraded.

2.3 添加 Docker 官方 GPG 密钥和软件源

📢这是最关键的一步,需要创建一个目录来存放密钥,下载 Docker 官方的 GPG 密钥,并将其添加到系统中。

2.3.1 创建存放密钥的目录并下载 GPG 密钥

bash 复制代码
zero@PC-LJM:~$ sudo install -m 0755 -d /etc/apt/keyrings
[sudo] password for zero:
# 使用阿里镜像云
zero@PC-LJM:~$ sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
zero@PC-LJM:~$

# 将 Docker 官方源替换为阿里云镜像源
zero@PC-LJM:~$ echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
zero@PC-LJM:~$

# 更新源索引
zero@PC-LJM:~$ sudo apt-get update
Get:1 https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble InRelease [48.5 kB]
Get:2 https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble/stable amd64 Packages [60.5 kB]
Hit:3 http://archive.ubuntu.com/ubuntu noble InRelease
Hit:4 http://security.ubuntu.com/ubuntu noble-security InRelease
Hit:5 http://archive.ubuntu.com/ubuntu noble-updates InRelease
Hit:7 http://archive.ubuntu.com/ubuntu noble-backports InRelease
Hit:6 https://developer.download.nvidia.cn/compute/cuda/repos/wsl-ubuntu/x86_64  InRelease
Fetched 109 kB in 1s (84.9 kB/s)
Reading package lists... Done

2.3.2 安装 Docker Engine

安装 Docker 的主程序、命令行工具、容器运行时以及 Docker Compose 插件

bash 复制代码
zero@PC-LJM:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
# 这边省略过程提示
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /usr/lib/systemd/system/docker.socket.
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.7) ...
zero@PC-LJM:~$

2.3.3 配置使用国内镜像源

创建配置文件并编辑
bash 复制代码
zero@PC-LJM:~$ sudo mkdir -p /etc/docker
zero@PC-LJM:~$ sudo nano /etc/docker/daemon.json
输入镜像源配置代码
bash 复制代码
{
  "registry-mirrors": ["https://docker.m.daocloud.io"]
}

然后保存退出,顺序如下:

  1. 按 Ctrl + O 保存
  2. 按 Enter 确认文件名
  3. 按 Ctrl + X 退出
重启 Docker 服务
bash 复制代码
zero@PC-LJM:~$ sudo systemctl daemon-reload		# 重新加载系统服务
zero@PC-LJM:~$ sudo systemctl restart docker		# 重启docker

2.4 测试

bash 复制代码
zero@PC-LJM:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
4f55086f7dd0: Pull complete
d5e71e642bf5: Download complete
Digest: sha256:96498ffd522e70807ab6384a5c0485a79b9c7c08ca79ba08623edcad1054e62d
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/

zero@PC-LJM:~$
相关推荐
不会C语言的男孩2 小时前
Docker 在嵌入式设备中的常用玩法
运维·docker·容器
Loving_enjoy2 小时前
DFT可测试性设计规则与检查:确保芯片可测试性的关键步骤
学习
r_oo_ki_e_2 小时前
Java反射机制学习笔记
java·笔记·学习
weixin_307779134 小时前
Linux下Docker Compose里运行的MySQL数据库故障诊断Shell脚本
linux·运维·mysql·docker·自动化
绝世番茄4 小时前
鸿蒙HarmonyOS ArkTS原生学习:缩放手势实现 PinchToZoom 深度解析
学习·华为·harmonyos·鸿蒙
a1117764 小时前
SLAM 学习笔记(四)后端(KF BA)
笔记·学习
XGeFei5 小时前
【Django学习笔记】—— Django 高并发通俗讲解
笔记·学习·django
六点_dn5 小时前
Linux学习笔记-chmod命令
linux·笔记·学习
MartinYeung55 小时前
[论文学习]揭示大语言模型智能体记忆模块中的隐私风险
人工智能·学习·语言模型