Docker 知识梳理及其安装使用

Docker 介绍

Docker 是一个强大的工具,用于高效开发、打包和部署应用程序。Docker 是一种容器管理服务。Docker 于 2013 年发布。它是开源的,可用于 Windows、macOS 和 Linux 等不同平台。Docker 正在快速交付、测试和部署代码。这样可以减少编写代码和在生产环境中运行代码之间的延迟。您可以创建称为容器的自包含环境。它可以在不同的平台上一致地运行。

Docker 是一组平台即服务 (PaaS) 产品,它使用操作系统级虚拟化以称为容器的包的形式交付软件。容器彼此隔离,并捆绑自己的软件、库和配置文件;他们可以通过定义明确的渠道相互通信。所有容器都由单个操作系统内核运行,因此使用的资源比虚拟机少。

Docker 是一个开源 容器化 平台,通过该平台,您可以将应用程序及其所有依赖项打包到一个称为容器的标准化单元中。容器重量轻,这使得它们具有可移植性,并且它们与底层基础设施和其他容器隔离。您可以在任何安装了 docker 的计算机上将 docker 镜像 作为 docker 容器运行,而无需依赖 操作系统。

为什么 Docker 很受欢迎?

Docker 因其对软件开发和部署的影响而广受欢迎。以下是 docker 流行的一些主要原因:

  • 可移植性: Docker 有助于开发人员将其应用程序与所有依赖项打包到单个轻量级容器中。它有助于确保不同计算环境中的性能一致。
  • 可重复性: 通过将应用程序及其依赖项封装在一个容器中,它可以确保软件设置在开发、测试和生产环境中保持一致。
  • 效率: Docker 通过其基于容器的架构优化了资源利用率。它允许开发人员在单个主机系统上运行多个隔离的应用程序。
  • 可扩展性: Docker 的可扩展性功能有助于开发人员在工作负载增加时更轻松地处理他们的应用程序。

Docker 的关键组件

以下是 Docker 的一些关键组件:

  • Docker Engine: 它是 Docker 的核心部分,用于处理容器的创建和管理。
  • Docker Image: 它是一个只读模板,用于创建容器,包含应用程序代码和依赖项。
  • Docker Hub: 它是一个基于云的存储库,用于查找和共享容器映像。
  • Dockerfile: 这是一个脚本,其中包含构建 docker 镜像的说明。
  • Docker Registry :它是 docker 镜像的存储分发系统,您可以在其中以公共和私有模式存储镜像。

Docker 架构以及 Docker 的工作原理?

Docker 使用客户端-服务器架构。Docker 客户端与 docker 守护程序通信,这有助于构建、运行和分发 docker 容器。Docker 客户端与守护程序在同一系统上运行,或者我们可以远程将 Docker 客户端与 Docker 守护程序连接。在 UNIX 套接字或网络上的 REST API 的帮助下,docker 客户端和守护程序可以相互交互。

Docker Daemon

Docker 守护程序通过与其他守护程序通信来管理所有服务。它借助 Docker 的 API 请求管理 Docker 对象,例如映像、容器、网络和卷。

Docker Client

在 docker 客户端的帮助下,docker 用户可以与 docker 交互。docker 命令使用 Docker API。Docker 客户端可以与多个守护程序通信。当 docker 客户端在 docker 终端上运行任何 docker 命令时,终端会向守护进程发送指令。Docker 守护程序从命令形状和 REST API 请求内的 docker 客户端获取这些指令。

docker 客户端的主要目标是提供一种方法来指导从 docker 注册表中提取镜像并在 docker 主机上运行它们。客户端使用的常用命令是 docker build、docker pull 和 docker run。

Docker Host

Docker 主机是一种负责运行多个容器的机器。它包括 Docker 守护程序、映像、容器、网络和存储。

Docker Registry

所有 docker 镜像都存储在 docker 注册表中。有一个公共注册表,称为 docker hub,任何人都可以使用。我们也可以运行我们的私有注册表。在 docker run 或 docker pull 命令的帮助下,我们可以从配置的注册表中提取所需的镜像。在 docker push 命令的帮助下,镜像被推送到配置的注册表中。

Docker Objects

每当我们使用 docker 时,我们都会创建和使用镜像、容器、卷、网络和其他对象。现在,我们将讨论 docker 对象:-

Docker Images

镜像包含创建 Docker 容器的说明。它只是一个只读模板。它用于存储和交付应用程序。映像是 docker 体验的重要组成部分,因为它们以以前无法实现的任何方式支持开发人员之间的协作。

Docker Containers

