Centos 7 上安装docker-ce

服务器   发布日期:2025年06月05日   浏览次数:182

到目前为止,docker已经分化为两个主要分支

docker_ee: 企业版(docker enterprise edition)  

docer_ce:  社区版 (docker community edition)

本文主要介绍社区版的安装

配置更新yum源

设置yum源

  1. # 安装yum工具包
    sudo yum install -y yum-utils
    # 添加docker-ce yum源
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

 把yum包更新到最新

  1. sudo yum clean all && sudo yum update

安装依赖的的软件包

  1. sudo yum install -y device-mapper-persistent-data lvm2

安装docker

查看所有仓库中所有docker版本,并选择特定版本安装

  1. # yum list docker-ce --showduplicates | sort -r
  2. Loading mirror speeds from cached hostfile
  3. Loaded plugins: fastestmirror, langpacks
  4. docker-ce.x86_64 :19.03.-.el7 docker-ce-stable
  5. docker-ce.x86_64 :19.03.-.el7 docker-ce-stable
  6. docker-ce.x86_64 :19.03.-.el7 docker-ce-stable
  7. docker-ce.x86_64 :19.03.-.el7 docker-ce-stable
  8. docker-ce.x86_64 :19.03.-.el7 docker-ce-stable
  9. docker-ce.x86_64 :19.03.-.el7 docker-ce-stable
  10. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  11. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  12. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  13. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  14. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  15. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  16. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  17. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  18. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  19. docker-ce.x86_64 :18.09.-.el7 docker-ce-stable
  20. docker-ce.x86_64 18.06..ce-.el7 docker-ce-stable
  21. docker-ce.x86_64 18.06..ce-.el7 docker-ce-stable
  22. docker-ce.x86_64 18.06..ce-.el7 docker-ce-stable
  23. docker-ce.x86_64 18.06..ce-.el7 docker-ce-stable
  24. docker-ce.x86_64 18.03..ce-.el7.centos docker-ce-stable
  25. docker-ce.x86_64 18.03..ce-.el7.centos docker-ce-stable
  26. docker-ce.x86_64 17.12..ce-.el7.centos docker-ce-stable
  27. docker-ce.x86_64 17.12..ce-.el7.centos docker-ce-stable
  28. docker-ce.x86_64 17.09..ce-.el7.centos docker-ce-stable
  29. docker-ce.x86_64 17.09..ce-.el7.centos docker-ce-stable
  30. docker-ce.x86_64 17.06..ce-.el7.centos docker-ce-stable
  31. docker-ce.x86_64 17.06..ce-.el7.centos docker-ce-stable
  32. docker-ce.x86_64 17.06..ce-.el7.centos docker-ce-stable
  33. docker-ce.x86_64 17.03..ce-.el7 docker-ce-stable
  34. docker-ce.x86_64 17.03..ce-.el7.centos docker-ce-stable
  35. docker-ce.x86_64 17.03..ce-.el7.centos docker-ce-stable
  36. docker-ce.x86_64 17.03..ce-.el7.centos docker-ce-stable

安装Docker,命令:yum install docker-ce-版本号,我选的是19.03.5-3.el7,如下

  1. sudo yum install -y docker-ce-19.03.-.el7

 启动Docker,命令:systemctl start docker,然后加入开机启动,如下

  1. sudo systemctl start docker
  2. sudo systemctl enable docker

验证是否安装成功 docker version

# sudo docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:25:41 2019
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:24:18 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683

设置docker

使用Docker 中国加速器

由于网络原因,我们在pull Image 的时候,从Docker Hub上下载会很慢。

国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务,
这里的镜像源有多种选择,阿里yun的镜像,华为云的镜像等等
例如:
科大镜像:https://docker.mirrors.ustc.edu.cn/
网易:https://hub-mirror.c.163.com/
阿里云:https://<你的ID>.mirror.aliyuncs.com
七牛云加速器:https://reg-mirror.qiniu.com

如果有私有docker仓库,非https协议的,需要配置在非安全registry

  1. sudo vi /etc/docker/daemon.json
  1. #添加后:
  1. {
  1. "registry-mirrors": ["https://registry.docker-cn.com"],
  2. "live-restore": true,
    "insecure-registries":["http://192.xx.xx.xx:your_docker_server_port"]
  1. }

 

重启docker服务

  1. sudo systemctl restart docker

 

运行docker hello-word 镜像

  1. $ sudo docker run hello-world

如果屏幕上打印如下结构,表示安装成功

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
4ee5c797bcd7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
Status: Downloaded newer image for hello-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 the "hello-world" image from the Docker Hub.
(arm32v7)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

 

设置免 sudo 使用 docker

以下设置完成后,需要注销再登录才可生效.

  1. # 首先查看docer 组是否存在
  2. #如果不存在需要创建docker 组
  3. $getent group docker
  4. docker:x::philoenglish
  5. #然后将当前用户添加到docker和 sudo 组
  6. sudo usermod -a -G docker $USER
  1. sudo usermod -a -G sudo $USER
  1. ## 或者
    gpasswd -M $USER docker
  1. gpasswd -M $USER sudo

  1. ## 要从组中删除用户,请使用带有 -d 选项的 gpasswd 命令以及用户和组的名称。
    gpasswd -d $USER docker

 

安装过程中可能会出错,或需要卸载安装新版本,这时候可以参考以下步骤

  1. sudo docker stop $(docker ps -q)
  2. sudo docker rm $(docker ps -qa)
  3. sudo docker rmi $(docker images -qa)
  4. systemctl stop docker
  5. ## 查看已安装的版本
  6. yum list installed|grep docker
  7. # 卸载
  8. yum remove -y containerd.io.x86_64 docker-ce.x86_64 remove docker-ce-cli.x86_64
  9. #谨慎删除残留数据,如果不清楚这条命令有何影响,最好不要做,尤其是生产环境
  10. rm -rf /var/lib/docker

 

reference documents:

https://www.cnblogs.com/wyt007/p/11154156.html

https://www.jianshu.com/p/00e162bf587a

以上就是Centos 7 上安装docker-ce的详细内容,更多关于Centos 7 上安装docker-ce的资料请关注九品源码其它相关文章!

上一篇:SOCKS5一键安装脚本
下一篇:centos7安装jdk