1.安裝環境說明
Docker官方建議在Ubuntu中安裝,因爲Docker是基於Ubuntu發佈的,而且一般Docker出現的問題Ubuntu是最先更新或者打補丁的。在很多版本的CentOS中是不支持更新最新的一些補丁包的。
由於我們學習的環境都使用的是CentOS,因此這裏我們將Docker安裝到CentOS上。Docker 運行在 CentOS 7 上,要求系統爲64位、系統內核版本爲 3.10 以上。
建議使用CentOS7及以上,不要使用老版本,因爲環境有些不同,命令有些差異!
注意:這裏建議安裝在CentOS7.x以上的版本,在CentOS6.x的版本中,安裝前需要安裝其他很多的環境而且Docker很多補丁不支持更新。
- 查看自己的內核
uname命令用於打印當前系統相關信息(內核版本號、硬件架構、主機名稱和操作系統類型等)。
[root@localhost ~]# uname -r
3.10.0-957.el7.x86_64
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
2.安裝Docker
使用yum命令在線安裝
[root@localhost ~]# yum install docker
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.aliyun.com
* updates: mirrors.163.com
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 6.5 MB 00:00:01
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 docker.x86_64.2.1.13.1-94.gitb2f74b2.el7.centos 將被 升級
---> 軟件包 docker.x86_64.2.1.13.1-96.gitb2f74b2.el7.centos 將被 更新
--> 正在處理依賴關係 docker-common = 2:1.13.1-96.gitb2f74b2.el7.centos,它被軟件包 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 需要
--> 正在處理依賴關係 docker-client = 2:1.13.1-96.gitb2f74b2.el7.centos,它被軟件包 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 需要
--> 正在檢查事務
---> 軟件包 docker-client.x86_64.2.1.13.1-94.gitb2f74b2.el7.centos 將被 升級
---> 軟件包 docker-client.x86_64.2.1.13.1-96.gitb2f74b2.el7.centos 將被 更新
---> 軟件包 docker-common.x86_64.2.1.13.1-94.gitb2f74b2.el7.centos 將被 升級
---> 軟件包 docker-common.x86_64.2.1.13.1-96.gitb2f74b2.el7.centos 將被 更新
--> 解決依賴關係完成
依賴關係解決
===========================================================================================================================================================================
Package 架構 版本 源 大小
===========================================================================================================================================================================
正在更新:
docker x86_64 2:1.13.1-96.gitb2f74b2.el7.centos extras 18 M
爲依賴而更新:
docker-client x86_64 2:1.13.1-96.gitb2f74b2.el7.centos extras 3.9 M
docker-common x86_64 2:1.13.1-96.gitb2f74b2.el7.centos extras 96 k
事務概要
===========================================================================================================================================================================
升級 1 軟件包 (+2 依賴軟件包)
總下載量:22 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/3): docker-common-1.13.1-96.gitb2f74b2.el7.centos.x86_64.rpm | 96 kB 00:00:00
(2/3): docker-client-1.13.1-96.gitb2f74b2.el7.centos.x86_64.rpm | 3.9 MB 00:00:01
(3/3): docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64.rpm | 18 MB 00:00:06
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
總計 3.1 MB/s | 22 MB 00:00:07
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在更新 : 2:docker-common-1.13.1-96.gitb2f74b2.el7.centos.x86_64 1/6
正在更新 : 2:docker-client-1.13.1-96.gitb2f74b2.el7.centos.x86_64 2/6
正在更新 : 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 3/6
清理 : 2:docker-1.13.1-94.gitb2f74b2.el7.centos.x86_64 4/6
清理 : 2:docker-client-1.13.1-94.gitb2f74b2.el7.centos.x86_64 5/6
清理 : 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64 6/6
驗證中 : 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 1/6
驗證中 : 2:docker-client-1.13.1-96.gitb2f74b2.el7.centos.x86_64 2/6
驗證中 : 2:docker-common-1.13.1-96.gitb2f74b2.el7.centos.x86_64 3/6
驗證中 : 2:docker-client-1.13.1-94.gitb2f74b2.el7.centos.x86_64 4/6
驗證中 : 2:docker-1.13.1-94.gitb2f74b2.el7.centos.x86_64 5/6
驗證中 : 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64 6/6
更新完畢:
docker.x86_64 2:1.13.1-96.gitb2f74b2.el7.centos
作爲依賴被升級:
docker-client.x86_64 2:1.13.1-96.gitb2f74b2.el7.centos docker-common.x86_64 2:1.13.1-96.gitb2f74b2.el7.centos
完畢!
3.安裝後查看Docker版本
[root@localhost ~]# docker -v
Docker version 1.13.1, build b2f74b2/1.13.1
4.啓動與停止Docker
啓動docker:service docker start
停止docker:service docker stop
重啓docker:service docker restart
查看docker狀態:service docker status
開機啓動:chkconfig docker on
查看docker概要信息:docker info
查看docker幫助文檔:docker --help
[root@localhost ~]# service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since 一 2019-06-17 15:17:51 CST; 48s ago
Docs: http://docs.docker.com
Main PID: 15257 (dockerd-current)
CGroup: /system.slice/docker.service
├─15257 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docke...
└─15262 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libconta...
6月 17 15:17:49 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:49...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:51 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:51 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:51 localhost.localdomain systemd[1]: Started Docker Application Container....
6月 17 15:17:51 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:51...
Hint: Some lines were ellipsized, use -l to show in full.
5.Docker的基本組成
Docker的架構圖
5.1.鏡像(image)
Docker 鏡像(Image)就是一個只讀的模板。鏡像可以用來創建 Docker 容器,一個鏡像可以創建很多容器。
5.2.容器(container)
Docker 利用容器(Container)獨立運行的一個或一組應用。容器是用鏡像創建的運行實例。
它可以被啓動、開始、停止、刪除。每個容器都是相互隔離的、保證安全的平臺。可以把容器看做是一個簡易版的 Linux 環境(包括root用戶權限、進程空間、用戶空間和網絡空間等)和運行在其中的應用程序。
容器的定義和鏡像幾乎一模一樣,也是一堆層的統一視角,唯一區別在於容器的最上面那一層是可讀可寫的。
5.3.倉庫(repository)
倉庫(Repository)是集中存放鏡像文件的場所。
倉庫(Repository)和倉庫註冊服務器(Registry)是有區別的。倉庫註冊服務器上往往存放着多個倉庫,每個倉庫中又包含了多個鏡像,每個鏡像有不同的標籤(tag)。
倉庫分爲公開倉庫(Public)和私有倉庫(Private)兩種形式。
最大的公開倉庫是 Docker Hub(https://hub.docker.com/),
存放了數量龐大的鏡像供用戶下載。國內的公開倉庫包括阿里雲 、網易雲等.
5.4.總結
需要正確的理解倉儲/鏡像/容器這幾個概念:
Docker 本身是一個容器運行載體或稱之爲管理引擎。我們把應用程序和配置依賴打包好形成一個可交付的運行環境,這個打包好的運行環境就似乎 image鏡像文件。只有通過這個鏡像文件才能生成 Docker 容器。image 文件可以看作是容器的模板。Docker 根據 image 文件生成容器的實例。同一個 image 文件,可以生成多個同時運行的容器實例。
- image 文件生成的容器實例,本身也是一個文件,稱爲鏡像文件。
- 一個容器運行一種服務,當我們需要的時候,就可以通過docker客戶端創建一個對應的運行實例,也就是我們的容器
- 至於倉儲,就是放了一堆鏡像的地方,我們可以把鏡像發佈到倉儲中,需要的時候從倉儲中拉下來就可以了。