環(huán)境檢查
進入 root
為了愉快,以下所有操作都是以 root 用戶執(zhí)行
sudo -i
查看 Linux 發(fā)行版本
[root@localhost ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
查看內(nèi)核版本
Docker 要求 CentOS 系統(tǒng)的內(nèi)核版本高于 3.10
[root@localhost /]# uname -r
3.10.0-123.el7.x86_64
結(jié)論
發(fā)行版本為 CentOS 7雏节,滿足 Docker 環(huán)境需求,接下來用 yum 安裝 docker-ce。
注意:
3.10 版本的內(nèi)核喧伞,可能無法正常運行
18.06.x
及以上版本的 docker(解決方法:升級內(nèi)核或者降低 docker 版本),可能報下面的錯誤:docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "process_linux.go:293: copying bootstrap data to pipe caused "write init-p: broken pipe"": unknown.
安裝 Docker
# 更新到最新 yum 包
yum update -y
# 卸載舊版本(如果安裝過舊版本的話)
yum remove docker docker-common docker-selinux docker-engine docer-io
# 安裝需要的軟件包
# yum-util 提供 yum-config-manager 功能, 另外兩個是 devicemapper 驅(qū)動依賴
yum install -y yum-utils device-mapper-persistent-data lvm2
# 設(shè)置 yum 源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 查看所有倉庫中所有 docker 版本等舔,并選擇特定版本安裝
yum list docker-ce --showduplicates | sort -r
* updates: centos.ustc.edu.cn
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks* extras: centos.ustc.edu.cn
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable* base: centos.ustc.edu.cn
Available Packages
# 由于 repo 中默認只開啟 stable 的倉庫牺荠,故這里安裝的是最新穩(wěn)定版(18.09.2)
# 由于內(nèi)核是 3.10 無法正常運行 18.06.x 及以上版本的 docker翁巍,所以不這么安裝
# yum install -y docker-ce
# 經(jīng)過測試發(fā)現(xiàn),3.10 內(nèi)核可以運行 18.03.1.ce
# yum install -y <FQPN>
yum install -y docker-ce-18.03.1.ce
# 啟動并加入開機啟動
systemctl start docker
systemctl enable docker
# 驗證安裝是否成功(有 client 和 service 兩部分表示 docker 安裝啟動都成功了)
docker version
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:20:16 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarmServer:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:23:58 2018
OS/Arch: linux/amd64
Experimental: false
更改配置(如果有必要)
sudo vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --graph=/home/docker/data
# 你可能會用到代理(如果原先沒有Environment休雌,可以添加)
Environment="HTTP_PROXY=http://0.0.0.0:8123" "HTTPS_PROXY=http://0.0.0.0:8123" "NO_PROXY=localhost,127.0.0.1,::1"
重啟 docker 使配置生效
sudo systemctl daemon-reload
sudo systemctl restart docker
安裝 Docker-Compose
通過訪問 https://github.com/docker/compose/releases/latest 得到最新的 docker-compose 版本(例如:1.23.2)灶壶,然后執(zhí)行一下命令安裝 docker-compose
# 下載最新版本的 docker-compose 到 /usr/bin 目錄下
curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose
# 給 docker-compose 授權(quán)
chmod +x /usr/bin/docker-compose
FAQ
1、yum update -y
失敗
[root@localhost ~]# yum update -y
Loaded plugins: fastestmirror, langpacks
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
One of the configured repositories failed (Unknown),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
1. Contact the upstream for the repository and get them to fix the problem.
2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).
3. Disable the repository, so yum won't use it by default. Yum will then
just ignore the repository until you permanently enable it again or use
--enablerepo for temporary usage:
yum-config-manager --disable <repoid>
4. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:
yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
Cannot find a valid baseurl for repo: base/7/x86_64
如果遇到上面的錯誤杈曲,通過修改 /etc/resolv.conf
驰凛,添加如下內(nèi)容,再次執(zhí)行 yum update -y
nameserver 8.8.8.8
search localdomain
2鱼蝉、yum install -y docker-ce-18.03.1.ce
失敗
Transaction check error:
file /usr/bin/docker from install of docker-ce-18.03.1.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
file /usr/bin/docker-containerd from install of docker-ce-18.03.1.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
file /usr/bin/docker-containerd-shim from install of docker-ce-18.03.1.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
file /usr/bin/dockerd from install of docker-ce-18.03.1.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
卸載舊版本的包
yum erase docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
再次安裝 docker
yum install -y docker-ce-18.03.1.ce