(四)Centos7 下安裝 最新版docker

1.基本要求

Docker官方建議將Docker運行在Linux系統上,當然也可以在其他平臺運行,本片博客只介紹在linux系統上的安裝方法。
Docker運行在CentOS7.X 版本以上,本文使用 CentOS 7.7 版本。
Docker需要安裝在64位平臺。
Docker 要求 CentOS 系統的內核版本高於 3.10 ,查看本頁面的前提條件來驗證你的CentOS 版本是否支持 Docker

1.1 虛擬機與Docker的不同

虛擬機與Docker有何不同 https://www.cnblogs.com/kex1n/p/6933039.htm

那Docker由什麼組成呢, 包括三個基本概念:

  • 倉庫(Repository)
  • 鏡像(Image)
  • 容器(Container)

1.其中Registry是Docker用於存放鏡像文件的倉庫,Docker 倉庫的概念跟Git 類似(就像商店存放所有的英雄,只是更改英雄的權限在某些非程序員手裏)。
2.所謂鏡像就是構建容器的源代碼,是一個只讀的模板,由一層一層的文件系統組成的,類似於虛擬機的鏡像(英雄也是隻讀的,有自己的技能被動,你也不能進行操作)。
3.那麼容器就是由Docker鏡像創建的運行實例,類似於虛擬機,容器之間是相互隔離的,包含特定的應用及其所需的依賴文件(好比每個英雄都是隔離的,都有自己的皮膚,技能以及走的路線)。
注:Docker Hub是Docker公司提供的一個註冊服務器(Register)來保存多個倉庫,每個倉庫又可以包含多個具備不同tag的鏡像。

2 安裝Docker步驟

2.1 root賬戶登錄,查看內核版本如下

uname -a

我是虛擬機裝的Centos7,linux 3.10 內核

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2.1 把yum包更新到最新

yum update
[root@localhost ~]# yum update
已加載插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.neusoft.edu.cn
base                                                                              | 3.6 kB  00:00:00     
extras                                                                            | 2.9 kB  00:00:00     
updates                                                                           | 2.9 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                                                 | 159 kB  00:00:00     
(2/4): base/7/x86_64/group_gz                                                     | 165 kB  00:00:00     
(3/4): base/7/x86_64/primary_db                                                   | 6.0 MB  00:00:03     
(4/4): updates/7/x86_64/primary_db                                                | 6.7 MB  00:00:09     
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 NetworkManager.x86_64.1.1.18.0-5.el7 將被 升級
---> 軟件包 NetworkManager.x86_64.1.1.18.0-5.el7_7.2 將被 更新

(期間要選擇確認,輸入 y 即可)

2.2 卸載舊版本(如果安裝過舊版本的話)

檢查軟件包是否安裝了Docker命令

yum list installed | grep docker

若是有的話,卸載docker,卸載命令如下

yum -y remove docker docker-common docker-selinux docker-engine
  (舊版名稱是docker , 最新社區版 docker-engine, 目前已改名爲docker-ce ) 

2.3 安裝需要的軟件包

yum-util 提供yum-config-manager功能,另外兩個是devicemapper驅動依賴的

yum install -y yum-utils device-mapper-persistent-data lvm2
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.neusoft.edu.cn
軟件包 device-mapper-persistent-data-0.8.5-1.el7.x86_64 已安裝並且是最新版本
軟件包 7:lvm2-2.02.185-2.el7_7.2.x86_64 已安裝並且是最新版本
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 yum-utils.noarch.0.1.1.31-52.el7 將被 安裝
--> 正在處理依賴關係 python-kitchen,它被軟件包 yum-utils-1.1.31-52.el7.noarch 需要
--> 正在處理依賴關係 libxml2-python,它被軟件包 yum-utils-1.1.31-52.el7.noarch 需要

2.4 添加docker的yum源

選擇其中一個命令執行

yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo(中央倉庫)

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo(阿里倉庫)

使用中央倉庫,出現以下內容,表示安裝成功

[root@localhost ~]# yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo
已加載插件:fastestmirror
adding repo from: http://download.docker.com/linux/centos/docker-ce.repo
grabbing file http://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

此處可能會報錯 (原因是國內訪問不到docker官方鏡像的緣故),那就使用阿里倉庫(第二個命令)

 [Errno 14] curl#35 - "TCP connection reset by peer

