這個時候centos官方已經(jīng)主推centos8了糖驴,但是8的鏡像我下載完已經(jīng)有8.6G了声旺,相對于7的4G多的空間跃捣,再加上企業(yè)目前生產(chǎn)主流用的還是7谱醇,所以我在虛擬機(jī)還是繼續(xù)玩7暇仲。
Docker從1.13版本之后采用時間線的方式作為版本號,分為社區(qū)版CE和企業(yè)版EE副渴。
我這里用社區(qū)版測試
1.查看centos內(nèi)核版本(docker要求centos內(nèi)核版本高于3.10)
[sandwich@192 ~]$ uname -r
3.10.0-957.el7.x86_64
2.使用root權(quán)限更新yum, 確保yum是最新的
[sandwich@192 ~]$ sudo yum update
如果是新用戶奈附,有可能沒有sudo權(quán)限,可以參考我的另外一個文章添加
http://www.reibang.com/p/5ac7980a7955
3.卸載舊版本(如果已經(jīng)安裝過的話)
[sandwich@192 ~]$ sudo yum remove docker docker-common docker-selinux docker-engine
[sudo] password for sandwich:
Loaded plugins: fastestmirror, langpacks
No Match for argument: docker
No Match for argument: docker-common
No Match for argument: docker-selinux
No Match for argument: docker-engine
No Packages marked for removal
因?yàn)槲沂切聶C(jī)煮剧,自然不會有東西卸載
4.安裝依賴包
# yum-util提供yum-config-manager功能斥滤,另外兩個是device mapper驅(qū)動依賴的
[sandwich@192 ~]$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
5.設(shè)置yum源
[sandwich@192 ~]$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
6.查看倉庫中所有的docker版本将鸵,選擇最新版或者特定版本安裝
[sandwich@192 ~]$ yum list docker-ce --showduplicates | sort -r
* updates: mirrors.aliyun.com
Loaded plugins: fastestmirror, langpacks
* extras: mirrors.aliyun.com
docker-ce.x86_64 3:20.10.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.1-3.el7 docker-ce-stable
...
7.安裝docker
# 由于repo中默認(rèn)只開啟stable倉庫,所以這里默認(rèn)安裝最新穩(wěn)定版本
[sandwich@192 ~]$ sudo yum install docker-ce
8.啟動并加入開機(jī)啟動
[sandwich@192 ~]$ sudo systemctl start docker
[sudo] password for sandwich:
[sandwich@192 ~]$ sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
9.驗(yàn)證安裝是否成功(有client和servcie兩部分)
[sandwich@192 ~]$ docker version
Client: Docker Engine - Community
Version: 20.10.10
API version: 1.41
Go version: go1.16.9
Git commit: b485636
Built: Mon Oct 25 07:44:50 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version": dial unix /var/run/docker.sock: connect: permission denied
由上可以看出來client部分沒有問題了佑颇,但是service部分似乎遇到permission的問題顶掉。
再執(zhí)行一下docker ps試試,同樣的問題
[sandwich@192 ~]$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied
出現(xiàn)這個問題的原因是:
docker進(jìn)程使用Unix Socket而不是TCP端口挑胸。而默認(rèn)情況下痒筒,Unix socket屬于root用戶,需要root權(quán)限才能訪問茬贵。
解決辦法如下:
方案一:使用sudo獲取管理員權(quán)限簿透,運(yùn)行docker命令
方案二:添加docker group組,將用戶添加進(jìn)去
我先試試方案一:
[sandwich@192 ~]$ sudo docker version
[sudo] password for sandwich:
Client: Docker Engine - Community
Version: 20.10.10
API version: 1.41
Go version: go1.16.9
Git commit: b485636
Built: Mon Oct 25 07:44:50 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.10
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: e2f740d
Built: Mon Oct 25 07:43:13 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.11
GitCommit: 5b46e404f6b9f661a205e28d59c982d3634148f8
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[sandwich@192 ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
這樣看起來沒有問題了解藻,但是我就不信docker version和docker ps都需要加上sudo.
再試試方案二:
#添加docker用戶組
[sandwich@192 ~]$ sudo groupadd docker
groupadd: group 'docker' already exists
#將當(dāng)前登錄用戶加入docker用戶組
[sandwich@192 ~]$ sudo gpasswd -a $USER docker
Adding user sandwich to group docker
#更新用戶組
[sandwich@192 ~]$ newgrp docker
#測試當(dāng)前用戶是否可以正常使用docker命令
[sandwich@192 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
至此完成安裝老充。