容器是从 docker 镜像创建的,因为它们是现成的应用程序。在 Docker API 或 CLI 的帮助下,我们可以启动、停止、删除或移动容器。容器只能访问映像中定义的那些资源,除非在容器中构建映像期间定义了其他访问权限。

Docker Storage

我们可以将数据存储在容器的可写层中,但它需要一个存储驱动程序。存储驱动程序控制和管理 Docker 主机上的镜像和容器。

以下是 Docker 中常见的数据持久化方法:

  • 数据卷(Volumes):数据卷是一种特殊的目录,可以绕过容器文件系统并将数据存储在宿主机上。数据卷可以被一个或多个容器共享,并且在容器之间持久存在。使用数据卷可以方便地备份、恢复和迁移数据。
  • 绑定挂载(Bind Mounts):绑定挂载允许将宿主机上的文件或目录直接挂载到容器中。这样,容器可以访问宿主机文件系统中的数据,并且对数据的修改也会反映到宿主机上。
  • 命名卷(Named Volumes):命名卷是一种具有名称的数据卷,可以在多个容器之间共享,并且可以方便地管理和使用。通过为卷指定名称,可以在创建容器时直接引用该名称,并且 Docker 会自动创建和管理卷。
  • 数据卷容器(Data Volume Containers):数据卷容器是一种特殊类型的容器,用于存储和管理数据卷。其他容器可以通过 --volumes-from 选项挂载这些数据卷容器,从而实现数据的共享和持久化。

Docker 网络

Docker 网络为 Docker 容器提供完全隔离。这意味着用户可以将 docker 容器链接到多个网络。它需要的操作系统实例非常少来运行工作负载。

网络模式说明

网络模式 配置 说明
bridge --network=bridge 为每个容器分配 IP 。并将容器连接到 docker0 虚拟网桥上,这种模式是默认模式
host --network=host 容器不会创建自己的网卡,配置 IP 等,而是使用宿主机的 IP 和端口
none --network=none 容器关闭网络功能,不进行任何网路设置
container --network=container 容器不会创建自己的网卡和IP,而是和一个指定的容器共享 IP 和端口
custom --network=new_bridge 为每个容器分配 IP 。并将容器连接到自定义的虚拟网桥上

Docker 安装方式

准备环境

系统要求:Docker 支持 64 位版本 CentOS 7/8,并且要求内核版本不低于 3.10。 CentOS 7 满足最低内核的要求,但由于内核版本比较低,部分功能(如 overlay2 存储层驱动)无法使用,并且部分功能可能不太稳定。

