用国内软件源加速 Docker-CE 安装
本文最后更新于 1810 天前,其中的信息可能已经有所发展或是发生改变。

前言

Docker 已经是作为越来越多环境必备的组件了,所以米饭后面也会有大量的教程要使用到 Docker,所以还是有必要写一篇关于安装 Docker 的教程的。

介绍

Docker 是一个开源的应用容器引擎,基于 Go 语言开发并遵从 Apache2.0 协议。

Docker 可以让开发者将应用以及依赖打包到一个轻量、可移植的容器当中,这样就可以避免依赖和兼容性问题,比如说 .NET 应用就可以不必再安装到 Win Server 中而可以在任一 Linux 发行版中。

容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),实现应用间的有效隔离,特别是在一些应用托管的领域,Docker 大大提升了安全性。

更重要的是容器性能开销极低,像阿里云的 POLARDB 就不再基于一层虚拟化了,而是直接用 Docker 来隔离不同的数据库以实现超高的性能。

安装

很多新手在学习 Docker 的时候,老是因为下载没有速度卡在安装哪里,或者说安装好了拖容器下来的时候又没有速度。这里就介绍如何用国内的软件源和镜像源来加速 Docker 的体验。

注意:

Docker 只能安装在 64bit 系统上

Linux 内核越新体验越好,不建议使用早于 2016 年的 Linux 发行版。

Windows 安装 Docker 不支持二次虚拟,也就是说已经虚拟化过的 Win 就不能再使用 Docker了。

软件源安装

这里使用阿里云的源,需要使用内网源或者其他源的可以替换一下地址。

Debian/Ubuntu

卸载旧版本

apt-get remove docker \
               docker-engine \
               docker.io

安装系统依赖

sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

添加软件源及密钥

add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add -

更新软件源缓存并安装Docker-CE

apt update -y
apt install docker-ce -y

启动并设置开机启动

systemctl enable docker
systemctl start docker

CentOS

卸载旧版本

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

添加软件源

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

更新缓存并安装

yum makecache fast
yum install docker-ce -y

启动并设置开机启动

systemctl enable docker
systemctl start docker

测试 Docker 是否安装正确

没错了,当然是试一下 Hello World 管用不管用了~

$ docker run hello-world

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/

若能正常输出以上信息,则证明安装成功。

镜像加速

尽管我们通过国内软件源安装好了 Docker,但是软件本身下载容器依旧是通过海外线路,这时候我们就可以通过阿里云免费提供的专属加速镜像了。

登陆阿里云账号后进入:https://cr.console.aliyun.com/cn-hangzhou/mirrors

可以如下图,看到我们的专属加速器地址,然后根据操作文档下面,选择不同的操作系统,然后用蓝框里的命令运行一下就可以了!

enter description here

容器服务

如果使用的是阿里云的 ECS,其实可以直接通过阿里云的容器服务直接安装,自动化安装并设置镜像源加速。

进入:https://cs.console.aliyun.com/?.aliyun_sidebar.aliyun_sidebar_cs.#/cluster/createNew

然后如下图,选择添加已有实例

然后红框的地方,勾两个一个不勾即可。 就可以不产生额外成品使用阿里云的容器服务监控我们的 ECS 了!当然了容器服务的一些收费项比如说 SLB、NAT 网关等都是为了集群化 Docker 所提供的。 容器服务本身是免费的。

enter description here

上一篇
下一篇