參考:
https://www.cnblogs.com/qgc1995/archive/2018/08/29/9553572.html
早就聽說過Docker,一直不清楚是個啥,今天搗鼓了一下,這里做個記錄。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
一,Docker簡介
百科說:Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然后發布到任何流行的Linux機器上,也可以實現虛擬化,容器是完全使用沙箱機制,相互之間不會有任何接口。
看起來有點霧,用過虛擬機的應該對虛擬化技術有點印象,不知道也沒關系,就把它當成輕量級的虛擬機吧(雖然一個是完全虛擬化,一個是操作系統層虛擬化),這個解釋到位:https://www.cnblogs.com/kex1n/p/6933039.html
百科又說:Docker 使用客戶端-服務器 (C/S) 架構模式 使用遠程API來管理和創建Docker容器。Docker 容器(Container)通過 Docker 鏡像(Image)來創建,二者之間的關系類似於面向對象編程中的對象與類
那Docker由什么組成呢, 包括三個基本概念:
- 倉庫(Repository)
- 鏡像(Image)
- 容器(Container)
打個比方:你如果想玩英雄聯盟中騷氣的亞索,你首先得有這個英雄(Docker的鏡像),然后你得花金幣去英雄商店(Docker的倉庫)買,接着進游戲就會看到一個半蹲的發型飄逸的劍客(Docker的容器),所以:
1,其中Registry是Docker用於存放鏡像文件的倉庫,Docker 倉庫的概念跟Git 類似(就像商店存放所有的英雄,只是更改英雄的權限在某些非程序員手里)。
2,所謂鏡像就是構建容器的源代碼,是一個只讀的模板,由一層一層的文件系統組成的,類似於虛擬機的鏡像(英雄也是只讀的,有自己的技能被動,你也不能進行操作)。
3,那么容器就是由Docker鏡像創建的運行實例,類似於虛擬機,容器之間是相互隔離的,包含特定的應用及其所需的依賴文件(好比每個英雄都是隔離的,都有自己的皮膚,技能以及走的路線)。
注:Docker Hub
是Docker公司提供的一個注冊服務器(Register)來保存多個倉庫,每個倉庫又可以包含多個具備不同tag的鏡像
二,安裝Docker
我是虛擬機裝的Centos7,linux 3.10 內核,docker官方說至少3.8以上,建議3.10以上(ubuntu下要linux內核3.8以上, RHEL/Centos 的內核修補過, centos6.5的版本就可以——這個可以試試)
1,root賬戶登錄,查看內核版本如下
[root@localhost ~]# uname -a Linux localhost.qgc 3.10.0-862.11.6.el7.x86_64 #1 SMP Tue Aug 14 21:49:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
2,(可選)把yum包更新到最新(生產環境慎重!yum update會對軟件包和內核升級,此處只是為了排除系統環境的影響,來自筆者的備注—2019年10月30日)
[root@localhost ~]# yum update 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: mirrors.aliyun.com * updates: centos.ustc.edu.cn 正在解決依賴關系 --> 正在檢查事務 ---> 軟件包 bind-libs.x86_64.32.9.9.4-61.el7 將被 升級 ---> 軟件包 bind-libs.x86_64.32.9.9.4-61.el7_5.1 將被 更新 ---> 軟件包 bind-libs-lite.x86_64.32.9.9.4-61.el7 將被 升級 ---> 軟件包 bind-libs-lite.x86_64.32.9.9.4-61.el7_5.1 將被 更新 ---> 軟件包 bind-license.noarch.32.9.9.4-61.el7 將被 升級 ---> 軟件包 bind-license.noarch.32.9.9.4-61.el7_5.1 將被 更新
...
...
驗證中 : 32:bind-license-9.9.4-61.el7.noarch 8/8
更新完畢:
bind-libs.x86_64 32:9.9.4-61.el7_5.1
bind-libs-lite.x86_64 32:9.9.4-61.el7_5.1
bind-license.noarch 32:9.9.4-61.el7_5.1
bind-utils.x86_64 32:9.9.4-61.el7_5.1
完畢!
[root@localhost ~]#
3,安裝需要的軟件包, yum-util 提供yum-config-manager功能,另外兩個是devicemapper驅動依賴的
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: mirrors.aliyun.com * updates: centos.ustc.edu.cn ...
4,設置yum源
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 已加載插件:fastestmirror, langpacks adding repo from: https://download.docker.com/linux/centos/docker-ce.repo grabbing file https://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
5,可以查看所有倉庫中所有docker版本,並選擇特定版本安裝
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r 已加載插件:fastestmirror, langpacks 可安裝的軟件包 * updates: centos.ustc.edu.cn Loading mirror speeds from cached hostfile * extras: mirrors.aliyun.com 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 ...
6,安裝Docker,命令:yum install docker-ce-版本號,我選的是17.12.1.ce,如下
[root@localhost ~]# yum install docker-ce-17.12.1.ce 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: mirrors.aliyun.com * updates: centos.ustc.edu.cn base | 3.6 kB 00:00 docker-ce-stable | 2.9 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 正在解決依賴關系 --> 正在檢查事務 ---> 軟件包 docker-ce.x86_64.0.17.12.1.ce-1.el7.centos 將被 安裝 --> 正在處理依賴關系 container-selinux >= 2.9,它被軟件包 docker-ce-17.12.1.ce-1.el7.centos.x86_64 需要
...
7, 啟動Docker,命令:systemctl start 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.
8,驗證安裝是否成功(有client和service兩部分表示docker安裝啟動都成功了)
[root@localhost ~]# docker version Client: Version: 17.12.1-ce API version: 1.35 Go version: go1.9.4 Git commit: 7390fc6 Built: Tue Feb 27 22:15:20 2018 OS/Arch: linux/amd64
Server: Engine: Version: 17.12.1-ce API version: 1.35 (minimum version 1.12) Go version: go1.9.4 Git commit: 7390fc6 Built: Tue Feb 27 22:17:54 2018 OS/Arch: linux/amd64 Experimental: false
備注:因為我已經安裝過一次,部分貼圖可能有出入,具體可參考博客:https://www.cnblogs.com/yufeng218/p/8370670.html
爬一些常用Docker命令,更多命令詳解,請訪問:http://www.docker.org.cn/dockerppt/106.html:
----------------- docker ps 查看當前正在運行的容器
----------------- docker ps -a 查看所有容器的狀態
----------------- docker start/stop id/name 啟動/停止某個容器
----------------- docker attach id 進入某個容器(使用exit退出后容器也跟着停止運行)
----------------- docker exec -ti id 啟動一個偽終端以交互式的方式進入某個容器(使用exit退出后容器不停止運行)
----------------- docker images 查看本地鏡像
----------------- docker rm id/name 刪除某個容器
----------------- docker rmi id/name 刪除某個鏡像
----------------- docker run --name test -ti ubuntu /bin/bash 復制ubuntu容器並且重命名為test且運行,然后以偽終端交互式方式進入容器,運行bash
----------------- docker build -t soar/centos:7.1 . 通過當前目錄下的Dockerfile創建一個名為soar/centos:7.1的鏡像
----------------- docker run -d -p 2222:22 --name test soar/centos:7.1 以鏡像soar/centos:7.1創建名為test的容器,並以后台模式運行,並做端口映射到宿主機2222端口,P參數重啟容器宿主機端口會發生改變