Docker 知識梳理及其 CentOS7.9 在線/離線安裝使用
Docker 介紹
Docker 是一個(gè)強(qiáng)大的工具,用于高效開發(fā)、打包和部署應(yīng)用程序。Docker 是一種容器管理服務(wù)。Docker 于 2013 年發(fā)布。它是開源的,可用于 Windows、macOS 和 Linux 等不同平臺。Docker 正在快速交付、測試和部署代碼。這樣可以減少編寫代碼和在生產(chǎn)環(huán)境中運(yùn)行代碼之間的延遲。您可以創(chuàng)建稱為容器的自包含環(huán)境。它可以在不同的平臺上一致地運(yùn)行。
Docker 是一組平臺即服務(wù) (PaaS) 產(chǎn)品,它使用操作系統(tǒng)級虛擬化以稱為容器的包的形式交付軟件。容器彼此隔離,并捆綁自己的軟件、庫和配置文件;他們可以通過定義明確的渠道相互通信。所有容器都由單個(gè)操作系統(tǒng)內(nèi)核運(yùn)行,因此使用的資源比虛擬機(jī)少。
Docker 是一個(gè)開源 容器化 平臺,通過該平臺,您可以將應(yīng)用程序及其所有依賴項(xiàng)打包到一個(gè)稱為容器的標(biāo)準(zhǔn)化單元中。容器重量輕,這使得它們具有可移植性,并且它們與底層基礎(chǔ)設(shè)施和其他容器隔離。您可以在任何安裝了 docker 的計(jì)算機(jī)上將 docker 鏡像 作為 docker 容器運(yùn)行,而無需依賴 操作系統(tǒng)。
為什么 Docker 很受歡迎?
Docker 因其對軟件開發(fā)和部署的影響而廣受歡迎。以下是 docker 流行的一些主要原因:
- 可移植性: Docker 有助于開發(fā)人員將其應(yīng)用程序與所有依賴項(xiàng)打包到單個(gè)輕量級容器中。它有助于確保不同計(jì)算環(huán)境中的性能一致。
- 可重復(fù)性: 通過將應(yīng)用程序及其依賴項(xiàng)封裝在一個(gè)容器中,它可以確保軟件設(shè)置在開發(fā)、測試和生產(chǎn)環(huán)境中保持一致。
- 效率: Docker 通過其基于容器的架構(gòu)優(yōu)化了資源利用率。它允許開發(fā)人員在單個(gè)主機(jī)系統(tǒng)上運(yùn)行多個(gè)隔離的應(yīng)用程序。
- 可擴(kuò)展性: Docker 的可擴(kuò)展性功能有助于開發(fā)人員在工作負(fù)載增加時(shí)更輕松地處理他們的應(yīng)用程序。
Docker 的關(guān)鍵組件
以下是 Docker 的一些關(guān)鍵組件:
- Docker Engine: 它是 Docker 的核心部分,用于處理容器的創(chuàng)建和管理。
- Docker Image: 它是一個(gè)只讀模板,用于創(chuàng)建容器,包含應(yīng)用程序代碼和依賴項(xiàng)。
- Docker Hub: 它是一個(gè)基于云的存儲庫,用于查找和共享容器映像。
- Dockerfile: 這是一個(gè)腳本,其中包含構(gòu)建 docker 鏡像的說明。
- Docker Registry :它是 docker 鏡像的存儲分發(fā)系統(tǒng),您可以在其中以公共和私有模式存儲鏡像。
Docker 架構(gòu)以及 Docker 的工作原理?
Docker 使用客戶端-服務(wù)器架構(gòu)。Docker 客戶端與 docker 守護(hù)程序通信,這有助于構(gòu)建、運(yùn)行和分發(fā) docker 容器。Docker 客戶端與守護(hù)程序在同一系統(tǒng)上運(yùn)行,或者我們可以遠(yuǎn)程將 Docker 客戶端與 Docker 守護(hù)程序連接。在 UNIX 套接字或網(wǎng)絡(luò)上的 REST API 的幫助下,docker 客戶端和守護(hù)程序可以相互交互。

