首页 » Docker » 正文

CentOS 7 安装 Docker-CE 并配置阿里云加速器

环境:CentOS 7.4 64bit

一、安装

官方安装文档:https://docs.docker.com/install/linux/docker-ce/centos/

 

1. 卸载机器上的老版本

yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

如果你机器上有安装过 Docker 的话, /var/lib/docker/ 目录会被保留下来。

 

2. 安装 Docker

有两种安装方式:一种是yum安装,另一种是下载软件包安装,这里只介绍yum安装方式。

1) 安装所需软件包

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

2) 配置稳定的仓库

官方:

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

国内推荐使用:

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

 

3) 安装 Docker

yum install docker-ce docker-ce-cli containerd.io

4) 启动 Docker

启动:systemctl start docker
停止:systemctl stop docker
重启:systemctl restart docker
开机启动:systemctl enable docker

5) 测试是否安装成功

a. 通过 docker version 查看docker版本信息

[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.3
 API version:       1.40
 Go version:        go1.12.10
 Git commit:        a872fc2f86
 Built:             Tue Oct  8 00:58:10 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.3
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.10
  Git commit:       a872fc2f86
  Built:            Tue Oct  8 00:56:46 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

 

b. 通过运行 Docker 测试容器 hello-world 查看: docker run hello-world

[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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/

成功。

 

二、配置阿里云加速器

根据默认的配置,我们获取镜像都是要到docker官方的镜像仓库去拉取,由于国内网络原因,获取镜像速度很慢,所以这里我们配置阿里云加速器。

1. 登录阿里云控制台,找到“容器镜像服务”,首次进去会提示你设置Registry登录密码,按照要求设置即可;

2. 完成之后依次点击 “镜像中心-镜像加速器” 即可查看到你的加速器地址,以及在不同操作系统上的配置方法;

3. 在CentOS上配置加速器:

3.1 创建所需目录:

mkdir -p /etc/docker

3.2 创建文件 /etc/docker/daemon.json ,内容如下,注意请将里面的地址替换为你自己的加速地址

{
  "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
}

3.3 重启doker

systemctl daemon-reload
systemctl restart docker

3.4 查看是否成功

通过 docker info 即可查看到尾部有 Registry Mirrors .

 

发表评论

验证码加载中....