版权声明:本文为黄海兵(haibing.org)原创文章,转载请附上本文链接。
本文永久链接:http://haibing.org/756

前面有一篇讲了在 CentOS 6上安装 docker-io/docker-engine

概述
CentOS6上虽然还可以安装Docker,但是不推荐在CentOS6上进行安装,CentOS6也没办法安装最新版本的docker-ce版本。
如果要学习和在生产中使用Docker,推荐在CentOS7上安装docker-ce版本。
转载本站文章请注明出处:黄海兵haibing.org

一、在CentOS7上安装docker-ce
在CentOS7上安装Docker还是比较简单的。

CentOS本身就有提供一个docker版本可以安装,但版本较旧,不推荐使用。
# yum list|grep docker

docker.x86_64                  1.13.1-102.git7f2769b.el7.centos

要想安装最新版的Docker版本,还是需要从Docker官网下载repo文件来进行安装。
把repo文件下载到/etc/yum.repos.d目录下。
转载本站文章请注明出处:黄海兵haibing.org

#cd /etc/yum.repos.d
#wget https://download.docker.com/linux/centos/docker-ce.repo

开始安装:
# yum install docker-ce

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: hkg.mirror.rackspace.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:19.03.2-3.el7 will be installed
--> Processing Dependency: containerd.io >= 1.2.2-3 for package: 3:docker-ce-19.03.2-3.el7.x86_64
--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-19.03.2-3.el7.x86_64
--> Running transaction check
---> Package containerd.io.x86_64 0:1.2.6-3.3.el7 will be installed
---> Package docker-ce-cli.x86_64 1:19.03.2-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================
 Package                    Arch                Version                      Repository                     Size
=================================================================================================================
Installing:
 docker-ce                  x86_64              3:19.03.2-3.el7              docker-ce-stable               24 M
Installing for dependencies:
 containerd.io              x86_64              1.2.6-3.3.el7                docker-ce-stable               26 M
 docker-ce-cli              x86_64              1:19.03.2-3.el7              docker-ce-stable               39 M

Transaction Summary
=================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 90 M
Installed size: 368 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-19.03.2-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-19.03.2-3.el7.x86_64.rpm is not installed
(1/3): docker-ce-19.03.2-3.el7.x86_64.rpm                                                 |  24 MB  00:00:04     
(2/3): containerd.io-1.2.6-3.3.el7.x86_64.rpm                                             |  26 MB  00:00:04     
(3/3): docker-ce-cli-19.03.2-3.el7.x86_64.rpm                                             |  39 MB  00:00:04     
-----------------------------------------------------------------------------------------------------------------
Total                                                                             11 MB/s |  90 MB  00:00:08     
Warning: RPMDB altered outside of yum.
  Installing : 1:docker-ce-cli-19.03.2-3.el7.x86_64                                                          1/3 
  Installing : containerd.io-1.2.6-3.3.el7.x86_64                                                            2/3 
  Installing : 3:docker-ce-19.03.2-3.el7.x86_64                                                              3/3 
  Verifying  : 3:docker-ce-19.03.2-3.el7.x86_64                                                              1/3 
  Verifying  : containerd.io-1.2.6-3.3.el7.x86_64                                                            2/3 
  Verifying  : 1:docker-ce-cli-19.03.2-3.el7.x86_64                                                          3/3 

Installed:
  docker-ce.x86_64 3:19.03.2-3.el7                                                                               

Dependency Installed:
  containerd.io.x86_64 0:1.2.6-3.3.el7                    docker-ce-cli.x86_64 1:19.03.2-3.el7                   

Complete!


从上面可以看出,安装了三个软件docker-ce、containerd.io和docker-ce-cli。

注意:安装完后最好把docker默认的保存地址/var/lib/docker单独存在一个分区,并且这个分区格式化时,要带上参数ftype=1。
# mkfs -t xfs -n ftype=1 /dev/sdx
或者修改默认配置,指向其它已经使用ftype=1格式过的分区。
详细原因见我另一篇文章:docker中rm: cannot remove ‘/xxx/xxxxx’: Directory not empty
转载本站文章请注明出处:黄海兵haibing.org

启动Docker服务
# systemctl restart docker

# systemctl status docker

