Ubuntu学习笔记-安装docker容器

Ubuntu学习笔记-安装docker容器 目录一、概述1.1 系统信息1.2 Docker二、安装Docker2.1 安装准备2.2 安装依赖工具用于通过http获取docker2.3 添加 Docker 官方 GPG 密钥和软件源2.3.1 创建存放密钥的目录并下载 GPG 密钥2.3.2 安装 Docker Engine2.3.3 配置使用国内镜像源创建配置文件并编辑输入镜像源配置代码重启 Docker 服务2.4 测试天极诗号执古纪御今传指天运地无极门太上识道尊。身背天极剑全身散发着仙风道骨的气息乃道门久未入世的先天。语气稳重中带着肃穆感不容人造次。赏善分明的性格下对待部属一视同仁且就算是自家之人犯错亦绝不宽待因而受到道门之人的敬重。与师弟「地限」一同创建道镇伏魔崖并开立「太上府」于道界极富盛名。一、概述1.1 系统信息zeroPC-LJM:~$uname-aLinux PC-LJM6.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/Linux1.2 DockerDocker 相当于一个轻量级的“虚拟机”但它的运行方式更高效、更灵活。它的核心价值在于环境标准化——让应用能在任何地方你的笔记本、公司的服务器、云端的电脑以完全相同的方式运行。 Docker 的核心概念要理解 Docker需要掌握这三个基本概念概念说明类比镜像 (Image)一个只读的模板包含了运行某个应用所需的代码、运行环境、依赖库、配置文件等所有东西。类似于安装光盘或系统快照你用它来“安装”出容器。容器 (Container)由镜像运行起来的独立进程。它是一个轻量级的、可执行的沙箱环境。就是用“安装光盘”装出来的一个独立的“小电脑”。你可以启动、停止、删除它。仓库 (Repository)存放和共享镜像的地方最著名的是官方仓库 Docker Hub。类似于应用商店或代码仓库如GitHub你可以从那里下载别人制作好的镜像。二、安装Docker2.1 安装准备查看下docker版本查询不到信息就表示没有安装。zeroPC-LJM:~$docker--version首先清除相关旧的docker相关的包避免干扰。zeroPC-LJM:~$forpkgindocker.io docker-docdocker-composedocker-compose-v2 podman-docker containerd runc;dosudoapt-getremove$pkg;done[sudo]passwordforzero: Reading package lists... Done Building dependency tree... Done# 此处省略许多状态信息0upgraded,0newly installed,0to remove and36not upgraded. zeroPC-LJM:~$2.2 安装依赖工具用于通过http获取dockerzeroPC-LJM:~$sudoapt-getupdate# 更新库zeroPC-LJM:~$sudoapt-getinstallca-certificatescurlReading package lists... Done Building dependency tree... Done Reading state information... Done ca-certificates is already the newest version(20260601~24.04.1). ca-certificatessetto manually installed.curlis already the newest version(8.5.0-2ubuntu10.11).curlsetto manually installed.0upgraded,0newly installed,0to remove and36not upgraded.2.3 添加 Docker 官方 GPG 密钥和软件源这是最关键的一步需要创建一个目录来存放密钥下载 Docker 官方的 GPG 密钥并将其添加到系统中。2.3.1 创建存放密钥的目录并下载 GPG 密钥zeroPC-LJM:~$sudoinstall-m0755-d/etc/apt/keyrings[sudo]passwordforzero:# 使用阿里镜像云zeroPC-LJM:~$sudocurl-fsSLhttps://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg-o/etc/apt/keyrings/docker.asc zeroPC-LJM:~$# 将 Docker 官方源替换为阿里云镜像源zeroPC-LJM:~$echo\deb [arch$(dpkg --print-architecture)signed-by/etc/apt/keyrings/docker.asc] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \$(./etc/os-releaseecho${UBUNTU_CODENAME:-$VERSION_CODENAME})stable|\sudotee/etc/apt/sources.list.d/docker.list/dev/null zeroPC-LJM:~$# 更新源索引zeroPC-LJM:~$sudoapt-getupdate Get:1 https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble InRelease[48.5kB]Get:2 https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble/stable amd64 Packages[60.5kB]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 Fetched109kBin1s(84.9kB/s)Reading package lists... Done2.3.2 安装 Docker Engine安装 Docker 的主程序、命令行工具、容器运行时以及 Docker Compose 插件zeroPC-LJM:~$sudoapt-getinstalldocker-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 triggersforman-db(2.12.0-4build2)... Processing triggersforlibc-bin(2.39-0ubuntu8.7)... zeroPC-LJM:~$2.3.3 配置使用国内镜像源创建配置文件并编辑zeroPC-LJM:~$sudomkdir-p/etc/docker zeroPC-LJM:~$sudonano/etc/docker/daemon.json输入镜像源配置代码{registry-mirrors:[https://docker.m.daocloud.io]}然后保存退出顺序如下按 Ctrl O 保存按 Enter 确认文件名按 Ctrl X 退出重启 Docker 服务zeroPC-LJM:~$sudosystemctl daemon-reload# 重新加载系统服务zeroPC-LJM:~$sudosystemctl restartdocker# 重启docker2.4 测试zeroPC-LJM:~$sudodockerrun hello-world Unable tofindimagehello-world:latestlocally latest: Pulling from library/hello-world 4f55086f7dd0: Pull complete d5e71e642bf5: Download complete Digest: sha256:96498ffd522e70807ab6384a5c0485a79b9c7c08ca79ba08623edcad1054e62d Status: Downloaded newer imageforhello-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 thehello-worldimage from the Docker Hub.(amd64)3. The Docker daemon created a new container from that imagewhichruns the executable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client,whichsent it to your terminal. To try somethingmoreambitious, you can run an Ubuntu container with: $dockerrun-itubuntubashShare images, automate workflows, andmorewith afreeDocker ID: https://hub.docker.com/ Formoreexamples and ideas, visit: https://docs.docker.com/get-started/ zeroPC-LJM:~$