文章目錄
- 一、 docker概述
- 1.1 Docker中基本概念(三大核心)
- 鏡像(Image)
- 容器(Container)
- 倉庫(Repository)
- 1.2 Docker 與虛擬機
- 相同點
- docker的優點
- docker和虛擬機的不足
- 1.3 Docker引擎(Docker Engine)
- 1.4 docker的分層存盤
- 二、 安裝docker及docker的基本命令
- 2.1 安裝docker
- 2.2 docker 常用命令
- 2.2.1 關于鏡像的操作
- 2.2.2 關于容器的操作
一、 docker概述
Docker 是一個開源的應用容器引擎:讓開發者可以打包他們的應用以及依賴包到一個可移植的鏡像中,然后發布到任何流行的 Linux或Windows 機器上,也可以實作虛擬化,是一個輕量級的“虛擬機”,容器是完全使用沙箱機制,相互之間不會有任何介面,
1.1 Docker中基本概念(三大核心)
鏡像(Image)
鏡像,從認識上簡單的來說,就是面向物件中的類,相當于一個模板,從本質上來說,鏡像相當于一個檔案系統,Docker 鏡像是一個特殊的檔案系統,除了提供容器運行時所需的程式、庫、資源、配置等檔案外,還包含了一些為運行時準備的一些配置引數(如匿名卷、環境變數、用戶等),鏡像不包含任何動態資料,其內容在構建之后也不會被改變,
容器(Container)
容器,從認識上來說,就是類創建的實體,就是依據鏡像這個模板創建出來的物體,容器的實質是行程,但與直接在宿主執行的行程不同,容器行程運行于屬于自己的獨立的命名空間,因此容器可以擁有自己的root 檔案系統、自己的網路配置、自己的行程空間,甚至自己的用戶ID 空間,容器內的行程是運行在一個隔離的環境里,使用起來,就好像是在一個獨立于宿主的系統下操作一樣,這種特性使得容器封裝的應用比直接在宿主運行更加安全,
倉庫(Repository)
倉庫,從認識上來說,就好像軟體包上傳下載站,有各種軟體的不同版本被上傳供用戶下載,鏡像構建完成后,可以很容易的在當前宿主機上運行,但是,如果需要在其它服務器上使用這個鏡像,我們就需要一個集中的存盤、分發鏡像的服務,Docker Registry 就是這樣的服務,
1.2 Docker 與虛擬機
相同點
- 可在不同的主機之間遷移;
- 都具備 root 權限;
- 都可以遠程控制;
- 都有備份、回滾操作,
docker的優點
- 更高效的利用系統資源:由于容器不需要進行硬體虛擬以及運行完整作業系統等額外開銷,Docker 對系統資源的利用率更高,無論是應用執行速度、記憶體損耗或者檔案存盤速度,都要比傳統虛擬機技術更高效,因此,相比虛擬機技術,一個相同配置的主機,往往可以運行更多數量的應用,
- 更快速的啟動時間:傳統的虛擬機技術啟動應用服務往往需要數分鐘,而Docker 容器應用,由于直接運行于宿主內核,無需啟動完整的作業系統,因此可以做到秒級、甚至毫秒級的啟動時間,大大的節約了開發、測驗、部署的時間,
- 一致的運行環境:開發程序中一個常見的問題是環境一致性問題,由于開發環境、測驗環境、生產環境不一致,導致有些bug 并未在開發程序中被發現,而Docker 的鏡像提供了除內核外完整的運行時環境,確保了應用運行環境一致性,從而不會再出現「這段代碼在我機器上沒問題啊」這類問題,
- 持續交付和部署:Docker是build once,run everywhere. 使用Docker 可以通過定制應用鏡像來實作持續集成、持續交付、部署,開發人員可以通過Dockerfile 來進行鏡像構建,并結合持續集成(Continuous Integration) 系統進行集成測驗,而運維人員則可以直接在生產環境中快速部署該鏡像,甚至結合持續部署(Continuous Delivery/Deployment) 系統進行自動部署,
- 更輕松的遷移:Docker 使用的分層存盤以及鏡像的技術,使得應用重復部分的復用更為容易,也使得應用的維護更新更加簡單,基于基礎鏡像進一步擴展鏡像也變得非常簡單,此外,Docker 團隊同各個開源專案團隊一起維護了一大批高質量的官方鏡像,既可以直接在生產環境使用,又可以作為基礎進一步定制,大大的降低了應用服務的鏡像制作成本,使用Dockerfile 使鏡像構建透明化,不僅僅開發團隊可以理解應用運行環境,也方便運維團隊理解應用運行所需條件,幫助更好的生產環境中部署該鏡像,
docker和虛擬機的不足
- docker:對內核沒有控制權限,只有容器的提供者具備升級權限,只有一個內核運行在物理節點上,幾乎不能實作不同的作業系統混合,容器提供者一般僅提供少數的幾個作業系統,
- 虛擬機:每一臺虛擬機都具有更大的負載,耗費更多的資源,用戶需要全權維護和管理,一臺物理機上能夠運行的虛擬機非常有限,
1.3 Docker引擎(Docker Engine)
docker之所以可以跨系統,就是因為有 Docker引擎(Docker Engine)的存在,只要你能在你的服務上裝上 Docker引擎(Docker Engine),你就可以使用Docker鏡像進行部署,
Docker Engine是具有以下主要組件的客戶端-服務器應用程式:
- 服務器是一種長期運行的程式,稱為守護程式行程( dockerd命令),
- REST API,它指定程式可以用來與守護程式進行通信并指示其操作的介面,
- 命令列界面(CLI)客戶端(docker命令),
1.4 docker的分層存盤
因為鏡像包含作業系統完整的root 檔案系統,其體積往往是龐大的,因此在Docker設計時,就充分利用Union FS 的技術,將其設計為分層存盤的架構,所以嚴格來說,鏡像并非是像一個ISO 那樣的打包檔案,鏡像只是一個虛擬的概念,其實際體現并非由一個檔案組成,而是由一組檔案系統組成,或者說,由多層檔案系統聯合組成,
鏡像構建時,會一層層構建,前一層是后一層的基礎,每一層構建完就不會再發生改變,后一層上的任何改變只發生在自己這一層,比如,洗掉前一層檔案的操作,實際不是真的洗掉前一層的檔案,而是僅在當前層標記為該檔案已洗掉,在最終容器運行的時候,雖然不會看到這個檔案,但是實際上該檔案會一直跟隨鏡像,因此,在構建鏡像的時候,需要額外小心,每一層盡量只包含該層需要添加的東西,任何額外的東西應該在該層構建結束前清理掉,
分層存盤的特征還使得鏡像的復用、定制變的更為容易,甚至可以用之前構建好的鏡像作為基礎層,然后進一步添加新的層,以定制自己所需的內容,構建新的鏡像,
二、 安裝docker及docker的基本命令
2.1 安裝docker
1. 安裝依賴包
yum install yum-utils device-mapper-persistent-data lvm2 -y
#yum-utils提供了yum-config-manager
#device mapper存盤驅動程式需要device-mapper-persistent-data和Ivm2
#Device Mapper是 Linux2.6內核中支持邏輯卷管理的通用設備映射機制,
它為實作用于存盤資源管理的塊設備驅動提供了一個高度模塊化的內核架構,
2. 設定阿里云鏡像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3. 安裝Docker-CE
yum install docker-ce -y
4. 開啟docker服務,設為開機自啟動
systemctl start docker.service
systemctl enable docker.service
5. 配置鏡像加速
登錄阿里云官網(一定要登錄),在官網首頁搜索”鏡像加速“
搜索的第一個,點進去
網頁的下面有一個容器鏡像服務,點進去
進去以后點擊管理控制臺
找到鏡像加速器,選擇centos
復制以下的內容到自己的終端直接運行,就可以了
ee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xda9renm.mirror.aliyuncs.com"]
}
EOF
注意:每個人都有自己的專屬加速,用別人的就是會很慢,所以不要嫌麻煩
2.2 docker 常用命令
2.2.1 關于鏡像的操作
docker version 查看版本資訊

