红河哈尼族彝族自治州网站建设_网站建设公司_CMS_seo优化
2025/12/19 20:07:28 网站建设 项目流程

Docker CE 安装步骤 CentOS

步骤概览

  • 安装依赖

  • 添加 Docker CE 软件源

  • 安装 Docker 引擎与插件

1 安装依赖

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

说明:

  • yum-utils:提供 yum-config-manager​ 等工具

  • device-mapper-persistent-data、lvm2:为 Docker 的 devicemapper​ 存储驱动提供依赖

2 添加 Docker CE 软件源

任选其一:

# 官方源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 阿里云镜像源(国内更快)
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

说明:添加完成后可使用 yum makecache fast​ 刷新元数据(可选)。

3 安装 Docker

sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

说明:

  • 上述命令会安装 Docker CE 引擎CLIcontainerd​ 以及 Buildx​ 和 Compose​ 插件

  • 如需安装指定版本,可先列出可用版本:

    sudo yum list docker-ce.x86_64 --showduplicates | sort -r

    然后安装如:sudo yum install -y docker-ce-<VERSION_STRING>

4 启动与验证

# 启动并设置开机自启
sudo systemctl start docker
sudo systemctl enable docker
# 验证安装
docker version
docker info
# 运行测试镜像
sudo docker run --rm hello-world

说明:看到客户端与服务端版本信息,且能成功拉取并运行 hello-world​ 镜像,即表示安装成功。

Docker 常用命令速查

配置国内镜像加速

  • 创建或编辑配置文件:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["https://docker.m.daocloud.io","https://ccr.ccs.tencentyun.com","https://dockerhub.icu","https://docker.awsl9527.cn"]
}
EOF
  • 重新加载并重启服务(注意:修改 Docker 配置无需执行 systemctl daemon-reload,直接重启 docker 即可):

sudo systemctl restart docker
sudo systemctl enable docker
  • 验证配置是否生效:

docker info | grep -A 10 'Registry Mirrors'
  • 说明:

    • 建议优先使用HTTPS镜像源;如使用 HTTP​ 源,需确保 Docker 允许不安全仓库(不推荐)。

    • 若出现拉取失败,可暂时保留多个可用镜像源以提高成功率。

基础验证与拉取镜像

  • 验证 Docker 是否可用:

docker run --rm hello-world
  • 拉取并运行 Nginx:

docker pull nginx:latest
docker run --name web1 -d -p 80:80 nginx:latest
# 浏览器访问:http://服务器IP/
  • 常用查看与进入容器:

docker ps -a
docker logs -f web1
docker exec -it web1 /bin/bash
  • 说明:

    • 参数 -d​ 为后台运行,-p 80:80​ 将宿主机 80 端口映射到容器 80 端口。

镜像常用操作

  • 查看、打标签、查看详情、历史:

docker images
docker tag nginx:latest mynginx:v1
docker inspect nginx:latest
docker history nginx:latest
  • 导出与导入(镜像归档):

docker save -o nginx.tar nginx:latest
docker load -i nginx.tar
# 或:docker load < nginx.tar
  • 删除镜像:

docker rmi nginx:latest
# 强制删除(谨慎):docker rmi -f nginx:latest
  • 说明:

    • docker save/load​ 用于镜像(包含分层与历史);不要与 docker export/import(容器快照)混淆。

容器常用操作

  • 启动、停止、重启、删除:

docker start web1
docker stop web1
docker restart web1
docker rm -f web1   # -f 强制删除运行中的容器
  • 查看容器日志与进入交互:

docker logs -f --tail 100 web1
docker exec -it web1 /bin/bash
  • 说明:

    • 删除前请先停止容器;如容器使用了数据卷,可加 -v​ 一并清理卷(谨慎)。

常见问题与排错要点

  • 配置镜像加速后仍然慢或失败:

    • 检查 /etc/docker/daemon.json​ 语法是否正确(JSON 格式、逗号、引号)。

    • 执行 systemctl restart docker​ 使配置生效,并用 docker info​ 验证。

    • 临时更换或增加其他镜像源;避免使用不可靠或已失效的源。

  • 删除镜像时报错“被容器占用”:

    • docker stop​ 相关容器,再 docker rm;或 docker rm -f​ 强制删除容器后再删镜像。

    • 若镜像层仍被占用,说明仍有容器引用,需清理无用容器。

  • 运行容器端口访问不通:

    • 确认 -p 宿主机端口:容器端口​ 参数正确,且宿主机防火墙/安全组已放行对应端口。

  • 导入镜像后 REPOSITORY/TAG 为 <none>:

    • 使用 docker tag​ 重新命名,例如:docker tag <IMAGE_ID> myrepo/mynginx:latest。

