安装Docker(CentOS 8.5)

Docker(CentOS 8.5)

1.更换yum源

CentOS Linux 8在2022年12月31日来到生命周期终点(End of Life,EoL)。即CentOS Linux 8操作系统版本结束了生命周期(EOL),Linux社区已不再维护该操作系统版本。所以原来的CentOS Linux 8的yum源也都失效了!最终导致此问题的产生。

更换CentOS Linux 8yum

1、切换到源目录,备份原来的源

[root@17-132gui /]# cd /etc/yum.repos.d/
[root@17-132gui yum.repos.d]# ls -l
总用量 48
-rw-r--r--. 1 root root  719 2月  18 14:36 CentOS-Linux-AppStream.repo
-rw-r--r--. 1 root root  704 2月  18 14:36 CentOS-Linux-BaseOS.repo
-rw-r--r--. 1 root root 1130 9月  15 09:11 CentOS-Linux-ContinuousRelease.repo
-rw-r--r--. 1 root root  318 9月  15 09:11 CentOS-Linux-Debuginfo.repo
-rw-r--r--. 1 root root  732 9月  15 09:11 CentOS-Linux-Devel.repo
-rw-r--r--. 1 root root  704 9月  15 09:11 CentOS-Linux-Extras.repo
-rw-r--r--. 1 root root  719 9月  15 09:11 CentOS-Linux-FastTrack.repo
-rw-r--r--. 1 root root  740 9月  15 09:11 CentOS-Linux-HighAvailability.repo
-rw-r--r--. 1 root root  693 9月  15 09:11 CentOS-Linux-Media.repo
-rw-r--r--. 1 root root  706 9月  15 09:11 CentOS-Linux-Plus.repo
-rw-r--r--. 1 root root  724 9月  15 09:11 CentOS-Linux-PowerTools.repo
-rw-r--r--. 1 root root 1124 9月  15 09:11 CentOS-Linux-Sources.repo
[root@17-132gui yum.repos.d]# mkdir bak
[root@17-132gui yum.repos.d]# mv CentOS-Linux-* bak
[root@17-132gui yum.repos.d]# ls
bak

2、下载新的源文件,并用命令yum makecache建立新的元数据缓存

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@17-132gui yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
--2022-02-18 15:12:16--  https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 124.95.160.238, 124.95.160.244, 124.95.160.243, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|124.95.160.238|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2495 (2.4K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”

/etc/yum.repos.d/CentOS-B 100%[===================================>]   2.44K  --.-KB/s  用时 0s      

2022-02-18 15:12:16 (21.2 MB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2495/2495])

[root@17-132gui yum.repos.d]# ls
bak  CentOS-Base.repo
[root@17-132gui yum.repos.d]# vim CentOS-Base.repo 
[root@17-132gui yum.repos.d]# yum makecache
CentOS-8.5.2111 - Base - mirrors.aliyun.com                           1.2 MB/s | 4.6 MB     00:03    
CentOS-8.5.2111 - Extras - mirrors.aliyun.com                          37 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com                      1.1 MB/s | 8.4 MB     00:07    
元数据缓存已建立。
[root@17-132gui yum.repos.d]# 

yum源已经可以正常使用了

2.安装 Docker Engine-Community

设置仓库

安装所需的软件包。yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

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

使用以下命令来设置稳定的仓库,以阿里云源地址为例:

sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3.安装 Docker Engine-Community

执行顺序 2--->3--->1

1.安装最新版本的 Docker Engine-Community 和 containerd

sudo yum install docker-ce docker-ce-cli containerd.io

此时会安装失败,报错如下:

package docker-ce-3:19.03.8-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the provide

2.问题分析:centos8默认使用podman代替docker,所以需要安装containerd.io

解决方法:安装containerd.io即可

yum install https://download.docker.com/linux/fedora/30/x86_64/stable/Packages/containerd.io-1.2.6-3.3.fc30.x86_64.rpm

此时可能会出现报错:

package podman-3.0.1-7.module_el8.4.0 830 8027e1c4.x86_64 requires runc >= 1

3.问题分析:podman包冲突

解决方法:解决冲突

yum erase podman buildah

4.启动 Docker

 sudo systemctl start docker

5.验证

通过运行 hello-world 映像来验证是否正确安装了 Docker Engine-Community

sudo docker run hello-world

若输出下列文本即Docker安装成功,可正常使用

[root@xjx-centos-linux-8 xjx]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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.
    (amd64)
 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/

[root@xjx-centos-linux-8 xjx]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 months ago   13.3kB
[root@xjx-centos-linux-8 xjx]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@xjx-centos-linux-8 xjx]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                          PORTS     NAMES
2fbaf8a697f9   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             nervous_goldberg

6.添加用户组

通过socket去连接docker时权限不被允许。
这是应为socket需要root用户才能访问

1.切换成root用户操作。这是最直接的方法。切换命令:

su root

2.添加docker的用户组,把当前用户加入组中。

sudo groupadd docker               #添加用户组
sudo gpasswd -a 当前用户 docker    #将当前用户添加至用户组
newgrp docker                  #更新用户组

7.服务自启动

docker 服务开机自启动命令

systemctl enable docker.service 

关闭docker 服务开机自启动命令

systemctl disable docker.service

2.Docker Compose(失败了还是上传使用吧)

安装docker-compose

1.执行

# 安装
sudo curl -L https://github.com/docker/compose/releases/download/v2.18.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/v2.18.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

这里报错的话,更改本机的 DNS 设置

将 nameserver 改为 114.114.114.114 (如果需要访问的域名多在中国大陆,建议改为此 DNS,这个 DNS 是中国电信提供的免费 DNS Server,解析国内域名相对较快)或者 8.8.8.8 (如果需要访问的域名多在海外,建议改成 8.8.8.8,这个 DNS 是 Google 提供的供全球免费使用的 DNS Server)

如下示例:

sudo vim /etc/resolv.conf

或修改自己的hosts文件:

echo "192.168.17.140 github.com" >> /etc/hosts

2 .赋予执行权限

sudo chmod +x /usr/local/bin/docker-compose

3 .创建软链接

sudo rm -r /usr/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

4 .验证docker-compose是否安装成功

docker-compose -version

3.Base自动补全命令

补全命令

curl -L https://raw.githubusercontent.com/docker/compose/v2.18.1/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose

如果这里出现错误,需要修改自己的hosts文件:

sudo echo "192.168.17.140 raw.githubusercontent.com" >> /etc/hosts
posted @ 2024-06-25 18:19  3088577529  阅读(147)  评论(0)    收藏  举报