1. 程式人生 > >Docker在centos下安裝以及常見錯誤解決

Docker在centos下安裝以及常見錯誤解決

Docker安裝(使用阿里雲映象)

Docker從1.13版本之後採用時間線的方式作為版本號,分為社群版CE和企業版EE。

社群版是免費提供給個人開發者和小型團體使用的,企業版會提供額外的收費服務,比如經過官方測試認證過的基礎設施、容器、外掛等。

社群版按照stable和edge兩種方式釋出,每個季度更新stable版本,如17.06,17.09;每個月份更新edge版本,如17.09,17.10。

1) Docker 要求 CentOS 系統的核心版本高於 3.10 ,檢視本頁面的前提條件來驗證你的CentOS 版本是否支援 Docker 。

通過 uname -r 命令檢視你當前的核心版本

uname -r

需要注意的是以下命令均是在root賬戶下執行的,如果是普通賬戶有些命令則可能需要加上sudo

2)執行 yum update

yum update非必選項,但是建議執行操作

3)解除安裝舊版本docker

yum -y  remove docker  docker-common docker-selinux docker-engine

3)設定yum源



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源

需要注意的是,目前(2019年3月)測試的時候,docker不設定阿里雲映象源也是能正常安裝的,並且阿里雲映象源地址也可能會更改,所以以上地址可能會返回404,如果以上映象源地址不存在也可以忽略這一步直接進行下一步.

4)檢視所有倉庫中所有docker版本,並選擇特定版本安裝

yum list docker-ce --showduplicates | sort -r

5)安裝docker

yum install -y docker-ce  #由於repo中預設只開啟stable倉庫,故這裡安裝的是最新穩定版17.12.0
yum install  -y <FQPN>  # 例如:sudo yum install docker-ce-17.12.0.ce

6)啟動docker並加入開機自動啟動

systemctl start docker
systemctl enable docker

7)配置阿里雲映象加速

mkdir  -p  /etc/docker
vi  /etc/docker/daemon.json

新增以下內容

{"registry-mirrors": 
["https://5f2jam6c.mirror.aliyuncs.com", 
"http://hub-mirror.c.163.com"]
}

8)重新載入配置檔案

systemctl reload  docker

9)重啟docker

systemctl restart docker

安裝過程中可能會出現的問題:

1.正在處理依賴關係 docker-ce-selinux >= 17.03.0.ce-1.el7.centos,它被軟體包 docker-ce-17.03.0.ce-1.el7.centos.x86_64 需要
軟體包 docker-ce-selinux 已經被 docker-ce-cli 取代,但是取代的軟體包並未滿足需求”
等一大串的問題

這時我們需要通過 yum install 安裝一個rpm包

通過這個地址我們檢視和我們安裝docker版本一直的rpm包
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

通過

yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm

問題解決

2.非root使用者使用docker命令報錯

以上我們為了方便在安裝過程中使用的是root賬戶,然而實際操作中我們可能更多的是用普通使用者操作docker命令.從root使用者切到普通使用者執行docker命令時會報如下錯誤

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.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied

官方解釋如下:

The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the root user. 
To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
  • 建立docker使用者組
sudo groupadd docker

此步驟並非必須,預設情況下docker安裝過程中會自動建立一個docker使用者組

  • 將當前使用者加入docker使用者組
sudo gpasswd -a ${USER} docker

注意以上命令只能用當前使用者來操作,因為變數USER獲取的是當前使用者,如果要以root使用者來操作,則需要指定的是具體的使用者名稱,而非此變數

  • 當前使用者退出系統重新登陸

這裡並不是批退出系統,只要切換一下使用者即可,比如先切到root使用者然後再切回來就可以了

3.安裝docker時報container-selinux >= 2.9錯誤

由於測試環境的機器是不同批准到來的,第一批安裝非常順序,第二批是直接放置在辦公室的PC機,也非常不順序,安裝過程遇到各種各樣麻煩.

上面說過,加入叢集時發現docker沒有安裝成功,安裝docker時出現了以下錯誤

Error: Package: docker-ce-18.03.1.ce-1.el7.centos.x86_64 (docker-ce-edge)
Requires: container-selinux >= 2.9
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
這個報錯是container-selinux版本低或者是沒安裝的原因

yum 安裝container-selinux 一般的yum源又找不到這個包

需要安裝epel源 才能yum安裝container-selinux

然後在安裝docker-ce就可以了。

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

yum install epel-release #阿里雲上的epel源

yum makecache

然後yum install container-seli