如需升级 Linux 内核参考《CentOS 7 内核升级最新记录(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 安装

时间:2024-09

# 卸载旧版本
[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  # 清华大学源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 请执行以下命令
yum-config-manager --enable docker-ce-test# 验证
[root@localhost ~]# docker -v
Docker version 26.1.4, build 5650f9b# 添加国内镜像加速(每次修改需重启 docker)
[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
{"registry-mirrors": ["https://mirror.baidubce.com","https://mirror.ccs.tencentyun.com","https://wxvmh7l6.mirror.aliyuncs.com","https://hub.atomgit.com","https://registry.docker-cn.com","https://mirrors.tuna.tsinghua.edu.cn","http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"]
}
EOF# 启动
[root@localhost ~]# systemctl daemon-reload && systemctl start 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:latestHello 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 theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare 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/若能正常输出以上信息,则说明安装成功。

二进制离线安装

时间:2024-09

下载 Docker 地址

  • 阿里地址【https://mirrors.aliyun.com/docker-ce 】
  • 清华大学地址【https://mirrors.tuna.tsinghua.edu.cn/docker-ce 】
  • 官方地址【https://download.docker.com 】

# 下载 Docker docker-23.0.6.tgz 为例 (当前最新版本 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/bin/[root@localhost ~]# docker -v
Docker version 23.0.6, build ef23cbc
[root@localhost ~]# docker -h# 将docker注册为 Systemd 的 service
[root@localhost ~]# cat > /etc/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/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.targetEOF# 如果需要开启远程服务ExecStart属性修改为以下命令:
ExecStart=/usr/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 开启远程连接命令# 添加文件可执行权限
[root@localhost ~]# chmod +x /etc/systemd/system/docker.service# 添加国内镜像加速(每次修改需重启 docker)
[root@localhost ~]# mkdir /etc/docker
[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
{"registry-mirrors": ["https://mirror.baidubce.com","https://mirror.ccs.tencentyun.com","https://wxvmh7l6.mirror.aliyuncs.com","https://hub.atomgit.com","https://registry.docker-cn.com","https://mirrors.tuna.tsinghua.edu.cn","http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"]
}
EOF# 启动
[root@localhost ~]# systemctl daemon-reload && systemctl start 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:latestHello 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 theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare 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.hqwc.cn/news/794333.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

硬件功能电路

EEPROMLCD1602电路数码管max485红外开关蜂鸣器译码器移位寄存器步进电机控制复位电路下载电路电源模块温度模块红外热敏电阻交通灯时钟555彩屏矩阵按键单片机烧录电路数码管红外发射显示模块红外接收蜂鸣器驱动流水灯usb供电单片机矩阵单片机电路时钟ADC接口电路单片机电源声音…

BeanShell预处理器获取jmeter请求后变量无法替换

背景 jmeter接口验签逻辑是从请求参数中获取值处理加密验签后,讲请求参数中的sign值替换 如果处理后直接讲sign生成Jmeter变量,在当前请求是无法替换的,因为jmeter请求的URL和body已经运行,不会再重新运行替换解决方法 在当前预处理程序中再写处理当前请求的逻辑// 替换URL…

word 看不到页眉和页脚 以及空白页面的设置

修改文档时,发现递交的文档 不显示 页眉和页脚 以及空白页面的设置在选项卡中勾选jcmaxx33@gmail.com jcmaxx33Team@github

plsql工具无法显示tables问题

每次打开plsql12时tables、views等为空,没有表和视图等 解决办法:选择tool->preferences2、取消勾选最近使用最后重新选择已经登录的用户,就可以正常显示table了

Ftrans无缝替代FTP方案:保障数据传输的安全性与合规性!

FTP(文件传输协议)是一种用于在网络上进行文件传输的标准网络协议,历史悠久并且被广泛使用。但随着业务规模和文件体量的快速增长,在应用实践中,FTP存在一些安全和效率问题。因此政府单位需要可以平滑替代FTP的文件传输解决方案,从而更好的保障数据安全、提升工作效率。具…

Ftrans跨域文件传输方案,数据流动无阻的高效路径!

大型集团企业由于其规模庞大、业务广泛且往往将分支机构、办事处分布在多个地域,因此会涉及到跨域文件传输的需求。主要源于以下几个方面: 1.业务协同:集团内部的不同部门或子公司可能位于不同的地理位置,但需要进行紧密的业务协同。文件传输是实现这种协同的重要方式之一,…

【详解】网络隔离环境下,跨网文件交换存在的核心问题及解决方法

一、为什么要进行网络隔离? 随着互联网技术的发展和推进,人们对于网络使用的场景也越来越多元化,而网络黑客入侵、病毒攻击、网络泄密等安全事件的不断发生,也让人们认识到网络安全的重要性。企业、社会、国家机构在日常经营管理中,由于业务和数据的保密和安全需求,需要对…

明厨亮灶视频监控分析系统

明厨亮灶视频监控分析系统依据“大数据技术明厨亮灶”在基本建设形式上,能够对后厨房的主要地区开展集中统一的视频监控。明厨亮灶视频监控分析系统能够随时随地查看食堂后厨房的监控界面。在厨房工作人员的行为方面,可以识别厨师帽厨师工作服查验,不戴口罩识别,餐厅厨房抽…

未穿厨师服厨师帽穿戴识别检测系统

未穿厨师服厨师帽穿戴识别检测系统适用厨房餐厅的全部关键位置。依据人工智能技术和图像识别优化算法,全天候实时监控餐厅厨房和厨师的整个操作过程。它能够识别厨房内有没有老鼠,厨师不穿厨师服,不佩戴口罩,不戴厨师帽,不戴手套,抽烟,玩手机等,一旦检测识别到上述情况…

Javaweb-JDBC快速如门

package JDBC; import java.sql.*; public class JDBCDemo { public static void main(String[] args) throws Exception { //1.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //2.获取连接 String url = "jdbc:mysql://127.0.0.1:3306/bookmanagement&…

秸秆禁烧识别系统 烟雾智能识别检测系统

秸秆禁烧识别系统烟雾智能识别检测系统具备全天、及时发现问题、管控高效率、有益于证据收集的优势,利用现场已经存在的监控摄像头和秸秆禁烧识别系统烟雾智能识别检测系统,在通信基站塔顶端监控摄像头的帮助下,对图像预置开展实时分析查验,能够积极主动识别户外违法焚烧处…

工地反光衣识别检测系统

工地反光衣识别检测系统运用智能视频分析和神经网络算法,进行工程施工区工作员是不是穿反光衣开展实时分析识别、追踪和警报,马上依据短视频实时分析和预警提醒,不穿反光衣危险行为及时预警提醒,警报截屏和视频储存到数据库系统系统生成表格,与此同时向有关人员消息推送报…