¡ñ docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-09-20 22:52:08 CST; 5s ago
     Docs: https://docs.docker.com
 Main PID: 7222 (dockerd)
   Memory: 46.2M
   CGroup: /system.slice/docker.service
           ©¸©¤7222 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
转载本站文章请注明出处:黄海兵haibing.org
Sep 20 22:52:08 logserver dockerd[7222]: time="2019-09-20T22:52:08.124251525+08:00" level=info msg="[grap...lay2"
Sep 20 22:52:08 logserver dockerd[7222]: time="2019-09-20T22:52:08.133805844+08:00" level=warning msg="mo...ound"
Sep 20 22:52:08 logserver dockerd[7222]: time="2019-09-20T22:52:08.134250040+08:00" level=info msg="Loadi...art."
Sep 20 22:52:08 logserver dockerd[7222]: time="2019-09-20T22:52:08.376128471+08:00" level=info msg="Defau...ress"
Sep 20 22:52:08 logserver dockerd[7222]: time="2019-09-20T22:52:08.465507266+08:00" level=info msg="Loadi...one."
Sep 20 22:52:08 logserver dockerd[7222]: time="2019-09-20T22:52:08.525920859+08:00" level=warning msg="No...rlay2
Sep 20 22:52:08 logserver dockerd[7222]: time="2019-09-20T22:52:08.526507025+08:00" level=info msg="Docke....03.2
Sep 20 22:52:08 logserver dockerd[7222]: time="2019-09-20T22:52:08.526657094+08:00" level=info msg="Daemo...tion"
Sep 20 22:52:08 logserver systemd[1]: Started Docker Application Container Engine.
Sep 20 22:52:08 logserver dockerd[7222]: time="2019-09-20T22:52:08.580220493+08:00" level=info msg="API l...sock"
Hint: Some lines were ellipsized, use -l to show in full.

二、Docker的基本操作
1、查看当前Docker的版本信息
# docker version

Client: Docker Engine - Community
 Version:           19.03.2
 API version:       1.40
 Go version:        go1.12.8
 Git commit:        6a30dfc
 Built:             Thu Aug 29 05:28:55 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.2
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.8
  Git commit:       6a30dfc
  Built:            Thu Aug 29 05:27:34 2019
  OS/Arch:          linux/amd64
  Experimental:     false

可以看出,Docker客户端和服务端的版本都是19.03.2,API版本是1.40,go语言版本是1.12.8。
转载本站文章请注明出处:黄海兵haibing.org

2、搜索可用的镜像
使用docker search搜索查寻某个镜像
# docker search hello-world

NAME                                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   1033                [OK]                
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   132                                     
tutum/hello-world                          Image to test docker deployments. Has Apache…   63                                      [OK]
dockercloud/hello-world                    Hello World!                                    15                                      [OK]
crccheck/hello-world                       Hello World web server in under 2.5 MB          10                                      [OK]
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2                                       
vad1mo/hello-world-rest                    A simple REST Service that echoes back all t…   2                                       [OK]
souravpatnaik/hello-world-go               hello-world in Golang                           1                                       
carinamarina/hello-world-app               This is a sample Python web application, run…   1                                       [OK]
markmnei/hello-world-java-docker           Hello-World-Java-docker                         1                                       [OK]
kevindockercompany/hello-world                                                             0                                       
burdz/hello-world-k8s                      To provide a simple webserver that can have …   0                                       [OK]
koudaiii/hello-world                                                                       0                                       
freddiedevops/hello-world-spring-boot                                                      0                                       
datawire/hello-world                       Hello World! Simple Hello World implementati…   0                                       [OK]
infrastructureascode/hello-world           A tiny "Hello World" web server with a healt…   0                                       [OK]

第一列:镜像的名称
第二列:镜像的描述
第三列:STARS数(相当于点赞数,收藏数),数值越高,这个镜像越好
第四列:是否是官方的

可以看出第一个stars数最多的是hello-world镜像是官方的。

3、下载一个镜像到本地
把hello-world这个镜像下载到本地
# docker pull hello-world

Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:b8ba256769a0ac28dd126d584e0a2011cd2877f3f76e093a7ae560f2a5301c00
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
转载本站文章请注明出处:黄海兵haibing.org

4、查看当前本地上的镜像
# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        8 months ago        1.84kB

5、运行一个镜像
# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.
转载本站文章请注明出处:黄海兵haibing.org
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/