Docker Daemon
Docker 守護(hù)程序通過與其他守護(hù)程序通信來管理所有服務(wù)。它借助 Docker 的 API 請求管理 Docker 對象,例如映像、容器、網(wǎng)絡(luò)和卷。
Docker Client
在 docker 客戶端的幫助下,docker 用戶可以與 docker 交互。docker 命令使用 Docker API。Docker 客戶端可以與多個(gè)守護(hù)程序通信。當(dāng) docker 客戶端在 docker 終端上運(yùn)行任何 docker 命令時(shí),終端會(huì)向守護(hù)進(jìn)程發(fā)送指令。Docker 守護(hù)程序從命令形狀和 REST API 請求內(nèi)的 docker 客戶端獲取這些指令。
docker 客戶端的主要目標(biāo)是提供一種方法來指導(dǎo)從 docker 注冊表中提取鏡像并在 docker 主機(jī)上運(yùn)行它們。客戶端使用的常用命令是 docker build、docker pull 和 docker run。
Docker Host
Docker 主機(jī)是一種負(fù)責(zé)運(yùn)行多個(gè)容器的機(jī)器。它包括 Docker 守護(hù)程序、映像、容器、網(wǎng)絡(luò)和存儲。
Docker Registry
所有 docker 鏡像都存儲在 docker 注冊表中。有一個(gè)公共注冊表,稱為 docker hub,任何人都可以使用。我們也可以運(yùn)行我們的私有注冊表。在 docker run 或 docker pull 命令的幫助下,我們可以從配置的注冊表中提取所需的鏡像。在 docker push 命令的幫助下,鏡像被推送到配置的注冊表中。
Docker Objects
每當(dāng)我們使用 docker 時(shí),我們都會(huì)創(chuàng)建和使用鏡像、容器、卷、網(wǎng)絡(luò)和其他對象。現(xiàn)在,我們將討論 docker 對象:-