2.5 查看所有倉庫中所有docker版本,並選擇特定版本安裝

yum list docker-ce --showduplicates | sort -r
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
已加載插件:fastestmirror
已安裝的軟件包
可安裝的軟件包
 * updates: mirrors.neusoft.edu.cn
Loading mirror speeds from cached hostfile
 * extras: mirrors.ustc.edu.cn
docker-ce.x86_64            3:19.03.6-3.el7                    docker-ce-stable 
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.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 
 * base: mirrors.cqu.edu.cn

2.6 配置鏡像加速器

(必須要配置,否則會出現訪問鏡像並不穩定 ,而且下載緩慢等問題)

註冊阿里雲賬號 ,登陸該網址自行註冊 ( https://www.aliyun.com/?utm_content=se_1010377 )
在這裏插入圖片描述
進入阿里雲控制檯如圖
在這裏插入圖片描述
執行命令以下3條命令

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["輸入自己的加速器地址"]}
EOF

導入操作

systemctl daemon-reload

結果如下
在這裏插入圖片描述

2.7 安裝Docker

  $ yum install docker-ce (這樣寫默認安裝最新版本)
  $ yum install  docker-ce-<VERSION_STRING> (指定安裝版本) 

執行如下命令

yum install docker-ce-18.03.1.ce

(此處若報錯,一般還是無法官方鏡像,請檢查上一步文件中字母是否有錯誤)

安裝成功,如下

[root@localhost ~]# yum install docker-ce-18.03.1.ce
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.neusoft.edu.cn
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 docker-ce.x86_64.0.18.03.1.ce-1.el7.centos 將被 安裝
--> 正在處理依賴關係 libseccomp >= 2.3,它被軟件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在處理依賴關係 container-selinux >= 2.9,它被軟件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在處理依賴關係 pigz,它被軟件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在處理依賴關係 libcgroup,它被軟件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在處理依賴關係 libltdl.so.7()(64bit),它被軟件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在處理依賴關係 libseccomp.so.2()(64bit),它被軟件包 docker-ce-18.03.1.ce-1.el7.centos.x86_64 需要
--> 正在檢查事務

2.8 啓動並加入開機啓動

啓動和開啓啓動命令

systemctl start docker    (重啓命令 	systemctl restart docker) 
systemctl enable docker   (開機啓動)
docker version            (查看docker版本號)

結果如下

[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]# docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:20:16 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:23:58 2018
  OS/Arch:      linux/amd64
  Experimental: false

2.9 驗證是否安裝成功

執行命令

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:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
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已經安裝成功了 。

接下來我們就可以訪問並下載你所需要的鏡像,或者使用dockerfile自行構建Docker鏡像,部署安裝你的應用程序到docker容器中了。

3 docker的其它命令

3.1 查看docker的狀態

service docker status

結果如下

[root@centos7-00 ~]# service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2020-02-16 16:54:35 CST; 10min ago
     Docs: https://docs.docker.com
 Main PID: 1093 (dockerd)
    Tasks: 18
   Memory: 128.3M
   CGroup: /system.slice/docker.service
           ├─1093 /usr/bin/dockerd
           └─2418 docker-containerd --config /var/run/docker/containerd/containerd.toml

2月 16 16:54:33 centos7-00 dockerd[1093]: time="2020-02-16T16:54:33+08:00" level=info msg="contai...nerd
2月 16 16:54:33 centos7-00 dockerd[1093]: time="2020-02-16T16:54:33.840191618+08:00" level=info m...ay2"
2月 16 16:54:34 centos7-00 dockerd[1093]: time="2020-02-16T16:54:34.089454686+08:00" level=info m...nds"
2月 16 16:54:34 centos7-00 dockerd[1093]: time="2020-02-16T16:54:34.091321598+08:00" level=info m...rt."
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.298481220+08:00" level=info m...ess"
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.627563756+08:00" level=info m...ne."
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.928880127+08:00" level=info m...1-ce
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.952860792+08:00" level=info m...ion"
2月 16 16:54:35 centos7-00 dockerd[1093]: time="2020-02-16T16:54:35.986619303+08:00" level=info m...ock"
2月 16 16:54:35 centos7-00 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

3.2 停止docker

service docker stop
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章