在Linux上安装Docker并配置镜像加速器:从入门到实战

 

🔥「炎码工坊」技术弹药已装填!
点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】

引言

Docker作为容器化技术的标杆工具,已经成为现代软件开发和运维的必备技能。对于程序员和技术爱好者来说,在Linux系统上搭建Docker环境是探索云原生、微服务架构的第一步。本文将详解如何在主流Linux发行版(CentOS/Ubuntu)上完成Docker安装、配置国内镜像加速器、验证安装及服务管理的完整流程。


一、安装Docker引擎

1.1 系统要求

  • 支持的系统:CentOS 7+/Ubuntu 16.04+/Debian 9+/Rocky Linux
  • 内核版本:3.10以上
  • 用户权限:需具备sudo权限

1.2 安装依赖包

# CentOS/Rocky Linux
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

# Ubuntu/Debian
sudo apt update && sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release

1.3 添加Docker官方仓库

# CentOS/Rocky Linux (使用阿里云镜像加速)
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# Ubuntu (官方源)
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

1.4 安装Docker引擎

# CentOS/Rocky Linux
sudo yum install -y docker-ce docker-ce-cli containerd.io

# Ubuntu
sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io

替代方案:使用官方一键安装脚本(仅限测试环境)

curl -fsSL https://get.docker.com | sudo sh

二、配置国内镜像加速器(关键步骤)

2.1 阿里云镜像加速(需注册账号)

  1. 登录阿里云容器镜像服务[1]
  2. 获取专属加速地址(如https://<your-id>.mirror.aliyuncs.com
  3. 创建配置文件:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://<your-id>.mirror.aliyuncs.com"]
}
EOF

2.2 公共镜像源(中科大/腾讯云)

{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",  // 中科大
    "https://hub-mirror.c.163.com",       // 网易
    "https://mirror.ccs.tencentyun.com"    // 腾讯云
  ]
}

2.3 重启Docker服务

sudo systemctl daemon-reload
sudo systemctl restart docker

三、服务管理与开机自启

3.1 启动Docker服务

sudo systemctl start docker

3.2 设置开机自启

sudo systemctl enable docker

3.3 验证服务状态

sudo systemctl status docker
# 预期输出包含 "active (running)"

四、验证安装有效性

4.1 检查版本信息

docker --version
# 示例输出: Docker version 27.1.1, build 6312585

4.2 运行测试容器

docker run hello-world
# 成功输出 "Hello from Docker!" 标志安装完成

4.3 常用验证命令

功能命令示例
查看运行容器docker ps
拉取Nginx镜像docker pull nginx
运行交互式容器docker run -it ubuntu bash

五、权限优化:免sudo使用Docker

5.1 将当前用户加入docker组

sudo usermod -aG docker $USER
newgrp docker  # 立即生效(或重新登录终端)

安全提示:docker组等价于root权限,生产环境请谨慎授权


六、常见问题排查

6.1 镜像拉取缓慢

  • 确认daemon.json配置正确
  • 切换多镜像源组合(如阿里云+中科大)
  • 使用docker info查看镜像加速器是否生效

6.2 服务启动失败

  • 检查端口冲突(默认2375/2376)
  • 查看日志:journalctl -u docker.service
  • 清理旧配置:sudo rm -rf /etc/docker/daemon.json

结语:进阶学习方向

  1. 容器编排:学习Docker Compose编排多容器应用
  2. 私有镜像仓库:搭建Harbor企业级镜像管理平台
  3. 安全加固:研究Docker Bench安全审计工具
  4. CI/CD集成:结合Jenkins/GitLab CI实现自动化部署

通过本文的完整实践,您已掌握Docker在Linux环境的核心部署能力。下一步建议尝试部署实际应用(如MySQL/Nginx),深入理解容器化技术带来的开发运维范式变革。

引用链接

[1] 阿里云容器镜像服务: https://cr.console.aliyun.com/

 

🚧 您已阅读完全文99%!缺少1%的关键操作:
加入「炎码燃料仓」
🚀 获得:
√ 开源工具红黑榜 √ 项目落地避坑指南
√ 每周BUG修复进度+1%彩蛋
(温馨提示:本工坊不打灰工,只烧脑洞🔥)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值