CentOS 7 安装 Docker 和 Docker-Compose

环境检查

进入 root

为了愉快,以下所有操作都是以 root 用户执行

sudo -i

查看 Linux 发行版本

[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/"

查看内核版本

Docker 要求 CentOS 系统的内核版本高于 3.10

[root@localhost /]# uname -r
3.10.0-123.el7.x86_64

结论

发行版本为 CentOS 7,满足 Docker 环境需求,接下来用 yum 安装 docker-ce。

注意:

3.10 版本的内核,可能无法正常运行 18.06.x 及以上版本的 docker(解决方法:升级内核或者降低 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 驱动依赖
yum install -y yum-utils device-mapper-persistent-data lvm2

# 设置 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 的仓库,故这里安装的是最新稳定版(18.09.2)
# 由于内核是 3.10 无法正常运行 18.06.x 及以上版本的 docker,所以不这么安装
# yum install -y docker-ce

# 经过测试发现,3.10 内核可以运行 18.03.1.ce
# yum install -y 
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: swarm

Server:
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),然后执行一下命令安装 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 授权
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 

     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=.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

如果遇到上面的错误,通过修改 /etc/resolv.conf,添加如下内容,再次执行 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

你可能感兴趣的:(CentOS 7 安装 Docker 和 Docker-Compose)