Docker概述

Docker为什么会出现?

需求场景
原来:
场景1:一套产品包括开发-测试-上线三套环境,环境配置,应用配置;
场景2:开发----运维问题:在我们电脑上可以运行,但是测试环境或上线会出现多个问题;
场景3:开发者开发生成iar,运维做部署,人力资源浪费;
场景4:接口冲突问题,特备是分布式、微服务;
场景5:系统不同,比如window系统开发的应用,发布到linux系统上。

招聘信息: ngnix,redis,mysql,tomcat,nacos,Elasticsearch+kibana, mycat
现在:
Doker来了
开发-测试-运营一套环境配置,解决原来场景1到5;
集装箱形式,两两集装箱不影响,解决场景4。

Docker的历史

2010年:
几个搞IT的年轻人,在美国成立了一家公司 dotcloud,dotCloud是平台即服务 (paas)的提供商,采用付费模式,底层技术是基于Linux的LXC容器技术。

技术很厉害,用Google的Go语言开发了一套内部工具,后被称为Docker。

2013年3月,经营不下去了,技术开源;很多人加入这个社区,Docker开始快速被大家接受
2014年,Docker1.0发布,从此开始疯狂扩张,风靡全球。
https://docs.docker.com (文档)
https://hub.docker.com (仓库)

Docker能做什么?

Docker的定义
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接。

Accelerate how you build, share and runmodern applications


虚拟机技术

Docker技术

Docker与虚拟机的不同

传统虚拟机:虚拟出一套硬件,运行一个完整的操作系统,在这个操作系统上安装和运行软件
docker:容器内的应用,直接运行在宿主机的内核上,容器没有内核,也没有虚拟硬件。容器间相互隔离,每个容器都有自己的文件系统,互不影响。

docker的优点

1.节省空间
2.应用更恢捷的安装和部署
传统:一堆的帮助文档,安装程序,配置文档。
Docker:打包镜像,一键安装测试。
3.更便捷的升级和扩缩容
使用了Docker后,我们部署就像搭积木一样
项目打包为一个镜像,扩展服务器A,服务器B
4.更简单的系统运维
使用Docker后,开发、测试、上线环境是一致的。
5.更高效的计算资源利用
Docker是内核级别的虚拟化。可以在一台物理机上运行多个容器实例,服务器的性能可以被压缩到极致。

Docker的基本组成