docker search nginx 搜索nginx鏡像(共有倉庫)

下載nginx鏡像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
d121f8d1c412: Pull complete
ebd81fc8c071: Pull complete
655316c160af: Pull complete
d15953c0e0f8: Pull complete
2ee525c5c3cc: Pull complete
Digest: sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
查看下載的鏡像串列
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 7e4d58f0e5f3 9 days ago 133MB
下載的鏡像存放位置
[root@localhost ~]# cd /var/lib/docker/image/overlay2/
[root@localhost overlay2]# ls
distribution imagedb layerdb repositories.json
[root@localhost overlay2]# vim repositories.json ##存放著鏡像檔案
{"Repositories":{"nginx":{"nginx:latest":"sha256:7e4d58f0e5f3b60077e9a5d96b4be1b974b5a484f54f9393000a99f3b6816e3d","nginx@sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0":"sha256:7e4d58f0e5f3b60077e9a5d96b4be1b974b5a484f54f9393000a99f3b6816e3d"}}}
查看下載鏡像的詳細資訊,兩種方式
docker inspect nginx:latest 后面跟鏡像名稱
docker inspect 7e4d58f0e5f3 后面跟鏡像ID
添加鏡像的標簽,相當于創建了一個別名
[root@localhost overlay2]# docker tag nginx:latest nginx:web
[root@localhost overlay2]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 7e4d58f0e5f3 9 days ago 133MB
nginx web 7e4d58f0e5f3 9 days ago 133MB
洗掉鏡像,如果鏡像添加了新的標簽,則無法洗掉,只有鏡像只存在一個標簽時才可以洗掉鏡像,不管這個標簽是新添加的,還是原來的
[root@localhost overlay2]# docker rmi nginx:web
Untagged: nginx:web
[root@localhost overlay2]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 7e4d58f0e5f3 9 days ago 133MB
[root@localhost overlay2]# docker tag nginx:latest nginx:web
[root@localhost overlay2]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 7e4d58f0e5f3 9 days ago 133MB
nginx web 7e4d58f0e5f3 9 days ago 133MB
[root@localhost overlay2]# docker rmi 7e4d58f0e5f3
Error response from daemon: conflict: unable to delete 7e4d58f0e5f3 (must be forced) - image is referenced in multiple repositories
重新為nginx添加標簽
[root@localhost overlay2]# docker tag nginx:latest nginx:web
[root@localhost overlay2]# docker rmi nginx:web
Untagged: nginx:web
[root@localhost overlay2]# docker rmi 7e4d58f0e5f3
Untagged: nginx:latest
Untagged: nginx@sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
Deleted: sha256:7e4d58f0e5f3b60077e9a5d96b4be1b974b5a484f54f9393000a99f3b6816e3d
Deleted: sha256:eedfd37abc0b05520a719addf6675b372687fe9a2ed1746fa988335720d4376c
Deleted: sha256:4f052ddd517f6dc5b74d2f7d881838b8ec6a5e67ec7a1da8bbbe91e1e068ad3a
Deleted: sha256:5f78154d356577280e4ac0d9bf0d021d488828e57f953b011dcbadaecd539ce4
Deleted: sha256:db5783668220e16186080f3e9d69ad748aab8c72abb8e457f6fc45369461634b
Deleted: sha256:07cab433985205f29909739f511777a810f4a9aff486355b71308bb654cdc868
[root@localhost overlay2]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
重新下載nginx鏡像進行匯出到指定目錄
[root@localhost overlay2]# docker pull nginx ##下載nginx鏡像
[root@localhost overlay2]# docker save -o /opt/nginx nginx:latest ##將nginx:latest匯出到/opt目錄下,命名為nginx
[root@localhost overlay2]# ll -h /opt
總用量 131M
drwx--x--x. 4 root root 28 9月 18 15:40 containerd
-rw-------. 1 root root 131M 9月 19 21:37 nginx
drwxr-xr-x. 2 root root 6 10月 31 2018 rh
鏡像的重新載入,如果不小心做了誤刪鏡像的操作,可以通過到處的檔案進行重新載入
[root@localhost opt]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
Deleted: sha256:7e4d58f0e5f3b60077e9a5d96b4be1b974b5a484f54f9393000a99f3b6816e3d
Deleted: sha256:eedfd37abc0b05520a719addf6675b372687fe9a2ed1746fa988335720d4376c
Deleted: sha256:4f052ddd517f6dc5b74d2f7d881838b8ec6a5e67ec7a1da8bbbe91e1e068ad3a
Deleted: sha256:5f78154d356577280e4ac0d9bf0d021d488828e57f953b011dcbadaecd539ce4
Deleted: sha256:db5783668220e16186080f3e9d69ad748aab8c72abb8e457f6fc45369461634b
Deleted: sha256:07cab433985205f29909739f511777a810f4a9aff486355b71308bb654cdc868
[root@localhost opt]# docker load < /opt/nginx ##重新載入鏡像
07cab4339852: Loading layer 72.49MB/72.49MB
f431d0917d41: Loading layer 64.31MB/64.31MB
60c688e8765e: Loading layer 3.072kB/3.072kB
eabfa4cd2d12: Loading layer 4.096kB/4.096kB
908cf8238301: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 7e4d58f0e5f3 9 days ago 133MB
2.2.2 關于容器的操作
查看容器
docker ps -a
-a 列出所有的容器,包括未運行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
容器的ID 來源于哪一個鏡像 執行的指令 創建時間 容器狀態 埠 名字
容器的創建
[root@localhost opt]# docker create -it nginx:latest /bin/bash ##創建容器,并同時創建一個子bash環境
-i 讓容器的便準輸入保持打開
-t 讓Docker分配一個偽終端
6e198a8c25d9d38f130a1e569857986d7a84ae0d177540e4017a6b1e9451fe0c
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" About a minute ago Created quirky_greider

