Bug难找的认知根源:工作记忆、确认偏差与可观测性调试
2026/9/26 3:59:04
Docker支持主流的Linux发行版:
支持的发行版:
内核要求:
检查内核版本:
uname-r# 输出示例:5.15.0-91-generic# 更新包索引sudoapt-getupdate# 安装必要的依赖sudoapt-getinstall-y\ca-certificates\curl\gnupg\lsb-release# 添加Docker官方GPG密钥sudomkdir-p /etc/apt/keyringscurl-fsSL https://download.docker.com/linux/ubuntu/gpg|\sudogpg --dearmor -o /etc/apt/keyrings/docker.gpg# 设置Docker仓库echo\"deb [arch=$(dpkg --print-architecture)signed-by=/etc/apt/keyrings/docker.gpg] \ https://download.docker.com/linux/ubuntu \$(lsb_release -cs)stable"|\sudotee/etc/apt/sources.list.d/docker.list>/dev/null# 更新包索引sudoapt-getupdate# 安装Docker Enginesudoapt-getinstall-y\docker-ce\docker-ce-cli\containerd.io\docker-buildx-plugin\docker-compose-plugin# 验证安装sudodockerrun hello-world# 下载并执行官方安装脚本curl-fsSL https://get.docker.com -o get-docker.shsudoshget-docker.sh# 清理安装脚本rmget-docker.sh# 列出可用版本apt-cachemadison docker-ce# 安装特定版本(例如5:24.0.0-1~ubuntu.22.04~jammy)sudoapt-getinstall-y\docker-ce=5:24.0.0-1~ubuntu.22.04~jammy\docker-ce-cli=5:24.0.0-1~ubuntu.22.04~jammy\containerd.io# 卸载旧版本sudoyum remove -ydocker\docker-client\docker-client-latest\docker-common\docker-latest\docker-latest-logrotate\docker-logrotate\docker-engine# 安装必要的依赖sudoyuminstall-y yum-utils# 添加Docker仓库sudoyum-config-manager\--add-repo\https://download.docker.com/linux/centos/docker-ce.repo# 安装Docker Enginesudoyuminstall-y\docker-ce\docker-ce-cli\containerd.io\docker-buildx-plugin\docker-compose-plugin# 启动Docker服务sudosystemctl startdocker# 设置开机自启sudosystemctlenabledocker# 验证安装sudodockerrun hello-world# 启动Docker服务sudosystemctl startdocker# 停止Docker服务sudosystemctl stopdocker# 重启Docker服务sudosystemctl restartdocker# 查看Docker服务状态sudosystemctl statusdocker# 设置开机自启sudosystemctlenabledocker# 禁用开机自启sudosystemctl disabledocker# 查看Docker服务日志sudojournalctl -u docker.servicesudojournalctl -fu docker.service# 实时查看# 查看Docker版本docker--version# 输出:Docker version 24.0.7, build afdd53b# 查看详细信息dockerversion# 查看系统信息dockerinfo# 运行测试容器dockerrun hello-worldhello-world输出示例:
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. 3. The Docker daemon created a new container from that image. 4. The Docker daemon streamed that output to the Docker client.# 以管理员身份运行PowerShell# 启用WSL功能dism.exe/online/enable-feature/featurename:Microsoft-Windows-Subsystem-Linux/all/norestart# 启用虚拟机平台dism.exe/online/enable-feature/featurename:VirtualMachinePlatform/all/norestart# 重启计算机Restart-Computer# 设置WSL 2为默认版本wsl--set-default-version 2# 安装Linux内核更新包# 从Microsoft下载并安装WSL2 Linux内核更新包docker--version docker run hello-world// Settings → Docker Engine → 编辑daemon.json{"builder":{"gc":{"defaultKeepStorage":"20GB","enabled":true}},"experimental":false,"features":{"buildkit":true},"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]}# 方法1:手动安装# 1. 双击下载的.dmg文件# 2. 将Docker图标拖到Applications文件夹# 3. 从Applications启动Docker# 方法2:使用Homebrewbrewinstall--caskdocker# 启动Docker Desktopopen-a Docker# 等待Docker引擎启动docker--versiondockerrun hello-world# 启用Rosetta 2模拟(如需运行x86镜像)softwareupdate --install-rosetta# 在Docker Desktop设置中:# Settings → Features in development → Use Rosetta for x86/amd64 emulation图形化界面功能:
命令行集成:
# 在Windows PowerShell、CMD或macOS Terminal中直接使用dockerpsdockerimagesdocker-composeupDocker Daemon的主配置文件:
文件位置:
/etc/docker/daemon.jsonC:\ProgramData\docker\config\daemon.json~/.docker/daemon.json(通过Docker Desktop管理)完整配置示例:
{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn","https://hub-mirror.c.163.com"],"insecure-registries":["192.168.1.100:5000","registry.local.com"],"max-concurrent-downloads":10,"max-concurrent-uploads":5,"log-driver":"json-file","log-opts":{"max-size":"10m","max-file":"3","labels":"production_status","env":"os,customer"},"storage-driver":"overlay2","storage-opts":["overlay2.override_kernel_check=true"],"data-root":"/var/lib/docker","exec-root":"/var/run/docker","hosts":["unix:///var/run/docker.sock"],"default-address-pools":[{"base":"172.80.0.0/16","size":24}],"default-ulimits":{"nofile":{"Name":"nofile","Hard":64000,"Soft":64000}},"dns":["8.8.8.8","8.8.4.4"],"dns-search":["example.com"],"live-restore":true,"userland-proxy":false,"experimental":false,"metrics-addr":"127.0.0.1:9323","fixed-cidr":"172.17.0.0/16","bip":"172.17.0.1/16","mtu":1500,"debug":false,"labels":["environment=production","region=us-west"]}{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"],"insecure-registries":["192.168.1.100:5000"]}registry-mirrors: 镜像加速器列表insecure-registries: 允许不安全的私有仓库(HTTP){"log-driver":"json-file","log-opts":{"max-size":"10m","max-file":"3"}}支持的日志驱动:
json-file: 默认,JSON格式syslog: 系统日志journald: systemd journalgelf: Graylog Extended Log Formatfluentd: Fluentd日志收集awslogs: Amazon CloudWatchnone: 禁用日志{"storage-driver":"overlay2","data-root":"/var/lib/docker"}支持的存储驱动:
overlay2: 推荐,性能最好aufs: 旧版Ubuntudevicemapper: RHEL/CentOS 7btrfs: Btrfs文件系统zfs: ZFS文件系统{"bip":"172.17.0.1/16","fixed-cidr":"172.17.0.0/16","default-address-pools":[{"base":"172.80.0.0/16","size":24}],"dns":["8.8.8.8","8.8.4.4"],"mtu":1500}bip: Docker0网桥的IP地址fixed-cidr: 容器IP范围default-address-pools: 自定义网络的默认地址池dns: DNS服务器mtu: 网络MTU大小{"default-ulimits":{"nofile":{"Name":"nofile","Hard":64000,"Soft":64000},"nproc":{"Name":"nproc","Hard":64000,"Soft":64000}}}# 修改配置文件后sudosystemctl daemon-reloadsudosystemctl restartdocker# 验证配置dockerinfo# 检查配置错误sudojournalctl -u docker.service --no-pager|tail-n50问题:
解决方案:使用国内镜像加速器
| 提供商 | 地址 | 特点 |
|---|---|---|
| 中科大 | https://docker.mirrors.ustc.edu.cn | 稳定,速度快 |
| 网易 | https://hub-mirror.c.163.com | 老牌服务商 |
| 阿里云 | https://[your-id].mirror.aliyuncs.com | 需注册,个性化 |
| 腾讯云 | https://mirror.ccs.tencentyun.com | 需内网访问 |
| DaoCloud | https://f1361db2.m.daocloud.io | 社区服务 |
# 创建或编辑配置文件sudonano/etc/docker/daemon.json# 添加镜像加速器{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn","https://hub-mirror.c.163.com"]}# 重启Docker服务sudosystemctl daemon-reloadsudosystemctl restartdocker# 验证配置dockerinfo|grep-A5"Registry Mirrors"图形界面配置:
"registry-mirrors"配置# 1. 登录阿里云容器镜像服务# https://cr.console.aliyun.com/# 2. 获取专属加速地址# 控制台 → 镜像加速器 → 复制加速器地址# 3. 配置{"registry-mirrors":["https://xxxxxxxx.mirror.aliyuncs.com"]}# 清空本地镜像(可选)dockersystem prune -a# 拉取镜像测试速度timedockerpull ubuntu:22.04# 使用加速器前:# real 3m25.432s# 使用加速器后:# real 0m12.685s默认情况:
# 需要sudo权限sudodockerps# 每次都输入sudo很不方便目标:
# 普通用户直接使用dockerps# 创建docker组(通常已存在)sudogroupadddocker# 将当前用户添加到docker组sudousermod-aGdocker$USER# 或添加指定用户sudousermod-aGdockerusername# 激活组权限(选择一种方式)# 方法1:注销并重新登录# 方法2:使用newgrpnewgrpdocker# 方法3:重启系统# 验证dockerpsgroups# 检查是否包含docker组# 查看docker.sock权限ls-l /var/run/docker.sock# 输出:srw-rw---- 1 root docker 0 Feb 10 10:00 /var/run/docker.sock# 如果权限不正确,修改sudochmod666/var/run/docker.sock# 临时方案,不推荐# 推荐方案:确保用户在docker组sudochownroot:docker /var/run/docker.socksudochmod660/var/run/docker.sock⚠️重要警告:
将用户添加到docker组等同于授予root权限!
原因:
# 用户可以通过挂载根目录获得系统完全控制dockerrun -it -v /:/host ubuntuchroot/host# 现在可以访问宿主机的所有文件最佳实践:
Rootless模式让Docker以非root用户运行,提高安全性:
# 安装rootless Dockercurl-fsSL https://get.docker.com/rootless|sh# 设置环境变量exportPATH=/home/$USER/bin:$PATHexportDOCKER_HOST=unix:///run/user/$(id-u)/docker.sock# 添加到shell配置echo'export PATH=/home/$USER/bin:$PATH'>>~/.bashrcecho'export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock'>>~/.bashrc# 启动rootless Dockersystemctl --user startdockersystemctl --userenabledocker# 验证dockerps限制:
# 1. 检查Docker版本docker--versiondockerversiondockerinfo# 2. 检查服务状态sudosystemctl statusdocker# 3. 检查存储驱动dockerinfo|grep"Storage Driver"# 4. 检查镜像加速器dockerinfo|grep-A5"Registry Mirrors"# 5. 检查网络dockernetworkls# 6. 检查数据卷dockervolumels# 7. 运行测试容器dockerrun --rm hello-worlddockerrun --rm -it ubuntu:22.04echo"Hello Docker"# 测试容器启动速度timedockerrun --rm alpineecho"test"# 测试网络性能dockerrun --rm networkstatic/iperf3 -c iperf.he.net# 测试磁盘I/Odockerrun --rm ubuntuddif=/dev/zeroof=/tmp/testbs=1Mcount=1024# 清理测试容器和镜像dockersystem prune -a# 查看磁盘使用dockersystemdf通过本章学习,我们完成了Docker的安装和配置:
✅Linux安装
✅Docker Desktop
✅配置文件
✅镜像加速器
✅权限设置
在第5章中,我们将学习Docker命令行基础:
掌握命令行是使用Docker的基础技能。
本章思考题:
相关资源: