CentOS7 安装及配置 Docker

目录[-]


一、Docker 简介

       Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux 或 Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

二、安装前检查环境

1、升级内核版本

Docker 要求 CentOS 系统的内核版本高于等于 3.10,所以我们确保能顺利安装,这里更新下内核版本。

#备份 yum 源
$ mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak 

#使用 aliyun yum 源
$ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

#清理缓存
$ yum clean all

#更新 yum
$ yum -y update

2、清理之前 Docker 版本

检测是否存在 Docker 相关包,如果存在则删除,否则可能造成冲突。

检测是否存在 Docker 相关包

$ rpm -qa|grep docker

如果有存在的 Docker 相关包就执行下面命令进行删除:

yum remove docker \
          docker-client \
          docker-client-latest \
          docker-common \
          docker-latest \
          docker-latest-logrotate \
          docker-logrotate \
          docker-selinux \
          docker-engine-selinux \
          docker-ce-cli \
          docker-engine

三、配置镜像源

安装 yum 工具,方便下一步配置 yum 源:

$ yum install -y yum-utils device-mapper-persistent-data lvm2

这里使用 Aliyun Docker 源文件进行安装 Docker,输入下面命令进行源配置:

$ yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

四、安装 Docker

显示docker-ce所有可安装版本:

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

Installed Packages
 * extras: mirrors.aliyun.com
 * epel: mirrors.yun-idc.com
docker-ce.x86_64            3:18.09.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.3-3.el7                    docker-ce-stable 
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版本

$ yum install docker-ce-18.06.3.ce-3.el7 -y

五、设置 Docker 存储位置

Docker 默认存储位置为”/var/lib/docker”,但是有时候这个目录下存储空间小需要修改到一个大的存储目录,例如这里设置一个就可以修改 Docker 配置文件 docker.service 进行下面操作

$ vi /lib/systemd/system/docker.service

找到 ExecStartExecStart=/usr/bin/docker 这行,在后台添加 “–graph /apps/docker”

...
ExecStart=/usr/bin/docker --graph /apps/docker
...

六、配置加速器

默认从 “https://hub.docker.com/" 下载镜像,由于网络原因经常超时或者下载速度慢,在国内一般需要给 Docker 设置镜像加速器来加快共有镜像的下载速度,这里创建一个 Docker 配置文件,配置一个下载代理加速地址。

#创建Docker配置文件夹
$ sudo mkdir -p /etc/docker

#添加加速器配置文件
$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://registry.docker-cn.com"] 
}
EOF

七、验证是否安装成功

拉取一个 hello-world 镜像来确认docker可用,执行以下命令:

# docker run hello-world

如果出现以下信息,则代表 docker 可用:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete 
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

八、安装中遇到的问题

  • 问题:
Cannot retrieve metalink for repository: epel. Please verify its path and try again
  • 解决办法:

打开/etc/yum.repos.d/epel.repo

$ vi /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch

修改为:

[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch

再清理源,重新安装

#清理缓存
$ yum clean all

#重新安装包
$ yum install -y 包名

–END–

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值