開啟容器
[root@localhost opt]# docker start 6e198a8c25d9 ##開啟容器
6e198a8c25d9
[root@localhost opt]# docker ps -a ##這時候容器的狀態變成了up,埠為80/tcp,這里的埠是對容器內部的埠,同樣的容器對外也會有一個埠,連個埠做映射才可以實作對外的通信
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 14 seconds 80/tcp quirky_greider
這時候我們查看網卡配置資訊,多了一個B類的私有地址作為docker的網關,一旦docker這個網關掛了,服務的狀態會仍然保持原有狀態,排錯時候需要注意,

[root@localhost ~]# docker pull centos:7 ##下載centos7的鏡像
7: Pulling from library/centos
75f829a71a1c: Pull complete
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@localhost ~]# docker create -it centos:7 /bin/bash ##為centos7鏡像創建容器
9dd5e445e513a6c64372b9e33a81f40f0494fdf87478739e25ca308fc8dec330
[root@localhost ~]# docker ps -a ##查看容器串列
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9dd5e445e513 centos:7 "/bin/bash" 50 seconds ago Created funny_grothendieck
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 10 hours ago Up 10 hours 80/tcp quirky_greider
[root@localhost ~]# docker start 9dd5e445e513 ##開啟容器
9dd5e445e513
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9dd5e445e513 centos:7 "/bin/bash" About a minute ago Up 7 seconds funny_grothendieck
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 10 hours ago Up 10 hours 80/tcp quirky_greider
停止容器
[root@localhost ~]# docker stop 9dd5e445e513 ##停止容器服務
9dd5e445e513
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9dd5e445e513 centos:7 "/bin/bash" 4 minutes ago Exited (137) 12 seconds ago funny_grothendieck
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 10 hours ago Up 10 hours 80/tcp quirky_greider
洗掉容器
[root@localhost ~]# docker rm 9dd5e445e513 ##洗掉容器,洗掉鏡像為rmi
9dd5e445e513
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 10 hours ago Up 10 hours 80/tcp quirky_greider
啟動執行命令查看系統/opt目錄,這種方式使用容器,會在使用完成后自動停止,并釋放資源
[root@localhost ~]# docker run centos:7 /bin/bash -c ls / ##啟動centos:7容器,并加載一個子bash環境,使用命令ls查看根目錄,執行完就關閉,狀態是 Exited
Unable to find image 'centos:7' locally
7: Pulling from library/centos
75f829a71a1c: Pull complete
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 7e4d58f0e5f3 9 days ago 133MB
centos 7 7e6257c9f8d8 5 weeks ago 203MB
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43957539a471 centos:7 "/bin/bash -c ls /" 25 minutes ago Exited (0) 25 minutes ago trusting_sanderson
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 11 hours ago Up 11 hours 80/tcp quirky_greider
持續在后臺執行
[root@localhost ~]# docker run -d centos:7 /bin/bash -c "while true;do echo hello;done" ##狀態為up
587a0678365e6e687c3a3f47d4bec64d3e2c829e3a6996b508464830b6862892
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
587a0678365e centos:7 "/bin/bash -c 'while…" 13 seconds ago Up 13 seconds wizardly_sutherland
43957539a471 centos:7 "/bin/bash -c ls /" 35 minutes ago Exited (0) 35 minutes ago trusting_sanderson
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 11 hours ago Up 11 hours 80/tcp quirky_greider
進入容器,進入容器之前必須保持容器是up狀態
[root@localhost ~]# docker create -it centos:7 /bin/bash
eec9262be812c3e70d99cc12e1245cd3298ff2c30a14ccb3fea5ebfcefb81126
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eec9262be812 centos:7 "/bin/bash" 13 seconds ago Created nostalgic_goldberg
587a0678365e centos:7 "/bin/bash -c 'while…" 30 minutes ago Up 30 minutes wizardly_sutherland
43957539a471 centos:7 "/bin/bash -c ls /" About an hour ago Exited (0) About an hour ago trusting_sanderson
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 12 hours ago Up 11 hours 80/tcp quirky_greider
[root@localhost ~]# docker start ee
ee
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eec9262be812 centos:7 "/bin/bash" 35 seconds ago Up 7 seconds nostalgic_goldberg
587a0678365e centos:7 "/bin/bash -c 'while…" 31 minutes ago Up 31 minutes wizardly_sutherland
43957539a471 centos:7 "/bin/bash -c ls /" About an hour ago Exited (0) About an hour ago trusting_sanderson
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 12 hours ago Up 11 hours 80/tcp quirky_greider
[root@localhost ~]# docker exec -it eec9262be812 /bin/bash ##進容器
[root@eec9262be812 /]#
[root@eec9262be812 ~]# exit ##退出容器
exit
進入容器后我們可以使用部分命令,也有一部分命令不能使用,因為容器畢竟不是虛擬機,而且現在沒有建立內外埠映射,以后再進行演示,
容器的匯出
[root@localhost ~]# docker export 6e198a8c25d9 > /opt/nginx_container
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
containerd nginx nginx_container rh
容器只要存在就不能洗掉容器的鏡像,只有先將容器洗掉,才可以洗掉鏡像,
容器的批量清理,不加bash就洗掉不了,運行中的容器不能洗掉
[root@localhost opt]# docker ps -a | awk '{print "docker rm "$1}'
docker rm CONTAINER
docker rm 4bedfe57a907
docker rm 691f58d21035
docker rm 6e198a8c25d9
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4bedfe57a907 centos:7 "/bin/bash -c ls /opt" 22 seconds ago Exited (0) 22 seconds ago bold_euler
691f58d21035 centos:7 "/bin/bash -c ls /" 27 seconds ago Exited (0) 27 seconds ago sweet_goldstine
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 12 hours ago Up 12 hours 80/tcp quirky_greider
[root@localhost opt]# docker ps -a | awk '{print "docker rm "$1}' | bash
Error: No such container: CONTAINER
4bedfe57a907
691f58d21035
Error response from daemon: You cannot remove a running container 6e198a8c25d9d38f130a1e569857986d7a84ae0d177540e4017a6b1e9451fe0c. Stop the container before attempting removal or force remove
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e198a8c25d9 nginx:latest "/docker-entrypoint.…" 12 hours ago Up 12 hours 80/tcp quirky_greider
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/105454.html
標籤:其他