镜像(lmage): Docker 镜像是用于创建 Docker 容器的模板。
容器(Container): 镜像 (lmage) 和容器 (Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
仓库 (Repository) :
仓库可看成一个代码控制中心,用来保存镜像
分为公有和私有
默认仓库在国外,可以设置国内的镜像加速

什么是守护进程?

守护进程(Daemon Process),也就是通常说的 Daemon 进程(精灵进程),是Linux 中的后台服务进程。它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件。
守护进程是个特殊的孤儿进程,这种进程脱离终端,为什么要脱离终端呢?之所以脱离于终端是为了避免进程被任何终端所产生的信息所打断,其在执行过程中的信息也不在任何终端上显示。由于在 Linux 中,每一个系统与用户进行交流的界面称为终端,每一个从此终端开始运行的进程都会依附于这个终端,这个终端就称为这些进程的控制终端,当控制终端被关闭时,相应的进程都会自动关闭。

Docker安装

安装准备

  1. 需要有一台服务器,最好是阿里云的,一点点的Linux基础;
  2. centos7,Ubuntu, Debian, Gentoo;
  3. window使用xshell作为终端,mac使用nuoshell,感觉挺好用的
  4. 打开帮助文档(https://docs.docker.com/engine/install/)

Install Docker Engine on CentOS

Uninstall old versions

sudo yum remove docker
docker-client
docker-client-latest
docker-common
docker-latest
docker-latest-logrotate
docker-logrotate
docker-engine

Installation methods

您可以根据需要以不同的方式安装 Docker Engine

Set up the repository

在新主机上首次安装 Docker Engine 之前,您需要设置 Docker 存储库。之后,您可以从存储库安装和更新 Docker。

Install using the rpm repository

安装 yum-utils 包(它提供 yum-config-manager 实用程序)并设置存储库。

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Install Docker Engine

  1. Install Docker Engine, containerd, and Docker Compose:

sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

  1. Start Docker.

sudo systemctl start docker

  1. Verify that the Docker Engine installation is successful by running the hello-world image.

sudo docker run hello-world

运行hello-world的执行流程

Docker的常用命令

docker的常用命令,下面是两个文档地址:
官方文档(学习):https://docs.docker.com/reference/
命令大全(工具):https://www.runoob.com/docker/docker-command-manual.html

应用场景

三种身份 (角色) :
开发人员,能使用常用命令,了解命令运行原理.
运维人员:掌握常用命令是基础,能进行复杂的业务配置,精通测试环境集群部署、产品环境集群部署。(k8s)
开发经理(技术管理):能使用常用命令,Dockerfile,docker-compose,docker swarm,掌握各种应用场景的配置原理和对应的命令

镜像命令

Command Description
docker image build Build an image from a Dockerfile
docker image history Show the history of an image
docker image import Import the contents from a tarball to create a filesystem image
docker image inspect Display detailed information on one or more images
docker image load Load an image from a tar archive or STDIN
docker image ls List images
docker image prune Remove unused images
docker image pull Download an image from a registry
docker image push Upload an image to a registry
docker image rm Remove one or more images
docker image save Save one or more images to a tar archive (streamed to STDOUT by default)
docker image tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

常用命令

attach Attach to a running container # 当前 shell 下 attach 连接指定运行镜像

build Build an image from a Dockerfile # 通过 Dockerfile 定制镜像

commit Create a new image from a container changes # 提交当前容器为新的镜像

cp Copy files/folders from the containers filesystem to the host path #从容器中拷贝指定文件或者目录到宿主机中

create Create a new container # 创建一个新的容器,同 run,但不启动容器

diff Inspect changes on a container’s filesystem # 查看 docker 容器变化

events Get real time events from the server # 从 docker 服务获取容器实时事件

exec Run a command in an existing container # 在已存在的容器上运行命令

export Stream the contents of a container as a tar archive # 导出容器的内容流作为一个 tar 归档文件[对应 import ]

history Show the history of an image # 展示一个镜像形成历史

images List images # 列出系统当前镜像

import Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export]

info Display system-wide information # 显示系统相关信息

inspect Return low-level information on a container # 查看容器详细信息

kill Kill a running container # kill 指定 docker 容器

load Load an image from a tar archive # 从一个 tar 包中加载一个镜像[对应 save]

login Register or Login to the docker registry server # 注册或者登陆一个 docker 源服务器

logout Log out from a Docker registry server # 从当前 Docker registry 退出

logs Fetch the logs of a container # 输出当前容器日志信息

port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口对应的容器内部源端口

pause Pause all processes within a container # 暂停容器

ps List containers # 列出容器列表

pull Pull an image or a repository from the docker registry server # 从docker镜像源服务器拉取指定镜像或者库镜像

push Push an image or a repository to the docker registry server # 推送指定镜像或者库镜像至docker源服务器

restart Restart a running container # 重启运行的容器

rm Remove one or more containers # 移除一个或者多个容器

rmi Remove one or more images # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]

run Run a command in a new container # 创建一个新的容器并运行一个命令

save Save an image to a tar archive # 保存一个镜像为一个 tar 包[对应 load]

search Search for an image on the Docker Hub # 在 docker hub 中搜索镜像

start Start a stopped containers # 启动容器

stop Stop a running containers # 停止容器

tag Tag an image into a repository # 给源中镜像打标签

top Lookup the running processes of a container # 查看容器中运行的进程信息

unpause Unpause a paused container # 取消暂停容器

version Show the docker version information # 查看 docker 版本号

wait Block until a container stops, then print its exit code # 截取容器停止时的退出状态值

Docker镜像解读

Docker 镜像加载原理


第一层: bootfs(boot file system): 主要包含bootloader和kernel,bootloader 主要是引导加载kernel,Linux刚启动时会加载bootfs文件系统,在Docker镜像的最底层是bootfs。这一层与我们典型的Linux/Unix系统是一样的,包含boot加载器和内核。当boot加载完成之后整个内核就存在内存中了,此时内存的使用权已由bootfs转交
给内核,此时系统也会卸载bootfs。
第二层: Base lmage,rootfs (root file system),包含的就是典型Linux系统中的/dev,/proc, /bin,/etx等标准的目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu,Centos等等。
第三层: 集成开发环境emocs;
第四层: 我们的应用镜像层,比如mysql、tomcat镜像层。

镜像的定义

Docker images are the basis of containers, An lmage is an ordered collection of root filesystem changesand the corresponding execution parameters for use within a container runtime. An image typicallycontains a union of layered filesystems stacked on top of each other. An image does not have state and itnever changes.

  1. Docker images are the basis of containers.使用: 容器的基础
  2. An lmage is an ordered collection of root filesystem changes and the corresponding executionparameters for use within a container runtime.内容: 命令集合,文件系统变化和对应在容器运行时用到的执行参数。
  3. An image typically contains a union of layered filesystems stacked on top of each other.框架: 分层联合文件系统堆砌。
  4. An image does not have state and it never changes.特点: 无状态、不能改变。

    如何得到镜像
  5. 从远程仓库;hub.docker.com
  6. 制作镜像Dockerfile;

UnionFS(联合文件系统)

Union文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,他支持对文件系统的修改作为一次提交来层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下 (unite several directories into a singlevirtual filesystem)。Union文件系统是Docker镜像的基础。镜像可以通过分层来进行集成,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层文件和目录。

Docker 镜像为什么分层

镜像分层最大的一个好处就是共享资源,提升效率
比如说有多个镜像都从相同的 base 镜像构建而来,那么 Docker Host 只需在磁盘上保存一份base 镜像;同时内存中也只需加载一份 base 镜像,就可以为所有容器服务了。而且镜像的每一层都可以被共享。
如果多个容器共享一份基础镜像,当某个容器修改了基础镜像的内容,比如 /etc 下的文件,这时其他容器的/etc 是不会被修改的,修改只会被限制在单个容器内。这就是容器Copy-on-Write 特性。


docker 镜像都是只读的,当容器启动时,一个新的可写层被加载到镜像的顶部。这一层通常被称作“容器层“容器层”之下的都叫镜像层。

生成一个镜像

对容器进行修改,使用docker commit 命令提交修改后的容器,生成新的镜像

#docker commit 提交容器为一个新的镜像
docker commit -m="描述信息” -a="作者”容器id 镜像名称:tag
https://docs.docker.com/engine/reference/commandline/commit/

容器数据卷

什么是Docker数据卷

需求场景:
场景1.run mysql容器,增删改数据后,删除mysql的容器,但是要把数据保存下来,怎么办?

场景2.mysql容器配置文件更新,docker如何处理最简单?

数据卷的定义
A volume is a specially-designated directory within one or more containers that byxpasses the Union FileSystem. Volumes are designed to persist data, independent of the container’s life cycle. Docker thereforenever automatically deletes volumes when you remove a container, nor will it “garbage collect” volumesthat are no longer referenced by a container. Also known as: data volume
目录,绕过联合文件系统,持久化数据,与容器生命周期无关

容器与宿主机数据同步的一个对照目录

三种类型数据卷
There are three types of volumes: host anonymous, and named:

  • A host volume lives on the Docker host’s filesystem and can be accessed from within the container.
  • A named volume is a volume which Docker manages where on disk the volume is created, but it isgiven a name.
  • An anonymous volume is similar to a named volume, however, it can be difficult, to refer to the samevolume over time when it is an anonymous volumes. Docker handle where the files are stored.
    为了容器的持久化和同步操作!也支持容器间数据共享。

    https://docs.docker.com/storage/volumes/

mysql数据卷


具名挂载和匿名挂载

具名挂载

匿名挂载


所有的卷没有指定主机路径的情况下,都是在/var/lib/docker/volumes
通过卷名可以很方便的找到一个容器卷,通常使用具名挂载

#如何分辨匿名挂载、具名挂载、指定路径挂载
-v 容器内路径 #匿名挂载
-v 具名: #具名挂载
-v 主机路径/容器内路径 #指定路径挂载

拓展

通过 -v 容器内路径: ro rw 改变容器内的读写权限
ro只读,只能通过宿主机进行改变
rw 可读可写
docker run -d -p --name nqinx002 -y juming nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx002 -v juming nginx:/etc/nginx:rw nginx

如果我们想同步两个容器中的目录?

数据卷容器

nginx002

nginx001

多个mysql实现数据共享

docker run -d -it -p 3304:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7

docker run -d -it -p 3305:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7

这时可以实现数据同步

容器数据卷的生命周期一直持续到没有容器使用为止。
容器数据卷一旦持久化到本地,这时本地数据是不会被删除的。

DockerFile

如何构建自己的镜像?
https://docs.docker.com/engine/reference/builder/
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

DockerFile介绍

Docker can build images automatically by reading the instructions from a Dockerfile . A Dockerfile is atext document that contains all the commands a user could call on the command line to assemble animage. Using docker build users can create an automated build that executes several command-lineinstructions in succession.

Docker可以通过读取Dockerfile中的指令来自动构建镜像。Dockerfile是一个文本文档,其中包含用户可以调用命令行组装镜像的所有命令。使用“docker build",用户可以生成一个自动构建,该生成连续执行多个命令行指令。

dockerfile是用来构建镜像的命令行文本文件
如何使用dockerfile:

  1. 编写一个dockerfile文件
  2. docker build 构建一个镜像
  3. docker run 镜像
  4. doker push 可以发布镜像 (Docker Hub,阿里云镜像仓库)
    可以看到Centos的编码

DockerFile的构建过程

  1. 每个保留关键字(指令),不区分大小写,但是为了方便阅读,通常都是大写;
    The instruction is not case-sensitive. However, convention is for them to be UPPERCASE to distinguish themfrom arguments more easily.
  2. 都是从FROM 命令开始
  3. 从上到下执行指令;
  4. #表示注释
  5. 每一个指令都会提交一个新的镜像层;

Docker执行Dockerfile的大致流程

(1)docker从基础镜像运行一个容器
(2)执行一条指令并对容器作出修改
(3)执行类似docker commit的操作提交一个新的镜像层
(4)docker再基于刚提交的镜像运行一个新容器
(5)执行dockerfile中的下一条指令直到所有指令都执行完成


从应用软件的角度来看,Dockerfile、Docker镜像与Docker容器分别代表软件的三个不同阶段,

  • Dockerfile是软件的原材料
  • Docker镜像是软件的交付品
  • Docker容器则可以认为是软件镜像的运行态,也即依照镜像运行的容器实例
    Dockerfile面向开发,Docker镜像成为交付标准,Docker容器则涉及部署与运维,三者缺一不可,合力充当Docker体系的基石。
  • Dockerfile,需要定义一个Dockerfile,Dockerfile定义了进程需要的一切东西。Dockerfile涉及的内容包括执行代码或者是文件、环境变量、依赖包、运行时环境、动态链接库、操作系统的发行版、服务进程和内核进程(当应用进程需要和系统服务和内核进程打交道,这时需要考虑如何设计namespace的权限控制)等等;
  • Docker镜像,在用Dockerfile定义一个文件之后,docker build时会产生一个Docker镜像,当运行 Docker镜像时会真正开始提供服务;
  • Docker容器,容器是直接提供服务的。

dockerfile的指令

FROM 基础镜像,当前新镜像是基于哪个镜像的,指定一个已经存在的镜像作为模板,第一条必须是from
MAINTAINER(过时) 镜像维护者的姓名和邮箱地址MAINTAINER(被LABEL替代)
RUN
容器构建时需要运行的命令
RUN是在 docker build时运行
两种格式:

shell格式:RUN yum -y install vim

exec格式

EXPOSE 当前容器对外暴露出的端口
WORKDIR 指定在创建容器后,终端默认登陆的进来工作目录,一个落脚点
USER 指定该镜像以什么样的用户去执行,如果都不指定,默认是root
ENV 用来在构建镜像过程中设置环境变量

ENV MY_PATH /usr/mytest
这个环境变量可以在后续的任何RUN指令中使用,这就如同在命令前面指定了环境变量前缀一样;
也可以在其它指令中直接使用这些环境变量,

比如:WORKDIR $MY_PATH

ADD 将宿主机目录下的文件拷贝进镜像且会自动处理URL和解压tar压缩包
COPY 类似ADD,拷贝文件和目录到镜像中。 将从构建上下文目录中 <源路径> 的文件/目录复制到新的一层的镜像内的 <目标路径> 位置

COPY src dest
COPY [“src”, “dest”]
<src源路径>:源文件或者源目录
<dest目标路径>:容器内的指定路径,该路径不用事先建好,路径不存在的话,会自动创建。

VOLUME 容器数据卷,用于数据保存和持久化工作
CMD
指定容器启动后的要干的事情

注意

Dockerfile 中可以有多个 CMD 指令,但只有最后一个生效,CMD 会被 docker run 之后的参数替换

它和前面RUN命令的区别

CMD是在 docker run 时运行。
RUN是在 docker build 时运行。

ENTRYPOINT
也是用来指定一个容器启动时要运行的命令
类似于 CMD 指令,但是ENTRYPOINT不会被docker run后面的命令覆盖, 而且这些命令行参数会被当作参数送给ENTRYPOINT 指令指定的程序
命令格式和案例说明

命令格式:
ENTRYPOINT可以和CMD一起用,一般是变参才会使用 CMD ,这里的 CMD 等于是在给 ENTRYPOINT 传参。
当指定了ENTRYPOINT后,CMD的含义就发生了变化,不再是直接运行其命令而是将CMD的内容作为参数传递给ENTRYPOINT指令,他两个组合会变成
案例如下:假设已通过 Dockerfile 构建了 nginx:test 镜像:

是否传参 按照dockerfile编写执行 传参运行
Docker命令 docker run nginx:test nginx:test -c /etc/nginx/new.conf
衍生出的实际命令 nginx -c /etc/nginx/new.conf nginx -c /etc/nginx/new.conf

优点
在执行docker run的时候可以指定 ENTRYPOINT 运行所需的参数
注意
如果 Dockerfile 中如果存在多个 ENTRYPOINT 指令,仅最后一个生效。
总结

Docker网络原理

应用场景:

docker网络基础

清空容器和镜像

docker rm -f $(docker ps -aq)
docker rmi -f $(docker images -aq)

分析主机当前ip地址

[rootqizm5ej9s0zijym2fkmgsdxz /]# ip addr
#lo:local的简写,本机回环地址,127.0.0.1,通常被称为本地回环地址(Loopback Address),不属于任何一个有类别地址类。它代表设备的本地虚拟接口,所以默认被看作是永远不会宕掉的接口
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever#etho: ethernet的简写,一般用于以太网接口。阿里云的内网地址
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:16:3e:06:5e:42 brd ff:ff:ff:ff:ff:ffinet 172.25.225.70/20 brd 172.25.239.255 scope global dynamic noprefixroute eth0valid_lft 312659482sec preferred_lft 312659482sec#docker生成网卡的地址
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:a9:08:e4:5b brd ff:ff:ff:ff:ff:ffinet 172.17.0.1/16 brd 172.17.255.255 scope global docker0valid_lft forever preferred_lft foreverinet6 fe80::42:a9ff:fe08:e45b/64 scope link valid_lft forever preferred_lft forever

docker0容器网络、宿主机互联

1.查看容器和宿主机的ip地址
ip addr命令查看
2.容器间的互ping
3.容器与宿主机互 ping,可以ping通
tomca02:172.17.0.2
tomca02:172.17.0.3

docker0桥接模式原理

1.只要安装docker,就会有一个桥接模式的docker0网卡,采用evth-pair技术。每启动一个容器就会通过docker0网卡,分配一个ip地址。
2.再启动一个容器进行测试
发现又多了一个网卡地址

总结
我们发现docker容器带来的网卡都是一对一对的
evth-pair就是一对虚拟接口,一端连着协议,一端彼此连接
因为有这个特性,evth-pair作为一个桥梁,链接各种虚拟设备
openstack,docker容器间的链接,ovs链接,都是使用evth-pair技术

小结
docker使用的是linux的桥接,docker中所有的网络接口都是虚拟的


只要容器删除,对应的ip被删除。

docker的网络驱动类型

https://docs.docker.com/network/

docker 网络驱动类型示意图

Network drivers

  • Docker’s networking subsystem is pluggable, using drivers. Several drivers exist by default, and provide core networking functionality:
  • bridge: The default network driver. If you don’t specify a driver, this is the type of network you are creating. Bridge networks are usually used when your applications run in standalone containers that need to communicate. See bridge networks.
  • host: For standalone containers, remove network isolation between the container and the Docker host, and use the host’s networking directly. See use the host network.
  • overlay: Overlay networks connect multiple Docker daemons together and enable swarm services to communicate with each other. You can also use overlay networks to facilitate communication between a swarm service and a standalone container, or between two standalone containers on different Docker daemons. This strategy removes the need to do OS-level routing between these containers. See overlay networks.
  • ipvlan: IPvlan networks give users total control over both IPv4 and IPv6 addressing. The VLAN driver builds on top of that in giving operators complete control of layer 2 VLAN tagging and even IPvlan L3 routing for users interested in underlay network integration. See IPvlan networks.
  • macvlan: Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network. The Docker daemon routes traffic to containers by their MAC addresses. Using the macvlan driver is sometimes the best choice when dealing with legacy applications that expect to be directly connected to the physical network, rather than routed through the Docker host’s network stack. See Macvlan networks.
  • none: For this container, disable all networking. Usually used in conjunction with a custom network driver. none is not available for swarm services. See disable container networking.
  • Network plugins: You can install and use third-party network plugins with Docker. These plugins are available from Docker Hub or from third-party vendors. See the vendor’s documentation for installing and using a given network plugin.

Network driver summary(摘要)

  • User-defined bridge networks are best when you need multiple containers to communicate on the same Docker host.
  • Host networks are best when the network stack should not be isolated from the Docker host, but you want other aspects of the container to be isolated.
  • Overlay networks are best when you need containers running on different Docker hosts to communicate, or when multiple applications work together using swarm services.
  • Macvlan networks are best when you are migrating from a VM setup or need your containers to look like physical hosts on your network, each with a unique MAC address.
  • Third-party network plugins allow you to integrate Docker with specialized network stacks.

自定义网络的创建(bridge)

driver bridge
subnet 192.168.0.0/16
–gateway 192.168.0.1

[root@1zm5ej9s021ym2kmgsdxz /]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet 48e5c10758b94207294107335076d421d6e1d42519238753c00ad14090cda29c
[root@iZ2ze8irww043ixo6030igZ ~]# docker network ls
NETWORK ID          NAME                   DRIVER              SCOPE
40318bb9b63d        bridge                 bridge              local
b28cfc68711a        composetest_default    bridge              local
fe3497ab1648        host                   host                local
3adb61f83ad5        my_wordpress_default   bridge              local
b3ecd64c7a0d        mynet                  bridge              local

查看mynet

[root@iZ2ze8irww043ixo6030igZ ~]# docker network inspect mynet
[{"Name": "mynet","Id": "b3ecd64c7a0d60aa6c0dfebf439c3438cbb9343a6dcf2dd9783693e09353164a","Created": "2023-05-07T20:50:12.898561355+08:00","Scope": "local","Driver": "bridge","EnableIPv6": false,"IPAM": {"Driver": "default","Options": {},"Config": [{"Subnet": "192.168.0.0/16","Gateway": "192.168.0.1"}]},"Internal": false,"Attachable": false,"Ingress": false,"ConfigFrom": {"Network": ""},"ConfigOnly": false,"Containers": {},"Options": {},"Labels": {}}
]

docker单机部署redis集群

docker网络实战演练
构建集群场景:六个redis,三主三备,了解集群发展历史
单机、主从、哨兵、高可用

Docker Compose(集群编排)

了解docker compose

https://docs.docker.com/compose/

#定义:定义,运行多docker应用工具
Compose is a tool for defining and running multi-container Docker applications.
#工作原理
With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
#应用范围
Compose works in all environments: production, staging, development, testing, as well as CI workflows. It also has commands for managing the whole lifecycle of your application:

使用docker-compose三个步骤

Using Compose is basically a three-step process:
1.Define your app’s environment with a Dockerfile so it can be reproduced anywhere.。

  • 通过Dockerfile定义你的应用环境,保证能在任何环境运行。
    2.Define the services that make up your app in docker-compose.yml so they can be run together in anisolated environment.
  • 通过Docker-compose.yml定义构成你应用的各种服务,使他们可以在孤立的环境中运行。
    3.Run docker compose up and the Docker compose command starts and runs your entire app. You canalternatively run docker-compose up using the docker-compose binary.
  • 使用docker compose up,docker compose命令启动并运行整个应用程序。也可以使用docker compose二进制文件运行docker compose up。

应用场景

场景描述: 上线微服务应用,注册配置中心、网关、认证模块、系统业务、redis、mysql、ELK、filesystem版本更新,服务升级,关联服务重启
原来:

1.编写Dockerfile
2.docker build 生成镜像
3.docker run 镜像

现在
用docker compose工具,定义和运行多个容器应用,轻松高效的管理多个容器。
docker-compose.yml

version: "3.9"
services:web:build: .ports:- "8000:5000"volumes:- .:/codeenvironment:FLASK_DEBUG: "true"redis:image: "redis:alpine"

安装

运行以下命令以下载 Docker Compose 的当前稳定版本:

sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

实战

https://docs.docker.com/compose/gettingstarted/

compose.yaml构建

docker-compose.yml的核心命令文档
官方文档: https://docs.docker.com/compose/compose-file/
中文文档: https://www.runoob.com/docker/docker-compose.html

yaml文件结构

#共三层
第一层
version:"3.8"#版本第二层
services: #服务
服务1:
#服务配置
服务2:第三层,其他配置、网络卷、全局规则
volumes:
network:
config:

build(构建镜像)

指定为构建镜像上下文路径:
例如 webapp 服务,指定为从上下文路径 ./dir/Dockerfile 所构建的镜像:

version: "3.7"
services:webapp:build: ./dir

或者,作为具有在上下文指定的路径的对象,以及可选的 Dockerfile 和 args:

version: "3.7"
services:webapp:build:context: ./dirdockerfile: Dockerfile-alternateargs:buildno: 1labels:- "com.example.description=Accounting webapp"- "com.example.department=Finance"- "com.example.label-with-empty-value"target: prod
  • context:上下文路径。
  • dockerfile:指定构建镜像的 Dockerfile 文件名。
  • args:添加构建参数,这是只能在构建过程中访问的环境变量。
  • labels:设置构建镜像的标签。
  • target:多层构建,可以指定构建哪一层。

depends_on

设置依赖关系。

  • docker-compose up :以依赖性顺序启动服务。在以下示例中,先启动 db 和 redis ,才会启动 web。
  • docker-compose up SERVICE :自动包含 SERVICE 的依赖项。在以下示例中,docker-compose up web 还将创建并启动 db 和 redis。
  • docker-compose stop :按依赖关系顺序停止服务。在以下示例中,web 在 db 和 redis 之前停止。
version: "3.7"
services:web:build: .depends_on:- db- redisredis:image: redisdb:image: postgres

注意:web 服务不会等待 redis db 完全启动 之后才启动。

expose

暴露端口,但不映射到宿主机,只被连接的服务访问。
仅可以指定内部端口为参数:

expose:- "3000"- "8000"

networks

配置容器连接的网络,引用顶级 networks 下的条目 。

services:some-service:networks:some-network:aliases:- alias1other-network:aliases:- alias2
networks:some-network:# Use a custom driverdriver: custom-driver-1other-network:# Use a custom driver which takes special optionsdriver: custom-driver-2

aliases :同一网络上的其他容器可以使用服务名称或此别名来连接到对应容器的服务。

deploy

指定与服务的部署和运行有关的配置。只在 swarm 模式下才会有用。

version: "3.7"
services:redis:image: redis:alpinedeploy:mode:replicatedreplicas: 6endpoint_mode: dnsrrlabels: description: "This redis service label"resources:limits:cpus: '0.50'memory: 50Mreservations:cpus: '0.25'memory: 20Mrestart_policy:condition: on-failuredelay: 5smax_attempts: 3window: 120s

entrypoint

覆盖容器默认的 entrypoint。

entrypoint: /code/entrypoint.sh

也可以是以下格式:

entrypoint:- php- -d- zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so- -d- memory_limit=-1- vendor/bin/phpunit

command

覆盖容器启动的默认命令。

command: ["bundle", "exec", "thin", "-p", "3000"]

多看官方文档,多写docker-compose.yml
官方文档: https://docs.docker.com/compose/compose-file/
中文文档: https://www.runoob.com/docker/docker-compose.html

Docker Swarm (集群管理)

Docker Swarm(简单k8s)

官方文档:https://docs.docker.com/engine/swarm/
官方文档:https://docs.docker.com/get-started/swarm-deploy/
中文文档:https://www.runoob.com/docker/docker-swarm.html
应用场景:
互联网三高架构:高并发、高性能、高可用,简称三高 (3H)
那么,构建一个三高的系统,到底可以从哪些方面下手呢。
大概可以从以下方面入手:横向分层、纵向分割、分布式化、集群化(订单、用户访问、库存)、使用缓存、使用异步模式、使用冗余、自动化 (发布、部署、监控)。
具体来说,可以在不同层次常用的技术有:
前端

  • 浏览器优化技术:合理布局,页面缓存,减少http请求数,页面压缩,减少 cookie 传输
  • CDN
  • DNS负载均衡
  • 动静分离0
  • 动态图片独立提供服务0
  • 反向代理.
    应用层架构
  • 业务拆分
  • 负载均衡
  • 虚拟化服务器、容器化
  • 无状态 (以及分布式 Session)
  • 分布式缓存
  • 异步、事件驱动架构、消息队列
  • 多线程
  • 动态页面静态化
    服务层架构
  • 分布式微服务 (分级管理,超时设置,异步调用,服务降级,幂等性设计。)
  • 同应用层架构
    存储层架构
  • DFS
  • 关系数据库路由
  • 数据同步
  • 数据冗余
    安全架构
  • Web攻击 (XSS、Sql Injection)
  • 数据加密
  • 密钥管理
    发布、运维
  • 自动化测试与发布
  • 灰度发布
  • 浏览器数据采集
  • 服务器业务数据采集
  • 服务器性能数据采集
  • 系统监控
  • 系统报警
    原理
    如下图所示,swarm 集群由管理节点(manager)和工作节点(work node)构成。
  • swarm mananger:负责整个集群的管理工作包括集群配置、服务管理等所有跟集群有关的工作。
  • work node:即图中的 available node,主要负责运行相应的服务来执行任务(task)。

Docker stack

官方文档:
docker-compose: https://docs.docker.com/compose/compose-file/compose-file-v3/
stack 命令: https://docs.docker.com/engine/reference/commandline/stack/
stack deploy:https://docs.docker.com/engine/swarm/stack-deploy/
工具文档:
https://www.runoob.com/docker/docker-compose.html
1.Stack 是基于 Docker Swarm 之上来完成应用的部署。
2.步骤很简单。在 Compose 文件中定义应用,然后通过 docker stack deploy 命令完成部署和管理。
Compose 文件中包含了构成应用所需的完整服务栈。此外还包括了卷、网络、安全以及应用所需的其他基础架构。然后基于该文件使用 docker stack deploy 命令来部署应用。

CICD(持续集成、持续交付和持续部署)

后续…

Docker实战课程大纲,快速掌握基础docker,swarm,stack相关推荐

  1. 视频教程-SEO零基础实战课程+SEO快速盈利-网络营销

    SEO零基础实战课程+SEO快速盈利 专注搜索引擎优化,付费SEM推广 蒋惠杰 ¥1980.00 立即订阅 扫码下载「CSDN程序员学院APP」,1000+技术好课免费看 APP订阅课程,领取优惠,最 ...

  2. 尚硅谷Docker实战教程-笔记02【安装docker、镜像加速器配置】

    尚硅谷大数据技术-教程-学习路线-笔记汇总表[课程资料下载] 视频地址:尚硅谷Docker实战教程(docker教程天花板)_哔哩哔哩_bilibili 尚硅谷Docker实战教程-笔记01[理念简介 ...

  3. (Docker实战)在CentOS7上使用Docker;(超详细、附图、附代码)

    一.Docker的三大组件 要想使用Docker,就要先了解Docker的一些基本概念,首先要了解的是Docker的三大组件,分别是仓库.镜像.容器: 仓库(也有叫Registry(注册中心)) Do ...

  4. docker实战学习2022版本(七)之docker网络学习

    查看docker网络模式命令:docker network ls   (默认创建三大网络模式) 常用基本命令: 查看网络源数据:docker network inspect  xxx网络名字 删除网络 ...

  5. Docker实战6 之构建自己的docker镜像

    2019独角兽企业重金招聘Python工程师标准>>> 1.构建自己的war 这里做测试,使用jpress 下载war包 https://gitee.com/fuhai/jpress ...

  6. 【H3C V7路由器实战课程系列-2】WAN接入配置与管理-王达-专题视频课程

    [H3C V7路由器实战课程系列-2]WAN接入配置与管理-643人已学习 课程介绍         全面.系统地介绍了H3C路由器主要的WAN接入技术及应用配置与管理方法,包括PPP.MP.DDR和 ...

  7. android 课程大纲,Android课程大纲解读.doc

    Android课程大纲解读 Android课程大纲 Android-Java基础(25天)Java开发环境搭建基础知识 JAVA环境的搭建 Hello World 小程序JAVA语法基础基本语法掌握关 ...

  8. 【宋红康 MySQL数据库】【00】课程大纲

    持续学习&持续更新中- 学习态度:守破离 [宋红康 MySQL数据库][00]课程大纲 MySQL数据库基础篇大纲 1. 数据库概述与MySQL安装篇 2. SQL之SELECT使用篇 3. ...

  9. 【2020】UI设计师学习路线(附:课程大纲+视频教程+设计素材+工具)

    什么是全链路UI设计 UI设计师是随着网络而兴起的新兴设计行业,从事对软件的人机交互.操作逻辑.界面美观的整体设计工作.涉及范围包括商用平面设计.高级网页设计.移动应用界面设计及跨媒介设计,以人为本, ...

最新文章

  1. 以太坊开启区块链2.0时代
  2. html转义字符对照表
  3. 数据挖掘系列(4)使用weka做关联规则挖掘
  4. Python3算法基础练习:编程100例(11 ~ 15)
  5. Python基础概念_2_基础概念
  6. android Context的使用
  7. 通知:即日起本博客暂停更新,请移步至yanxin8.com获取最新文章
  8. 关于单元测试的思考--Asp.Net Core单元测试最佳实践
  9. J2ME结构与相关规范介绍
  10. django缓存优化(一)
  11. HC蓝牙模块测试AT指令搭建外部电路遇到的问题
  12. linux安装mysql配置,linux安装mysql,配置mysql文件
  13. 华硕服务器主板安装系统提示驱动,华硕z590主板装win7系统及bios设置教程(支持11代cpu驱动)...
  14. Hibernate 学习的书-夏昕(1)
  15. Mac source环境变量配置
  16. π型滤波频率计算_π型电感滤波电路计算
  17. Butterworth低通滤波器 Matlab实现
  18. 亲民地理38期-江西极顶武功山(上)_我是亲民_新浪博客
  19. ubuntu分区时主分区和逻辑分区怎么分
  20. 企业为什么做不好生产计划?

热门文章

  1. C++-教程2-VS2010C++相关文件说明
  2. RFM分析 | 一招搞定精细化客户管理,盒马鲜生等企业都在用
  3. vue仿钉钉的考勤日历(基于vue-calendar-component组件)
  4. oracle执行视图的权游,VR、Ncam、Simulcam齐上阵!《权游8》重点镜头技术详解
  5. 【刷题日记】笔试经典编程题目(八)
  6. 12 背包问题求具体方案(零一背包问题求具体方案)
  7. 关于C语言中.h和.c文件
  8. Java中的枚举与values()方法
  9. TinyProxy简明教程
  10. NTSC与PAL制式的转换