Docker Images
鏡像包含創(chuàng)建 Docker 容器的說明。它只是一個(gè)只讀模板。它用于存儲和交付應(yīng)用程序。映像是 docker 體驗(yàn)的重要組成部分,因?yàn)樗鼈円砸郧盁o法實(shí)現(xiàn)的任何方式支持開發(fā)人員之間的協(xié)作。
Docker Containers
容器是從 docker 鏡像創(chuàng)建的,因?yàn)樗鼈兪乾F(xiàn)成的應(yīng)用程序。在 Docker API 或 CLI 的幫助下,我們可以啟動(dòng)、停止、刪除或移動(dòng)容器。容器只能訪問映像中定義的那些資源,除非在容器中構(gòu)建映像期間定義了其他訪問權(quán)限。
Docker Storage
我們可以將數(shù)據(jù)存儲在容器的可寫層中,但它需要一個(gè)存儲驅(qū)動(dòng)程序。存儲驅(qū)動(dòng)程序控制和管理 Docker 主機(jī)上的鏡像和容器。
以下是 Docker 中常見的數(shù)據(jù)持久化方法:
- 數(shù)據(jù)卷(Volumes):數(shù)據(jù)卷是一種特殊的目錄,可以繞過容器文件系統(tǒng)并將數(shù)據(jù)存儲在宿主機(jī)上。數(shù)據(jù)卷可以被一個(gè)或多個(gè)容器共享,并且在容器之間持久存在。使用數(shù)據(jù)卷可以方便地備份、恢復(fù)和遷移數(shù)據(jù)。
- 綁定掛載(Bind Mounts):綁定掛載允許將宿主機(jī)上的文件或目錄直接掛載到容器中。這樣,容器可以訪問宿主機(jī)文件系統(tǒng)中的數(shù)據(jù),并且對數(shù)據(jù)的修改也會(huì)反映到宿主機(jī)上。
- 命名卷(Named Volumes):命名卷是一種具有名稱的數(shù)據(jù)卷,可以在多個(gè)容器之間共享,并且可以方便地管理和使用。通過為卷指定名稱,可以在創(chuàng)建容器時(shí)直接引用該名稱,并且 Docker 會(huì)自動(dòng)創(chuàng)建和管理卷。
- 數(shù)據(jù)卷容器(Data Volume Containers):數(shù)據(jù)卷容器是一種特殊類型的容器,用于存儲和管理數(shù)據(jù)卷。其他容器可以通過 --volumes-from 選項(xiàng)掛載這些數(shù)據(jù)卷容器,從而實(shí)現(xiàn)數(shù)據(jù)的共享和持久化。
Docker 網(wǎng)絡(luò)
Docker 網(wǎng)絡(luò)為 Docker 容器提供完全隔離。這意味著用戶可以將 docker 容器鏈接到多個(gè)網(wǎng)絡(luò)。它需要的操作系統(tǒng)實(shí)例非常少來運(yùn)行工作負(fù)載。
網(wǎng)絡(luò)模式說明
| 網(wǎng)絡(luò)模式 | 配置 | 說明 |
|---|---|---|
| bridge | --network=bridge | 為每個(gè)容器分配 IP 。并將容器連接到 docker0 虛擬網(wǎng)橋上,這種模式是默認(rèn)模式 |
| host | --network=host | 容器不會(huì)創(chuàng)建自己的網(wǎng)卡,配置 IP 等,而是使用宿主機(jī)的 IP 和端口 |
| none | --network=none | 容器關(guān)閉網(wǎng)絡(luò)功能,不進(jìn)行任何網(wǎng)路設(shè)置 |
| container | --network=container | 容器不會(huì)創(chuàng)建自己的網(wǎng)卡和IP,而是和一個(gè)指定的容器共享 IP 和端口 |
| custom | --network=new_bridge | 為每個(gè)容器分配 IP 。并將容器連接到自定義的虛擬網(wǎng)橋上 |
Docker 安裝方式
準(zhǔn)備環(huán)境
系統(tǒng)要求:Docker 支持 64 位版本 CentOS 7/8,并且要求內(nèi)核版本不低于 3.10。 CentOS 7 滿足最低內(nèi)核的要求,但由于內(nèi)核版本比較低,部分功能(如 overlay2 存儲層驅(qū)動(dòng))無法使用,并且部分功能可能不太穩(wěn)定。
如需升級 Linux 內(nèi)核參考《CentOS 7 內(nèi)核升級最新記錄(yum及編譯)》
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i.bak '7s/enforcing/disabled/' /etc/selinux/config
[root@localhost ~]# swapoff -a
[root@localhost ~]# sed -ri.bak 's/.*swap.*/#&/' /etc/fstab
[root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld
[root@localhost ~]# firewall-cmd --state
not running
# 更換阿里源
[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
在線 YUM 安裝
時(shí)間:2024-09 最新版本 Docker-26.1.4
# 卸載舊版本
[root@localhost ~]# yum remove docker docker-client docker-client-latest docker-common docker-latest \
docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
# 安裝所需的軟件包。yum-utils 提供了 yum-config-manager
[root@localhost ~]# yum install -y yum-utils
# 添加 yum 軟件源(三選一)
# -----------------------------------------------------------------------------------------------------------------------------
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 阿里源
yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo # 清華大學(xué)源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # 官方源(較慢)
# -----------------------------------------------------------------------------------------------------------------------------
# 列出存儲庫中可用的版本。按版本號(從高到低)
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
* updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
* extras: mirrors.aliyun.com
docker-ce.x86_64 3:26.1.4-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.3-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.5-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.4-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.3-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:24.0.9-1.el7 docker-ce-stable
docker-ce.x86_64 3:24.0.8-1.el7 docker-ce-stable
# 方式一:選擇安裝最新版本
[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io
# 或方式二:選擇安裝指定版本 例如docker-ce-24.0.9
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
# 若選擇測試版本的 Docker 請執(zhí)行以下命令
yum-config-manager --enable docker-ce-test
# 驗(yàn)證
[root@localhost ~]# docker -v
[root@localhost ~]# docker version
Client:
Version: 23.0.6
API version: 1.42
Go version: go1.19.9
Git commit: ef23cbc
Built: Fri May 5 21:13:15 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 23.0.6
API version: 1.42 (minimum version 1.12)
Go version: go1.19.9
Git commit: 9dbdbd4
Built: Fri May 5 21:14:22 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.6.21
GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc:
Version: 1.1.7
GitCommit: v1.1.7-0-g860f061
docker-init:
Version: 0.19.0
GitCommit: de40ad0
# 添加國內(nèi)鏡像加速(每次修改需重啟 docker)
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://dockerpull.com",
"https://docker.anyhub.us.kg",
"https://dockerhub.jobcher.com",
"https://dockerhub.icu",
"https://docker.awsl9527.cn"
]
}
EOF
# 重載
[root@localhost ~]# systemctl daemon-reload && systemctl restart docker
# 啟動(dòng)|停止|查看|開機(jī)自啟
[root@localhost ~]# systemctl start|stop|status|enable docker
# 測試 Docker 是否安裝正確
[root@localhost ~]# docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d195baed67eb: Pull complete
Digest: sha256:be06e3c4ce8780c0f87fbf66ec9b34623ba2fd14caa5559be5b593653821b814
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/
若能正常輸出以上信息,則說明安裝成功。
二進(jìn)制離線安裝
時(shí)間:2024-09 下載 Docker docker-23.0.6.tgz 為例
下載 Docker 地址
- 阿里地址【 docker-23.0.6.tgz 】
- 清華大學(xué)地址【 docker-23.0.6.tgz 】
- 官方地址【 docker-23.0.6.tgz 】

# 下載 Docker docker-23.0.6.tgz 為例 (當(dāng)前最新版本 docker-27.2.0.tgz)
[root@localhost ~]# wget https://mirrors.aliyun.com/docker-ce/linux/static/stable/x86_64/docker-23.0.6.tgz
[root@localhost ~]# tar -xf docker-23.0.6.tgz
[root@localhost ~]# cp docker/* /usr/local/bin/
[root@localhost ~]# docker -v
Docker version 23.0.6, build ef23cbc
[root@localhost ~]# docker -h
# 將docker注冊為 Systemd 的 service
# /usr/lib/systemd/system/docker.service 或 /etc/systemd/system/docker.service
[root@localhost ~]# cat > /usr/lib/systemd/system/docker.service <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/local/bin/dockerd -H unix:///var/run/docker.sock --selinux-enabled=false --default-ulimit nofile=65536:65536
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
# 如果需要開啟遠(yuǎn)程服務(wù)ExecStart屬性修改為以下命令:
ExecStart=/usr/local/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --selinux-enabled=false --default-ulimit nofile=65536:65536
# -H tcp://0.0.0.0:2375 開啟遠(yuǎn)程連接命令
# 測試遠(yuǎn)程連接 docker -H DockerHost_ip version
# 添加文件可執(zhí)行權(quán)限
[root@localhost ~]# chmod +x /usr/lib/systemd/system/docker.service
# 添加國內(nèi)鏡像加速(每次修改需重啟 docker)
[root@localhost ~]# mkdir /etc/docker
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://dockerpull.com",
"https://docker.anyhub.us.kg",
"https://dockerhub.jobcher.com",
"https://dockerhub.icu",
"https://docker.awsl9527.cn"
]
}
EOF
# 重載
[root@localhost ~]# systemctl daemon-reload && systemctl restart docker
# 啟動(dòng)|停止|查看|開機(jī)自啟
[root@localhost ~]# systemctl start|stop|status|enable docker
# 測試 Docker 是否安裝正確
[root@localhost ~]# docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d195baed67eb: Pull complete
Digest: sha256:be06e3c4ce8780c0f87fbf66ec9b34623ba2fd14caa5559be5b593653821b814
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 常用命令

出處:http://www.rzrgm.cn/zhangwencheng
版權(quán):本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出 原文鏈接
浙公網(wǎng)安備 33010602011771號