在京东云(JD Cloud)服务器上部署 Docker 环境,需完成一系列前置配置以确保安全、稳定、高效运行。以下是详细、分步骤的必备前置配置清单(适用于主流 Linux 发行版,如 CentOS 7/8、Ubuntu 20.04/22.04):
✅ 一、基础系统准备(必做)
-
选择合适的云服务器实例
- 推荐:
计算型(C系列)或通用型(G系列),内存 ≥ 2GB(Docker 基础运行+容器负载) - 操作系统:优先选用京东云官方镜像(如
Ubuntu 22.04 LTS或CentOS 7.9/8 Stream),避免使用已 EOL 版本(如 CentOS 6/7 已停止维护,建议 CentOS 8 Stream 或迁移至 Rocky/AlmaLinux) - 确保实例已通过安全组放行必要端口(如 SSH 22、Docker API 2375/2376(慎开)、Web服务端口等)
- 推荐:
-
系统更新与基础工具安装
# Ubuntu sudo apt update && sudo apt upgrade -y sudo apt install -y curl wget gnupg2 software-properties-common ca-certificates # CentOS/RHEL/Rocky/AlmaLinux sudo yum update -y # CentOS 7 sudo dnf update -y # CentOS 8+/Rocky/Alma sudo yum install -y yum-utils device-mapper-persistent-data lvm2 curl wget gnupg2 -
配置时区与时间同步(关键!)
sudo timedatectl set-timezone Asia/Shanghai sudo systemctl enable --now chronyd # RHEL/CentOS/Rocky # 或 Ubuntu:sudo systemctl enable --now systemd-timesyncd
✅ 二、Docker 安装前核心依赖配置(关键项)
| 配置项 | 说明 | 命令示例 |
|---|---|---|
| ✅ 内核版本 ≥ 3.10(推荐 ≥ 4.15) | Docker 运行依赖内核特性(cgroups, namespaces) | uname -r → 若低于要求,需升级内核(京东云支持自定义镜像或更换高内核实例) |
| ✅ 开启 IPv4 转发(必需) | 否则容器间网络、桥接网络异常 | echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf && sudo sysctl -p |
| ✅ 配置 ulimit(防容器启动失败) | Docker daemon 默认受限,需提升文件描述符限制 | 编辑 /etc/security/limits.conf:* soft nofile 65536* hard nofile 65536并确保 /etc/pam.d/common-session 包含 session required pam_limits.so |
| ✅ 配置 cgroup 驱动(推荐 systemd) | 避免与 Kubernetes 兼容问题(若后续接入 K8s) | Ubuntu/CentOS 8+ 默认使用 systemd;检查:cat /proc/1/comm 应为 systemd;Docker 安装后可在 /etc/docker/daemon.json 显式指定:{"exec-opts": ["native.cgroupdriver=systemd"]} |
✅ 三、Docker 官方安装(推荐方式,非 yum/apt 默认源)
⚠️ 注意:京东云镜像源(如
mirrors.jdcloud.com)不托管 Docker CE 官方包,请使用 Docker 官方源或清华/阿里等可信镜像源。
推荐安装流程(以 Ubuntu 22.04 为例):
# 1. 添加 Docker 官方 GPG 密钥(使用国内镜像提速)
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 2. 添加清华源仓库(比官方更快)
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 3. 安装 Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
# 4. 启动并设为开机自启
sudo systemctl enable docker
sudo systemctl start docker
# 5. 验证(非 root 用户可执行 docker 命令)
sudo usermod -aG docker $USER # 当前用户加入 docker 组(需重新登录或执行 newgrp docker)
docker run --rm hello-world # 测试成功即 OK
✅ CentOS/RHEL 类似,使用清华源:
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/
✅ 四、京东云环境增强配置(最佳实践)
| 项目 | 建议操作 | 说明 |
|---|---|---|
| ✅ 配置 Docker 镜像提速器(强烈推荐) | 编辑 /etc/docker/daemon.json:json<br>{<br> "registry-mirrors": [<br> "https://docker.mirrors.ustc.edu.cn",<br> "https://hub-mirror.c.163.com",<br> "https://mirror.baidubce.com"<br> ],<br> "log-driver": "json-file",<br> "log-opts": {"max-size": "100m", "max-file": "3"}<br>}然后 sudo systemctl restart docker |
提速拉取镜像(京东云华北区域推荐中科大或网易镜像);同时配置日志轮转防磁盘占满 |
| ✅ 配置京东云容器镜像服务(可选但推荐) | 登录 京东云容器镜像服务 CR → 创建命名空间 → 获取私有 Registry 地址(如 cr.cn-north-1.jdlabs.com.cn/your-ns)→ 配置 ~/.docker/config.json 登录认证 |
用于私有镜像托管、CI/CD 集成、安全合规 |
| ✅ 关闭 swap(Kubernetes 场景必需) | sudo swapoff -a && sudo sed -i '/swap/d' /etc/fstab |
Docker + K8s 要求禁用 swap,否则 kubelet 启动失败 |
| ✅ 配置防火墙(UFW/firewalld) | Ubuntu:sudo ufw allow OpenSSH;CentOS:sudo firewall-cmd --permanent --add-service=ssh && sudo firewall-cmd --reload |
切勿开放 Docker 默认监听的 2375/2376 端口(高危!),如需远程管理,请用 SSH 隧道或 TLS 认证 |
✅ 五、验证与收尾
# 检查服务状态
sudo systemctl status docker
# 查看 Docker 信息(确认 cgroup driver、存储驱动等)
docker info | grep -E "(Storage|Cgroup|Kernel)"
# 检查是否启用 overlay2(推荐存储驱动)
docker info | grep "Storage Driver"
# 测试运行一个轻量容器
docker run -d --name nginx-test -p 8080:80 nginx:alpine
curl -I http://localhost:8080 # 应返回 200 OK
docker stop nginx-test && docker rm nginx-test
📌 补充提醒:
- ❌ 不要使用
curl https://get.docker.com | bash(存在安全风险,且未适配京东云网络环境); - ❌ 避免在生产环境使用
--privileged或开放 Docker Socket(/var/run/docker.sock)给不可信容器; - ✅ 建议后续部署
docker-compose(v2.x)及portainer(可视化管理)提升运维效率; - 🔐 如需对接京东云 K8s(JDCloud Kubernetes Service, JKES),请额外配置
containerd+systemdcgroup driver + 禁用 swap + 配置kubelet参数。
需要我为你提供:
- ✅ 一键脚本(适配 Ubuntu/CentOS 自动化部署)?
- ✅ Docker + Nginx + MySQL 多容器编排示例(docker-compose.yml)?
- ✅ 京东云 CR 私有仓库完整配置指南(含 CI/CD 集成)?
- ✅ 安全加固 checklist(SELinux/AppArmor、rootless Docker、镜像扫描)?
欢迎随时告知,我可以立即生成 👇
云知道CLOUD