到目前为止,docker已经分化为两个主要分支

docker_ee: 企业版(docker enterprise edition)  

docer_ce:  社区版 (docker community edition)

本文主要介绍社区版的安装

配置更新yum源

设置yum源

# 安装yum工具包
sudo
yum install -y yum-utils
# 添加docker-ce yum源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

 把yum包更新到最新

sudo yum clean all && sudo yum update

安装依赖的的软件包

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

安装docker

查看所有仓库中所有docker版本,并选择特定版本安装

# yum list docker-ce --showduplicates | sort -r
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                     docker-ce-stable
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-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

安装Docker,命令:yum install docker-ce-版本号,我选的是19.03.5-3.el7,如下

sudo yum install -y docker-ce-19.03.5-3.el7

 启动Docker,命令:systemctl start docker,然后加入开机启动,如下

sudo systemctl start docker
sudo systemctl enable docker

验证是否安装成功 docker version

# sudo docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:25:41 2019
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:24:18 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

设置docker

使用Docker 中国加速器

由于网络原因,我们在pull Image 的时候,从Docker Hub上下载会很慢。

国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务,
这里的镜像源有多种选择,阿里yun的镜像,华为云的镜像等等
例如:
科大镜像:https://docker.mirrors.ustc.edu.cn/
网易:https://hub-mirror.c.163.com/
阿里云:https://<你的ID>.mirror.aliyuncs.com
七牛云加速器:https://reg-mirror.qiniu.com

如果有私有docker仓库,非https协议的,需要配置在非安全registry

sudo vi /etc/docker/daemon.json
#添加后:
{
   "registry-mirrors": ["https://registry.docker-cn.com"],
   "live-restore": true,
"insecure-registries":["http://192.xx.xx.xx:your_docker_server_port"]
}

 

重启docker服务

sudo systemctl restart docker

 

运行docker hello-word 镜像

$ sudo docker run hello-world

如果屏幕上打印如下结构,表示安装成功

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
4ee5c797bcd7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
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.
(arm32v7)
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/

 

设置免 sudo 使用 docker

以下设置完成后,需要注销再登录才可生效.

# 首先查看docer 组是否存在
#如果不存在需要创建docker 组
$getent group docker
docker:x:989:philoenglish


#然后将当前用户添加到docker和 sudo 组
sudo usermod -a -G docker $USER
sudo usermod -a -G sudo $USER
## 或者 
gpasswd
-M $USER docker
gpasswd -M $USER sudo

## 要从组中删除用户,请使用带有
-d 选项的 gpasswd 命令以及用户和组的名称。
gpasswd
-d $USER docker

 

安装过程中可能会出错,或需要卸载安装新版本,这时候可以参考以下步骤

sudo docker stop $(docker ps -q)
sudo docker rm $(docker ps -qa)
sudo docker rmi $(docker images -qa)
systemctl stop docker
## 查看已安装的版本
yum list installed|grep docker
# 卸载
yum remove -y containerd.io.x86_64 docker-ce.x86_64 remove docker-ce-cli.x86_64
#谨慎删除残留数据,如果不清楚这条命令有何影响,最好不要做,尤其是生产环境
rm -rf /var/lib/docker

 

reference documents:

https://www.cnblogs.com/wyt007/p/11154156.html

https://www.jianshu.com/p/00e162bf587a