CentOS 8 安装docker

参考 https://juejin.im/post/5e3032575188252c6e182a55

软件更新

把相关软件都更新

 yum update
卸载旧版本
yum remove docker 
                  docker-client 
                  docker-client-latest 
                  docker-common 
                  docker-latest 
                  docker-latest-logrotate 
                  docker-logrotate 
                  docker-engine

安装yum-utils软件包(提供yum-config-manager 实用程序)并设置稳定的存储库。

yum install -y yum-utils

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #阿里云yum源

yum-config-manager 
    --add-repo 
    https://download.docker.com/linux/centos/docker-ce.repo

查看Docker版本
yum list docker-ce --showduplicates | sort -r
安装Docker

报错

[root@localhost ~]# yum install docker-ce
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
Repository AppStream is listed more than once in the configuration
上次元数据过期检查:0:00:38 前,执行于 2020年01月28日 星期二 17时02分33秒。
错误:
 问题: package docker-ce-3:19.03.5-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed
  - cannot install the best candidate for the job
  - package containerd.io-1.2.10-3.2.el7.x86_64 is excluded
  - package containerd.io-1.2.2-3.3.el7.x86_64 is excluded
  - package containerd.io-1.2.2-3.el7.x86_64 is excluded
  - package containerd.io-1.2.4-3.1.el7.x86_64 is excluded
  - package containerd.io-1.2.5-3.1.el7.x86_64 is excluded
  - package containerd.io-1.2.6-3.3.el7.x86_64 is excluded
(尝试添加 '--skip-broken' 来跳过无法安装的软件包 或 '--nobest' 来不只使用最佳选择的软件包)


这个时候需要安装containerd.io,我们可以到这个网站https://download.docker.com/linux/centos/7/x86_64/stable/Packages/,找到最新的去安装

[root@localhost ~]# dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm

继续进行安装

[root@localhost ~]# yum install docker-ce docker-ce-cli
启动Docker,并设置为开机自启
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
启动docker服务远程允许访问

1、编辑服务器上的docker.service文件

vi /usr/lib/systemd/system/docker.service

大约 在 14行 原来的值

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

修改为

ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

2、保存修改退出,重启docker

systemctl daemon-reload

service docker restart

3、测试远程连接是否正常 输出下面的内容 如果出现以下内容则能正常连接:

curl http://localhost:2375/version

{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.9","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"2020-05-15T00:24:05.000000000+00:00","Experimental":"false","GitCommit":"9d988398e7","GoVersion":"go1.13.10","KernelVersion":"4.18.0-147.el8.x86_64","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.2.6","Details":{"GitCommit":"894b81a4b802e4eb2a91d1ce216b8817763c29fb"}},{"Name":"runc","Version":"1.0.0-rc8","Details":{"GitCommit":"425e105d5a03fabd737a126ad93d62a9eeede87f"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.9","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"9d988398e7","GoVersion":"go1.13.10","Os":"linux","Arch":"amd64","KernelVersion":"4.18.0-147.el8.x86_64","BuildTime":"2020-05-15T00:24:05.000000000+00:00"}

4、开放端口

需要将2375端口进行开放才能被远程连接,如果是阿里云主机的话,可以直接登录阿里云去进行开放:

如果是虚拟机 暂时 关闭 防火墙

查看防火墙运行状态

firewall-cmd --state
running

running 表示正在 运行

关闭防火墙命令

systemctl stop firewalld

本机 浏览器上输入 http://192.168.1.98:2375/version

有返回结果,则可以正常使用了

在idea中 正常连接了

防火墙开启 有可能阻止容器间访问 使用下面的设置修改 使防火墙运行状态下可以使容器间正常通讯
#配置docker0服务到受信任连接
[root@localhost ~]# nmcli connection modify docker0 connection.zone trusted
#停止NetworkManager(检测网络、自动连接网络的程序)服务
[root@localhost ~]# systemctl stop NetworkManager.service
#修改docker网络接口为内部区域(永久)
[root@localhost ~]# firewall-cmd --permanent --zone=trusted --change-interface=docker0
success
#启动NetworkManager(检测网络、自动连接网络的程序)服务
[root@localhost ~]# systemctl start NetworkManager.service
#配置docker0服务到受信任连接
[root@localhost ~]# nmcli connection modify docker0 connection.zone trusted
#重启docker服务
[root@localhost ~]# systemctl restart docker.service
原文地址:https://www.cnblogs.com/z_lb/p/12993148.html