Docker安裝(使用阿里雲鏡像)
Docker從1.13版本之后采用時間線的方式作為版本號,分為社區版CE和企業版EE。
社區版是免費提供給個人開發者和小型團體使用的,企業版會提供額外的收費服務,比如經過官方測試認證過的基礎設施、容器、插件等。
社區版按照stable和edge兩種方式發布,每個季度更新stable版本,如17.06,17.09;每個月份更新edge版本,如17.09,17.10。
- 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-selinux