自定义私有仓库 Registry 2 搭建与使用

部署 Registry 容器

  • 拉取镜像并启动服务,数据持久化到宿主机目录:

# 拉取镜像
docker pull registry:2
# 创建数据目录
sudo mkdir -p /opt/docker-registry/data
# 启动 Registry(推荐设置重启策略)
docker run -d \--name docker-registry \--restart=always \-p 5000:5000 \-v /opt/docker-registry/data:/var/lib/registry \registry:2
# 查看日志,确认监听 5000 端口
docker logs -f docker-registry
# 出现:level=info msg="listening on [::]:5000" 表示正常

说明:Registry 默认监听 5000​ 端口,数据默认存储在容器内的 /var/lib/registry,通过卷挂载实现持久化。


配置 Docker 客户端与私有仓库

  • 编辑或创建 Docker 客户端配置文件,添加私有仓库地址到 insecure-registries(HTTP 场景必需),并保留常用的日志与存储驱动配置:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<'EOF'
{"registry-mirrors": ["https://docker.m.daocloud.io","https://ccr.ccs.tencentyun.com","https://dockerhub.icu","https://docker.awsl9527.cn"],"insecure-registries": ["localhost:5000","127.0.0.1:5000","192.168.139.136:5000"],"exec-opts": ["native.cgroupdriver=systemd"],"log-driver": "json-file","log-opts": {"max-size": "100m"},"storage-driver": "overlay2"
}
EOF
  • 使配置生效并重启 Docker(修改 Docker 配置无需执行 systemctl daemon-reload):

sudo systemctl restart docker
sudo systemctl enable docker
# 验证配置是否生效
docker info | grep -A 10 'Registry Mirrors'
docker info | grep -A 5 'Insecure Registries'
  • 检查服务与容器状态:

systemctl status docker
docker ps -a
# 如未运行,启动 Registry
docker start docker-registry

说明:添加 insecure-registries​ 后,Docker 允许向该地址进行 HTTP​ 推送/拉取;生产环境建议使用 TLS


测试推送与拉取镜像

  • 标记镜像(注意仓库名与端口要与私有仓库地址一致):

docker tag hello-world localhost:5000/my-hello-world
  • 推送到私有仓库:

docker push localhost:5000/my-hello-world
  • 验证仓库内容:

# 查看仓库中的镜像列表
curl http://127.0.0.1:5000/v2/_catalog
# 期望输出:{"repositories":["my-hello-world"]}
# 查看镜像标签
curl http://127.0.0.1:5000/v2/my-hello-world/tags/list
# 期望输出:{"name":"my-hello-world","tags":["latest"]}
  • 从私有仓库拉取并运行:

docker pull localhost:5000/my-hello-world
docker run --rm localhost:5000/my-hello-world

提示:若在其他主机访问私有仓库,请将 localhost​ 替换为宿主机的 IP​ 或 域名,并确保该地址已加入 insecure-registries


容器常用操作速查

  • 启动与进入:

# 后台启动 Nginx 并挂载数据卷
docker run -d --name myweb -p 80:80 \-v /nginx/data:/usr/share/nginx/html \nginx:latest
# 进入容器
docker exec -it myweb /bin/bash
# 退出容器
# 在容器内执行:exit
  • 常用运维:

docker ps -a
docker logs -f --tail 100 myweb
docker stop|start|restart myweb
docker rm -f myweb

说明:参数 -d​ 后台运行,-p 80:80​ 做端口映射,-v 本地目录:容器目录​ 持久化数据。


安全与维护建议

  • 启用 TLS/HTTPS:为 Registry 配置证书,避免长期使用 insecure-registries;自签名证书需分发到客户端 /etc/docker/certs.d/<域名或IP>:5000/ca.crt​ 并重启 Docker。

  • 启用 身份认证:使用 htpasswd​ 配置基础认证,提升安全性。

  • 定期 垃圾回收:清理未引用层,释放磁盘空间(registry garbage-collect)。

  • 生产可选 Harbor:需要用户管理、权限控制、镜像复制与漏洞扫描时,考虑使用企业级 Harbor

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询