docker学习笔记

Docker常用命令

docker group

curtis@curtis-FP650:/etc/docker$ cat /etc/group | grep docker
docker:x:138:curtis
  • docker 是一个用户组的名称。
  • x 表示该组的密码已经设置。
  • 138 是该组在系统中的组ID(GID)。
  • curtis 是属于 docker 组的一个用户。

更改组成员资格后,用户需要注销再重新登录或重启系统以使更改生效。

帮助命令

docker version	# 显示dockee的版本信息
docker info	# 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 帮助信息

帮助文档地址:Reference documentation | Docker Documentation

镜像命令

docker image # 查看本地主机上的镜像

root@curtis-Aspire-E5-471G:/var/lib/docker# docker images
REPOSITORY    TAG       IMAGE ID       CREATED	           SIZE
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB
root@curtis-Aspire-E5-471G:/var/lib/docker#

# 解释
REPOSITORY   镜像的仓库源
TAG			 镜像标签
IMAGE ID	 镜像的id
CREATED	     镜像的创建时间
SZIE         镜像的大小

# 可选项
Options:
  -a, --all             Show all images (default hides 
  -q, --quiet           Only show image IDs

docker search 搜索镜像

root@curtis-Aspire-E5-471G:/var/lib/docker# docker search mysql
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                            MySQL is a widely used, open-source relation…   12497     [OK]
mariadb                          MariaDB Server is a high performing open sou…   4810      [OK]
mysql/mysql-server               Optimized MySQL Server Docker images. Create…   925                  [OK]
percona                          Percona Server is a fork of the MySQL relati…   575       [OK]
phpmyadmin                       phpMyAdmin - A web interface for MySQL and M…   520       [OK]


# 可选项,通过收藏来过滤
	--filter=STARS=3000			搜索出来的镜像是STARS大于3000的镜像

docker pull 下载镜像

# 下载镜像 docker pull 镜像名[:tag]
root@curtis-Aspire-E5-471G:/var/lib/docker# docker pull mysql
Using default tag: latest	# 如果不写tag,默认是 latest
latest: Pulling from library/mysql
4be315f6562f: Pull complete	# 分层下载:docker image的核心,联合文件系统
96e2eb237a1b: Pull complete
8aa3ac85066b: Pull complete
ac7e524f6c89: Pull complete
f6a88631064f: Pull complete
15bb3ec3ff50: Pull complete
ae65dc337dcb: Pull complete
573c3c7fa18d: Pull complete
9d10771b98b8: Pull complete
3d8ef442614b: Pull complete
7dc17a6cea26: Pull complete
752752efdaea: Pull complete
Digest: sha256:2dafe3f044f140ec6c07716d34f0b317b98f8e251435abd347951699f7aa3904	# 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest	#真实地址

# 等价于
docker pull mysql
docker pull docker.io/library/mysql:latest

# 下载特定版本的镜像
root@curtis-Aspire-E5-471G:/var/lib/docker# docker pull mysql:5.7
5.7: Pulling from library/mysql
4be315f6562f: Already exists		# 分层文件已经存在
96e2eb237a1b: Already exists
8aa3ac85066b: Already exists
ac7e524f6c89: Already exists
f6a88631064f: Already exists
15bb3ec3ff50: Already exists
ae65dc337dcb: Already exists
a4c4c43adf52: Pull complete
c6cab33e8f91: Pull complete
2e1c4f2c43f6: Pull complete
2e5ee322af48: Pull complete
Digest: sha256:e767595ba3408fbb2dda493be3594b9a148178df58325fafe8b0363662935624
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

root@curtis-Aspire-E5-471G:/var/lib/docker# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       8aa4b5ffb001   3 days ago     462MB
mysql         latest    96d0eae5ed60   3 days ago     524MB
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB

docker rmi 删除镜像!

root@curtis-Aspire-E5-471G:/var/lib/docker# docker rmi -f 8aa4b5ffb001 # 删除指定的容器
Untagged: mysql:5.7
Untagged: mysql@sha256:e767595ba3408fbb2dda493be3594b9a148178df58325fafe8b0363662935624
Deleted: sha256:8aa4b5ffb001f0092b491bdd8a48a83cb2ab2b721646db139d4cda64dea93600
Deleted: sha256:3c10adc5e3d6244bf3150fdbd5113dc61c7185066c5429df7f035cff76ea2dc3
Deleted: sha256:92a595964a3c30430b163e918640a9f2f47037cbab744866fa628d4096427e8a
Deleted: sha256:d4fb472f4bc83f4c024871ef9ab0e7533995cd25ba6c57d2272095edc894a685
Deleted: sha256:dc33ff6f21a8069a22f7176d1cc1fb7895af3f219b482c66a83073b9c140c3b6
root@curtis-Aspire-E5-471G:/var/lib/docker# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    96d0eae5ed60   3 days ago     524MB
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB
root@curtis-Aspire-E5-471G:/var/lib/docker# docker rmi -f $(docker images -aq) #删除所有
Untagged: mysql:latest
Untagged: mysql@sha256:2dafe3f044f140ec6c07716d34f0b317b98f8e251435abd347951699f7aa3904
Deleted: sha256:96d0eae5ed6069320a16ec1029b7378e330c31473bb7ba3027578c7c582c0076
Deleted: sha256:8b3106bcf588d8eb5b468b421e9de2559d51bfb4314c084a4e8d26a88ac0f28b
Deleted: sha256:a3d5651d373109abedaedd2a84bbdcdfc2efd18fc5ddc977365abfb11613dba5
Deleted: sha256:9eb4060a534852a3f9958111349521c81660d385bafbe88cd41acc5843c088ad
Deleted: sha256:064eedb664a6ec1774dd7b8e1e2d0696136b2d17004544d78888aa3db273ea23
Deleted: sha256:56def78a8b700366329f8fd6e5199c2d459f370630f0a6c537e54fa279dd8cee
Deleted: sha256:625e297c59e84d9cb5ae0dcd69714dcc7f3c823c281cce1616374232057c8fa9
Deleted: sha256:52994d077831159b79c473125916eb6874cebf38493e3046abe80935e181d8af
Deleted: sha256:9550bb960b1438abbdc93a0a5518055f4a455bbd65491e53fa795960ddf518a5
Deleted: sha256:d3dee54c8b55b10f9fe4100dd1fa6e59f98dbfbf84b5757f518d874f57acd1ec
Deleted: sha256:49ff85d13492ccda474015e5e7101f35e8cee2f4b23d2385195d8a897b4be097
Deleted: sha256:7310671d54d99fba70b7191bca28f8e9a4b5438c4b012a2ceca11cae17d3177b
Deleted: sha256:0ad3ddf4a4ce0fc9d65021897541c99e5497a11ced2419016147267df61732d3
Untagged: hello-world:latest
Untagged: hello-world@sha256:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412

容器命令

说明:我们有了镜像才可以创建容器,linux,下载一个 centos 镜像

root@curtis-Aspire-E5-471G:/var/lib/docker# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Downloading
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

新建容器并启动
docker run --name my_container -dit my_image:tag /bin/bash

docker run [可选参数] image
    
# 参数说明
--name="Nmae"   # 容器名字 tomacat01 tomacat02,用来区分容器
-d				# 后台方式运行
-it				# 使用交互方式运行,进入容器查看内容
-p				# 指定容器端口 -p 8080:8080
    -p:ip:主机端口:容器端口
    -p:主机端口:容器端口
    -p:容器端口
    容器端口
-P				# 随机指定端口

# 启动并进入容器
root@curtis-Aspire-E5-471G:/var/lib/docker# docker run -it centos /bin/bash
[root@17bcc3e3421f /]# ls	# 查看容器内的centos,基础版本
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 从容器中退回主机
[root@17bcc3e3421f /]# exit
exit
root@curtis-Aspire-E5-471G:/var/lib/docker# docker ps -a

列出所有运行的容器

# docker ps 命令
	# 当前在运行的容器
-a	# 累出当前重在运行的容器 + 历史运行过的容器
-n=?	# 显示最近运行的容器
-q	# 只显示容器的编号

root@curtis-Aspire-E5-471G:/var/lib/docker# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                            PORTS     NAMES
17bcc3e3421f   centos         "/bin/bash"   4 minutes ago   Exited (127) About a minute ago             stoic_driscoll
4e565fc5cbaa   feb5d9fea6a5   "/hello"      4 hours ago     Exited (0) 4 hours ago                      inspiring_morse
root@curtis-Aspire-E5-471G:/var/lib/docker# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

退出容器

exit	# 容器停止并退出
ctl + P + Q # 容器不停止退出

删除容器

docker rm 容器id	# 删除指定容器,不能删除正在运行的容器,rm -f
docker rm -f $(docker ps -aq)	#删除所有的容器
docker ps -a -q | xargs dokcer rm	#删除所有的容器

启动和停止容器的操作

docker start 容器id	# 启动容器
docker restart 容器id	# 重启容器
docker stop 容器id	# 停止当前正在运行的容器
docker kill 容器id	# 强制停止当前容器

常用的其他命令

后台启动容器

# dockcer run -d centos

# 问题docker ps 发现centos 停止了

#常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

查看日志

docker logs -f -t --tail 10 容器id

# 自己写一段shell脚本
root@curtis-Aspire-E5-471G:/var/lib/docker# docker run -d centos /bin/bash -c "while true;do echo goodstudy;sleep 1;done"
a108e9e1e9109f4bc0b78bc0da8b7f0080f8469280e5429b75ae3be7d02feb76

# 查看正在运行的容器
root@curtis-Aspire-E5-471G:/var/lib/docker# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
a108e9e1e910   centos    "/bin/bash -c 'while…"   2 seconds ago   Up 2 seconds             admiring_roentgen

# 查看容器日志
	-tf		# 显示日志
	--tail	# 显示日志的条数
root@curtis-Aspire-E5-471G:/var/lib/docker# docker logs -f -t --tail 10 a108e9e1e910
2022-05-01T08:12:24.133390751Z goodstudy
2022-05-01T08:12:25.138280466Z goodstudy
2022-05-01T08:12:26.143196473Z goodstudy
2022-05-01T08:12:27.147893502Z goodstudy

查看容器中的进程信息ps

# 命令 docker top 容器id
root@curtis-Aspire-E5-471G:/var/lib/docker# docker top a108e9e1e910
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                9830                9808                0                   16:10               ?                   00:00:00            /bin/bash -c while true;do echo goodstudy;sleep 1;done
root                10226               9830                0                   16:15               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看镜像的元数据

# docker inspect --help
root@curtis-Aspire-E5-471G:/var/lib/docker# docker inspect a108e9e1e910
[
    {
        "Id": "a108e9e1e9109f4bc0b78bc0da8b7f0080f8469280e5429b75ae3be7d02feb76",
        "Created": "2022-05-01T08:10:13.1218029Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo goodstudy;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 9830,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-05-01T08:10:13.539226129Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/a108e9e1e9109f4bc0b78bc0da8b7f0080f8469280e5429b75ae3be7d02feb76/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/a108e9e1e9109f4bc0b78bc0da8b7f0080f8469280e5429b75ae3be7d02feb76/hostname",
        "HostsPath": "/var/lib/docker/containers/a108e9e1e9109f4bc0b78bc0da8b7f0080f8469280e5429b75ae3be7d02feb76/hosts",
        "LogPath": "/var/lib/docker/containers/a108e9e1e9109f4bc0b78bc0da8b7f0080f8469280e5429b75ae3be7d02feb76/a108e9e1e9109f4bc0b78bc0da8b7f0080f8469280e5429b75ae3be7d02feb76-json.log",
        "Name": "/admiring_roentgen",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/0d36dec5eb73483fcd9d9f51ac39d32e648a5916dd192fb62bc2c2a0c256c75d-init/diff:/var/lib/docker/overlay2/97ed0f87eaeb5bf5be6df1bca8dc81aacf39365398e6b67282b0bc1d89cc005c/diff",
                "MergedDir": "/var/lib/docker/overlay2/0d36dec5eb73483fcd9d9f51ac39d32e648a5916dd192fb62bc2c2a0c256c75d/merged",
                "UpperDir": "/var/lib/docker/overlay2/0d36dec5eb73483fcd9d9f51ac39d32e648a5916dd192fb62bc2c2a0c256c75d/diff",
                "WorkDir": "/var/lib/docker/overlay2/0d36dec5eb73483fcd9d9f51ac39d32e648a5916dd192fb62bc2c2a0c256c75d/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "a108e9e1e910",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash",
                "-c",
                "while true;do echo goodstudy;sleep 1;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "7983047c2f6eb41bf970378920042585d2d162b8144cd4a78ec969e2011f4d0c",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/7983047c2f6e",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "9daeadbbf59a414149e826f8dc2bd562bfaca1363741286e77bf54d967486ad2",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "094cd4cc13856b9efcfcbc157640efcba6ea17205902be61c2b0f2a21a894d2c",
                    "EndpointID": "9daeadbbf59a414149e826f8dc2bd562bfaca1363741286e77bf54d967486ad2",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

在上述字段中MergeDir为容器内文件在host上的路径,可以用下述命令获取配置文件中相关字段。

docker inspect --format='{{.GraphDriver.Data.MergedDir}}' 60dc2da59582

进入当前正在运行的容器

# 我们通常容器都是使用后天方式运行的,我们需要进入容器修改一些配置

# 命令
docker exec -it 容器id /bin/bash

root@curtis-Aspire-E5-471G:/var/lib/docker# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
a108e9e1e910   centos    "/bin/bash -c 'while…"   13 minutes ago   Up 13 minutes             admiring_roentgen
root@curtis-Aspire-E5-471G:/var/lib/docker# docker exec -it a108e9e1e910 /bin/bash
[root@a108e9e1e910 /]# ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 08:10 ?        00:00:00 /bin/bash -c while true;do echo goodstudy;sleep 1;done
root         847       0  0 08:24 pts/0    00:00:00 /bin/bash
root         863       1  0 08:24 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root         864     847  0 08:24 pts/0    00:00:00 ps -ef

# 方式2
docker attach 容器id

# 测试
# 正在执行当前的代码....

#docker exec	#进入容器后开启一个新的终端,可以在里面操作
#docker attach	#进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机

docker cp 容器id:容器内路径 目的路径

# 拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现,自动同步 /home /home

CSDN参考笔记:Docker快速入门总结笔记_Code0cean的博客-CSDN博客_docker快速入门

图形化管理工具Portaniner安装

Portaniner是Docker的图形化管理工具,类似的工具还有Rancher(CI/CD再用),提供ige后台面板提供我们操作!

下载运行Portaniner镜像并运行,设置本机映射端口为8088

# 下载Portaniner镜像并启动
root@curtis-Aspire-E5-471G:/var/lib/docker# docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
Unable to find image 'portainer/portainer:latest' locally
latest: Pulling from portainer/portainer
94cfa856b2b1: Pull complete
49d59ee0881a: Pull complete
a2300fd28637: Pull complete
Digest: sha256:fb45b43738646048a0a0cc74fcee2865b69efde857e710126084ee5de9be0f3f
Status: Downloaded newer image for portainer/portainer:latest
04774a0f41c0ffce8b9d773dba85a16e3df7065fd87e21751f2aa84858f3a72e

# 查看跑起来的容器
root@curtis-Aspire-E5-471G:/var/lib/docker# docker ps
CONTAINER ID   IMAGE                 COMMAND        CREATED         STATUS         PORTS                                       NAMES
04774a0f41c0   portainer/portainer   "/portainer"   2 minutes ago   Up 2 minutes   0.0.0.0:8088->9000/tcp, :::8088->9000/tcp   friendly_galois

第一次登录设置admin用户的密码

密码为:admin123456
在这里插入图片描述

一般选择本地的容器仓库
在这里插入图片描述
在这里插入图片描述

Docker镜像详解

什么是镜像

镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需要的所有内容,包括代码,运行时(一个程序在运行或者在被执行的依赖)、库,环境变量和配置文件。

所有的应用,直接打包docker镜像,就可以直接跑起来!

如何可以得到镜像:

  • 从仓库下载
  • 朋友拷贝给你
  • 自己制作一个镜像Dockerfile

UinonFS(联合文件系统)

UinonFS(联合文件系统):是一种分层、轻量级且高效的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟机文件系统(unite several directories into a single virtual filesystem)。Union文件系统是docker镜像的基础,镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。

特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录。

Docker 镜像加载原理

docker 的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。

bootfs(boot file system)主要包含bootloader和kernel,bootloader主要是引导加载kernel,Linux刚启动时会加载bootfs文件系统,在docker镜像的最底层时bootfs。这一层与我们典型的Linux/Unix系统是一样的,包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已由bootfs转交给内核,此时系统也会卸载bootfs。

rootfs(root file system),在bootfs之上,包含的就是典型的Linux系统中的/dev,/proc,/bin,/etc等标准目录和文件。rootfs就是各种不同操作系统发行版,比如ubuntu,centos等等。

平时我们安装进虚拟机的Centos都是好几个G,为什么Docker这里才200M?

root@curtis-Aspire-E5-471G:/var/lib/docker# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
centos                latest    5d0da3dc9764   7 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   13 months ago   79.1MB

对于一个精简的OS,rootfs可以很小,只需要包含最基本的命令,工具和程序库就可以了,因为底层直接用Host的kernel,自己只需要提供rootfs就可以了,由此可见对于不同的Linux发行版,bootfs基本是一致的,rootfs会有差别,因此不同的发行版可以公用bootfs。

分层的理解

我们可以去下载一个镜像,注意观察下载日志的输出日志,可以看到是一层一层的下载。

root@curtis-Aspire-E5-471G:/var/lib/docker# docker pull mysql:5.7
5.7: Pulling from library/mysql
4be315f6562f: Already exists		# 分层文件已经存在
96e2eb237a1b: Already exists
8aa3ac85066b: Already exists
ac7e524f6c89: Already exists
f6a88631064f: Already exists
15bb3ec3ff50: Already exists
ae65dc337dcb: Already exists
a4c4c43adf52: Pull complete
c6cab33e8f91: Pull complete
2e1c4f2c43f6: Pull complete
2e5ee322af48: Pull complete
Digest: sha256:e767595ba3408fbb2dda493be3594b9a148178df58325fafe8b0363662935624
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

思考:为社么docker镜像要采用这种分层的结构呢?

最大的好处,我觉得莫过于是资源共享了,比如有多少个镜像都从相同的base镜像构建而来,那么宿主机只需要在磁盘上保留一份base镜像,同时内存中需要加载一份base镜像,这样就可以为所有的容器服务了,而且镜像的每一层都可以被共享。

查看镜像分层的方式可以通过docker image inspect命令!!


root@curtis-Aspire-E5-471G:/var/lib/docker# docker image inspect centos
[
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

理解:

所有的Docker镜像都起始于一个基础镜像层,当进行修改或者增加新的内容时,就会在当前镜像层之上,创建新的镜像层。

举一个加单的例子,假如基于ubuntu linux 16.04创建一个新的镜像,这就是新的镜像的第一层,如果在该镜像中添加Python包,该镜像就会在基础镜像层之上创建第二个镜像层,如果继续添加一个安全补丁,就会创建第三个镜像层。

该镜像当前包含3个镜像层,如下图所示(这只是一个用于演示的很简单的例子)

在这里插入图片描述

在添加额外的镜像层的同时,镜像始终保持是当前所有镜像的组合,理解这一点非常重要,下图举了一个简单的例子,每个镜像层包含3个文件,而镜像包含了来自两个镜像层的6个文件。

在这里插入图片描述

上图中的镜像层跟之前图中略有区别,主要目的便于展示文件。

下图中展示了一个稍微复杂的三层镜像,在外部看来整个镜像只有6个文件,这是因为最上层中的文件7是文件5的一个更新版本。
在这里插入图片描述

在这种情况下,上层镜像层中的文件覆盖了底层镜像中的文件,这样使得文件的更新版本作为一个镜像层添加到镜像当中。

Docker通过存储引擎(新版本采用快照机制)的方式来实现镜像层堆栈,并保证多层镜像层对外展示为统一的文件系统,Linux上可用的存储引擎有AUFS,Overlay2,Device Mapper,Btrfs以及ZFS。顾名思义,每种存储引擎都基于 Linux 中对应的文件或者块设备技术,并且每种存储引擎都有其独有的性能特点。
在这里插入图片描述

特点

Docker 镜像都是只读的,当容器启动时,一个新的可写层被加载到镜像的顶部!

这一层就是我们通常说的容器层,容器之下的都叫镜像层。

提交镜像

使用docker commit 命令提交容器成为一个新的版本

# 命令与
docker commit -m=“提交的描述信息”  -a="作者" 容器id 目标镜像名:[TAG]
# 1、启动一个默认的tomcat
root@curtis-Aspire-E5-471G:/var/lib/docker# docker run -it -p 8080:8080 tomcat
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/openjdk-11
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
01-May-2022 12:44:52.013 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/10.0.20
01-May-2022 12:44:52.023 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Mar 31 2022 14:24:36 UTC
01-May-2022 12:44:52.023 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.0.20.0
01-May-2022 12:44:52.023 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
01-May-2022 12:44:52.024 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            5.13.0-39-generic
01-May-2022 12:44:52.024 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
01-May-2022 12:44:52.024 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/openjdk-11
01-May-2022 12:44:52.025 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           11.0.15+10
01-May-2022 12:44:52.025 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
01-May-2022 12:44:52.025 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
01-May-2022 12:44:52.026 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat

# 2、发现这个默认的tomacat 是没有webapps应用,镜像的原因,官方默认没有tomcat应用

# 3、我自己拷贝进去了基本的文件
root@curtis-Aspire-E5-471G:/home/curtis# docker exec -it ac3c80cef910 /bin/bash
root@ac3c80cef910:/usr/local/tomcat#
root@ac3c80cef910:/usr/local/tomcat#
root@ac3c80cef910:/usr/local/tomcat# ls
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
root@ac3c80cef910:/usr/local/tomcat# cp -rf webapps.dist/* ./webapps
root@ac3c80cef910:/usr/local/tomcat#
root@ac3c80cef910:/usr/local/tomcat# exit

# 4、将操作过的容器通过commit提交为一个镜像,以后我们就可以使用我们修改过的镜像即可,这就是我们自己的一个修改的镜像
root@curtis-Aspire-E5-471G:/home/curtis# docker commit -m="add webapps" -a="Curtis" ac3c80cef910 tomcat02:1.0
sha256:aced601e8fbcf88ddb95042fb5760b569cf3f9a22dbc2165aa947abf675b912f
root@curtis-Aspire-E5-471G:/home/curtis# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS                                       NAMES
ac3c80cef910   tomcat    "catalina.sh run"   8 minutes ago   Up 8 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   quirky_shirley
root@curtis-Aspire-E5-471G:/home/curtis# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
tomcat02              1.0       aced601e8fbc   11 seconds ago   684MB
tomcat                latest    0183eb12bb0c   5 days ago       680MB
centos                latest    5d0da3dc9764   7 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   13 months ago    79.1MB

学习方式说明:理解概念,但是一定要实践,最后时间和理论相结合一次搞定这个只是

如果你想要保存当前容器的状态,就可以通过commit来提交,获得一个镜像。

好比学习VM的时候学习的快照。

容器数据卷

docker的理念回顾

将应用和环境打包成一个镜像!

数据?如果数据都在容器中,那么我们容器删除,数据就会丢失!需求:数据可以持久化

MySQL,容器删了,删库跑路!需求:MySQL数据可以存储在本地!

容器之间可以有一个数据共享的技术!Docker容器中产生的数据,同步到本地!

这就是卷技术!目录的挂载,将我们容器的目录,挂载到Linux上面!
在这里插入图片描述

总结一句话:容器的持久化和同步操作,容器间也是可以文件共享的!!

使用数据卷

方式一:直接使用命令来挂载 -v

docker run -it -v 主机目录:容器内目录

root@curtis-Aspire-E5-471G:/home/curtis# docker run -it -v /home/ceshi:/home centos /bin/bash
[root@45d74256a828 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@45d74256a828 /]# cd /home/
[root@45d74256a828 home]# ls
[root@45d74256a828 home]# mkdir ll

# 启动起来之后可以通过docker inspect 容器id

在这里插入图片描述

测试文件是否同步!!!

1、停止容器

2、修改宿主机上的文件

3、启动容器

4、数据还是同步

好处:我们以后只需要在本地修改即可,容器内会自动同步!!!!

实战:安装MySQL

思考:MySQL的数据持久化的问题,

# 获取镜像
root@curtis-Aspire-E5-471G:/home/ceshi# docker pull mysql:5.7

# 运行容器,需要做数据挂载
# 安装启动 mysql 需要配置密码,这个是需要注意的点
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7

# 启动docker
-d 后台运行
-p 端口映射
-P 随机端口
-v 卷挂载
-e 环境配置
--name 容器的名字

# 启动容器之后,本地使用sqlyog来连接测试
# sqlyog--h -u -p 连接到服务其的3310 和容器内的3306映射,这个时候就可以连接上了

# 在本地测试创建一个数据库,查看映射路径是不是正常?

我们挂载到本地的文件是不会丢失的!!!

具名挂载和匿名挂载

# 匿名挂载
-v 容器内路径!
docker run -d -P --name nginx01 -v /etc/ngix nginx

# docker volume命令
root@curtis-Aspire-E5-471G:/home/curtis# docker volume --help

Usage:  docker volume COMMAND

Manage volumes

Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove all unused local volumes
  rm          Remove one or more volumes

Run 'docker volume COMMAND --help' for more information on a command.

# 查看所有卷的情况
root@curtis-Aspire-E5-471G:/home/curtis# docker volume ls
DRIVER    VOLUME NAME
local     bc141a385fcb82d39ef0660e9dc8c46acb448904b9415da255c5fa775ff291cc
local     de62ac8708c942df92e5988207e6a4ddaf2afb223d0899e03aef3d8aab45a68f

# 这里发现,这种就是匿名挂载,我们在-v 只写了容器内的路径,没有写容器外的路径
root@curtis-Aspire-E5-471G:/home/curtis# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
8eebc5db290246555144638cdbd5a97107df8a536c03b22555473df2731a6cd5
root@curtis-Aspire-E5-471G:/home/curtis#
root@curtis-Aspire-E5-471G:/home/curtis# docker volume ls
DRIVER    VOLUME NAME
local     bc141a385fcb82d39ef0660e9dc8c46acb448904b9415da255c5fa775ff291cc
local     de62ac8708c942df92e5988207e6a4ddaf2afb223d0899e03aef3d8aab45a68f
local     juming-nginx
root@curtis-Aspire-E5-471G:/home/curtis#

# 具名挂载,通过-v 卷名:容器内路径
# 查看一下这个卷
root@curtis-Aspire-E5-471G:/home/curtis# docker volume inspect juming-nginx
[
    {
        "CreatedAt": "2022-05-03T18:19:57+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]

所有的docker容器内的卷,没有指定目录的情况下在/var/lib/docker/volumes/xxx

通过具名挂载可以方便找到挂载的卷,大多数情况下使用的是具名挂载!!!

# 如何确定是具名挂载还是匿名挂载,还是指定路径挂载!!
-v 容器内路径	# 匿名
-v 卷名:容器内路径	# 具名挂载
-v /宿主机路径:容器内路径	# 指定路径挂载!

拓展:

# 通过 -v 容器内路径:ro,rw 改变读写权限
ro readonly	# 只读
rw read/write	# 可读可写

# 一旦设置了容器权限,容器对我们挂载出来的内容就有限定了!
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx

# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器无法操作

初识DokerFile

Dockerfile就是用来构建docker镜像的构建文件!命令脚本!先体验一下!!

通过这个脚本可以生成镜像,镜像是一层一层的,脚本一个个的命令,每个命令都是一层!

# 创建一个dockerfile,名字可以随机,建议Dockerfile
# 文件中的内容 指令(大写)参数

FROM centos

VOLUME ["volume01", "volume02"]

CMD echo "----end----"

CMD /bin/bash

# 这里的每个命令,就是镜像的一层

root@curtis-Aspire-E5-471G:/home/docker-test# docker build -f dockerfile1 -t curtis/centos:2.0 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01", "volume02"]
 ---> Running in a8193431146c
Removing intermediate container a8193431146c
 ---> 91db7fdc8e3b
Step 3/4 : CMD echo "----end----"
 ---> Running in dc62893fda7f
Removing intermediate container dc62893fda7f
 ---> d2bfeca0a935
Step 4/4 : CMD /bin/bash
 ---> Running in 80e9d19520c1
Removing intermediate container 80e9d19520c1
 ---> c74fceb52921
Successfully built c74fceb52921
Successfully tagged curtis/centos:2.0
root@curtis-Aspire-E5-471G:/home/docker-test# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
curtis/centos         2.0       c74fceb52921   9 seconds ago   231MB
tomcat02              1.0       aced601e8fbc   46 hours ago    684MB
mysql                 5.7       8aa4b5ffb001   5 days ago      462MB
tomcat                latest    0183eb12bb0c   7 days ago      680MB
nginx                 latest    fa5269854a5e   13 days ago     142MB
centos                latest    5d0da3dc9764   7 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   13 months ago   79.1MB
# 使用自己的镜像启动一个容器

root@curtis-Aspire-E5-471G:/home/docker-test# docker run -it c74fceb52921 /bin/bash
[root@6e39a8226904 /]# ls -alh
total 64K
drwxr-xr-x   1 root root 4.0K May  3 10:55 .
drwxr-xr-x   1 root root 4.0K May  3 10:55 ..
-rwxr-xr-x   1 root root    0 May  3 10:55 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 May  3 10:55 dev
drwxr-xr-x   1 root root 4.0K May  3 10:55 etc
drwxr-xr-x   2 root root 4.0K Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4.0K Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4.0K Nov  3  2020 media
drwxr-xr-x   2 root root 4.0K Nov  3  2020 mnt
drwxr-xr-x   2 root root 4.0K Nov  3  2020 opt
dr-xr-xr-x 324 root root    0 May  3 10:55 proc
dr-xr-x---   2 root root 4.0K Sep 15  2021 root
drwxr-xr-x  11 root root 4.0K Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4.0K Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 May  3 10:55 sys
drwxrwxrwt   7 root root 4.0K Sep 15  2021 tmp
drwxr-xr-x  12 root root 4.0K Sep 15  2021 usr
drwxr-xr-x  20 root root 4.0K Sep 15  2021 var
drwxr-xr-x   2 root root 4.0K May  3 10:55 volume01
drwxr-xr-x   2 root root 4.0K May  3 10:55 volume02

这个卷和外部有一个同步目录!!!!!

# 查看卷外部同步路径目录
root@curtis-Aspire-E5-471G:/home/curtis# docker inspect 6e39a8226904
        "Mounts": [
            {
                "Type": "volume",
                "Name": "0c5e6cf6f43a29c706f5c558eac26ecde9ce991e805b55749aab7c3d00b76a0b",
                "Source": "/var/lib/docker/volumes/0c5e6cf6f43a29c706f5c558eac26ecde9ce991e805b55749aab7c3d00b76a0b/_data",
                "Destination": "volume01",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "ea67e29ca6c130206979ec33d5db6965354892026a37fed910f53dd8c82ca88b",
                "Source": "/var/lib/docker/volumes/ea67e29ca6c130206979ec33d5db6965354892026a37fed910f53dd8c82ca88b/_data",
                "Destination": "volume02",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

这种方式未来会十分常用,因为我们通常会构建自己的镜像,!
假设构建镜像时候没有挂载卷,要自动挂载镜像 -v

数据卷容器

两个mysql同步数据
在这里插入图片描述

# 启动三个容器,通过自己写的镜像启动
# 启动一个镜像docker01
root@curtis-Aspire-E5-471G:/home/curtis# docker run -it --name docker01 curtis/centos:2.0
[root@66d3aec19435 /]# ls -alh
total 64K
drwxr-xr-x   1 root root 4.0K May  4 02:10 .
drwxr-xr-x   1 root root 4.0K May  4 02:10 ..
-rwxr-xr-x   1 root root    0 May  4 02:10 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 May  4 02:10 dev
drwxr-xr-x   1 root root 4.0K May  4 02:10 etc
drwxr-xr-x   2 root root 4.0K Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4.0K Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4.0K Nov  3  2020 media
drwxr-xr-x   2 root root 4.0K Nov  3  2020 mnt
drwxr-xr-x   2 root root 4.0K Nov  3  2020 opt
dr-xr-xr-x 301 root root    0 May  4 02:10 proc
dr-xr-x---   2 root root 4.0K Sep 15  2021 root
drwxr-xr-x  11 root root 4.0K Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4.0K Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 May  4 02:10 sys
drwxrwxrwt   7 root root 4.0K Sep 15  2021 tmp
drwxr-xr-x  12 root root 4.0K Sep 15  2021 usr
drwxr-xr-x  20 root root 4.0K Sep 15  2021 var
drwxr-xr-x   2 root root 4.0K May  4 02:10 volume01 <- 已经挂载的容器卷01
drwxr-xr-x   2 root root 4.0K May  4 02:10 volume02 <- 已经挂载的容器卷02

# docker02共享docker01的挂载卷
root@curtis-Aspire-E5-471G:/home/curtis# docker run -it --name docker02 --volumes-from docker01 curtis/centos:2.0
[root@f219c702690a /]# ls -alh
total 64K
drwxr-xr-x   1 root root 4.0K May  4 02:14 .
drwxr-xr-x   1 root root 4.0K May  4 02:14 ..
-rwxr-xr-x   1 root root    0 May  4 02:14 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 May  4 02:14 dev
drwxr-xr-x   1 root root 4.0K May  4 02:14 etc
drwxr-xr-x   2 root root 4.0K Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4.0K Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4.0K Nov  3  2020 media
drwxr-xr-x   2 root root 4.0K Nov  3  2020 mnt
drwxr-xr-x   2 root root 4.0K Nov  3  2020 opt
dr-xr-xr-x 317 root root    0 May  4 02:14 proc
dr-xr-x---   2 root root 4.0K Sep 15  2021 root
drwxr-xr-x  11 root root 4.0K Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4.0K Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 May  4 02:14 sys
drwxrwxrwt   7 root root 4.0K Sep 15  2021 tmp
drwxr-xr-x  12 root root 4.0K Sep 15  2021 usr
drwxr-xr-x  20 root root 4.0K Sep 15  2021 var
drwxr-xr-x   2 root root 4.0K May  4 02:10 volume01
drwxr-xr-x   2 root root 4.0K May  4 02:10 volume02

# 测试,删除docker01,查看docker02和docker03是否还可以访问这个文件
# 测试依旧可以访问
# 两个mysql之间进行数据共享

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

$ docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7

# 这个时候可以实现数据的共享

结论:

容器之间配置信息的传递,数据卷容器的声明周期一直持续到没有容器使用为止。

但是一旦你持久化到了本地,这个时候,本地的数据是不会被删除的!

Dockerfile

dockerfile 是用来构建docker镜像的文件!命令参数脚本!!

构建步骤:

1、编写一个 dockerfile 文件

2、docker build 构建成为一个镜像

3、docker run 运行镜像

4、docker push到pub

在这里插入图片描述

很多官方的镜像都是基础包,很多功能没有,通常需要自己搭建镜像!!

官方既然可以制作镜像,我们也可以!!

Dockerfile构建过程

基础知识:

1、每个关键保留字(指令)都必须是大写字母

2、执行从上到下顺序执行

3、# 表示注释

4、每一个指令都会创建提交一个新的镜像层,并提交!!
在这里插入图片描述

dockerfile是面向开发的,发布项目,做镜像,需要编写dockerfile文件,编写简单!!

Docker 镜像逐渐成为企业交付的标准,必须要掌握!

步骤:开发,部署,运维

Dockerfile:构建文件,定义了一切步骤,源代码

Dockerimages:通过 Dockerfile 构建生成的镜像,最终发布和运行产品

Docker容器:容器就是镜像运行起来提供服务的

Dockerfile指令

FROM			# 基础镜像 centos ubuntu,一切从这里开始构建
MAINTAINER		# 镜像是谁写的,姓名 + 邮箱
RUN				# 镜像构建时需要运行的命令
ADD				# 步骤,tomacat镜像,tomcat压缩包,添加内容
WORKDIR			# 镜像的工作目录 / 
VOLUME			# 挂载的目录
EXPOSE			# 暴露端口配置  -p
CMD				# 指定容器启动时候要运行的命令,只有最后一个生效,可被替代
ENTRYPOINT		# 指定容器启动时候要运行的命令,可以追加命令
ONBUILD			# 当构建一个被继承 dockefile 这个时候就会运行ONBUILD 指令,触发指令
COPY			# 类似ADD命令,将文件拷贝到镜像中
ENV				# 构建时设置环境变量

在这里插入图片描述

实战测试

Docker Hub 中 99% 镜像都是从最基础的镜像过来, FROM scratch,然后配置自己的东西!!

创建子自己的 centos ,由于Centos 8镜像源变化需要先配置镜像源

# 1、编写Dockerfilew文件
root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# cat dockerfile01
FROM lzmcentos
MAINTAINER curtis<111111@qq.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum install -y vim
RUN yum install -y net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "------end-------"
CMD /bin/bash

# 2、通过dockerfiel构建镜像文件
# 命令 docker build -f dockefile文件路径 -t 镜像名:版本号 .

root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# docker build -f dockerfile01 -t lzmcentos:2.0 .
...........
Removing intermediate container ab5de32d61d7
 ---> 9603eb29709d
Step 10/10 : CMD /bin/bash
 ---> Running in be1a3472c97d
Removing intermediate container be1a3472c97d
 ---> 8877bd6ce411
Successfully built 8877bd6ce411
Successfully tagged lzmcentos:2.0

# 查看新的centos镜像
root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
lzmcentos             2.0       8877bd6ce411   7 minutes ago    340MB
lzmcentos             1.0       b6e39e6eee18   12 minutes ago   259MB

# 3、测试运行,工作目录默认是/usr/local,可以使用vim 和 ifconfig 命令
root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# docker run -it lzmcentos:2.0
[root@59826dfb5942 local]# pwd
/usr/local
[root@59826dfb5942 local]# vim
[root@59826dfb5942 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.4  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)
        RX packets 23  bytes 2894 (2.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

查看镜像构建的历史过程 docker history

root@curtis-Aspire-E5-471G:/home/curtis# docker history 8877bd6ce411
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
8877bd6ce411   12 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B
9603eb29709d   12 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
ed8bbad10f99   12 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
bc56ecb75a14   12 minutes ago   /bin/sh -c #(nop)  EXPOSE 80                    0B
f91716c62cf7   12 minutes ago   /bin/sh -c yum install -y net-tools             28.4MB
2fec6d637dcd   12 minutes ago   /bin/sh -c yum install -y vim                   53.2MB
7bd414cb5de1   13 minutes ago   /bin/sh -c #(nop) WORKDIR /usr/local            0B
79e54fe8fb40   13 minutes ago   /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B
3c2d24f43b5e   13 minutes ago   /bin/sh -c #(nop)  MAINTAINER curtis<111111@…   0B
b6e39e6eee18   17 minutes ago   /bin/bash                                       27.2MB    “centos添加安装源”
5d0da3dc9764   7 months ago     /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>      7 months ago     /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B
<missing>      7 months ago     /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB

拿到一个镜像,可以研究一下它是怎么做的!!!


root@curtis-Aspire-E5-471G:/home/curtis# docker history 0183eb12bb0c
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
0183eb12bb0c   9 days ago    /bin/sh -c #(nop)  CMD ["catalina.sh" "run"]    0B
<missing>      9 days ago    /bin/sh -c #(nop)  EXPOSE 8080                  0B
<missing>      9 days ago    /bin/sh -c set -eux;  nativeLines="$(catalin…   0B
<missing>      9 days ago    /bin/sh -c set -eux;   savedAptMark="$(apt-m…   20.2MB
<missing>      9 days ago    /bin/sh -c #(nop)  ENV TOMCAT_SHA512=53bfdba…   0B
<missing>      9 days ago    /bin/sh -c #(nop)  ENV TOMCAT_VERSION=10.0.20   0B
<missing>      9 days ago    /bin/sh -c #(nop)  ENV TOMCAT_MAJOR=10          0B
<missing>      9 days ago    /bin/sh -c #(nop)  ENV GPG_KEYS=A9C5DF4D22E9…   0B
<missing>      9 days ago    /bin/sh -c #(nop)  ENV LD_LIBRARY_PATH=/usr/…   0B
<missing>      9 days ago    /bin/sh -c #(nop)  ENV TOMCAT_NATIVE_LIBDIR=…   0B
<missing>      9 days ago    /bin/sh -c #(nop) WORKDIR /usr/local/tomcat     0B
<missing>      9 days ago    /bin/sh -c mkdir -p "$CATALINA_HOME"            0B
<missing>      9 days ago    /bin/sh -c #(nop)  ENV PATH=/usr/local/tomca…   0B
<missing>      9 days ago    /bin/sh -c #(nop)  ENV CATALINA_HOME=/usr/lo…   0B
<missing>      9 days ago    /bin/sh -c #(nop)  CMD ["jshell"]               0B
<missing>      9 days ago    /bin/sh -c set -eux;   arch="$(dpkg --print-…   343MB
<missing>      9 days ago    /bin/sh -c #(nop)  ENV JAVA_VERSION=11.0.15     0B
<missing>      2 weeks ago   /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B
<missing>      2 weeks ago   /bin/sh -c #(nop)  ENV PATH=/usr/local/openj…   0B
<missing>      2 weeks ago   /bin/sh -c { echo '#/bin/sh'; echo 'echo "$J…   27B
<missing>      2 weeks ago   /bin/sh -c #(nop)  ENV JAVA_HOME=/usr/local/…   0B
<missing>      2 weeks ago   /bin/sh -c set -eux;  apt-get update;  apt-g…   11.3MB
<missing>      2 weeks ago   /bin/sh -c apt-get update && apt-get install…   152MB
<missing>      2 weeks ago   /bin/sh -c set -ex;  if ! command -v gpg > /…   19MB
<missing>      2 weeks ago   /bin/sh -c set -eux;  apt-get update;  apt-g…   10.7MB
<missing>      2 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>      2 weeks ago   /bin/sh -c #(nop) ADD file:3a81c181c66f226bd…   124MB

CMD 和 ENTRYPOINT的区别

CMD				# 指定容器启动时候要运行的命令,只有最后一个生效,可被替代
ENTRYPOINT		# 指定容器启动时候要运行的命令,可以追加命令

测试CMD

FROM centos
CMD ["ls","-a"]

# 1、构建镜像
root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# docker build -f dockerfile-cmd-list -t cmdcentos:1.0 .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : CMD ["ls","-a"]
 ---> Running in c9e0ac29dd62
Removing intermediate container c9e0ac29dd62
 ---> 483ff0bc9537
Successfully built 483ff0bc9537
Successfully tagged cmdcentos:1.0

# 2、启动运行,只有最后一个CMD会生效
root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# docker run 483ff0bc9537
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

# 想要追加一个命令报错
root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# docker run 483ff0bc9537 -l
docker: Error response from daemon: failed to create shim: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled

# cmd的情况下 -l 替换了CMD ["ls","-a"]命令, —l不是命令,所以报错

docerfile-cmd-entrypoind

FROM cnetos
ENTRYPOINT ["ls","-a"]

root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# docker build -f dockerfile-cmd-entrypoint -t cmdcentos:1.0 .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 52abd61adc47
Removing intermediate container 52abd61adc47
 ---> a33938b504d7
Successfully built a33938b504d7
Successfully tagged cmdcentos:1.0
root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# docker run -it a33938b504d7
.   .dockerenv  dev  home  lib64       media  opt   root  sbin  sys  usr
..  bin         etc  lib   lost+found  mnt    proc  run   srv   tmp  var

# 追加命令直接追加在entrypoint的命令后面
root@curtis-Aspire-E5-471G:/home/docker-test/dockerfile# docker run -it a33938b504d7 -l
total 56
drwxr-xr-x   1 root root 4096 May  5 14:31 .
drwxr-xr-x   1 root root 4096 May  5 14:31 ..
-rwxr-xr-x   1 root root    0 May  5 14:31 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 May  5 14:31 dev
drwxr-xr-x   1 root root 4096 May  5 14:31 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 377 root root    0 May  5 14:31 proc
dr-xr-x---   2 root root 4096 Sep 15  2021 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 May  5 14:31 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var

Docker命令都十分的相似,要了解他们的区别,对比测试效果!!!

实战:Tomcat镜像

1、准备镜像 tomcat,apache镜像

Docker网络

理解Docker0

root@curtis-Aspire-E5-471G:/home/curtis# ip addr
# 本机回环网络
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
# 物理网卡
2: enp4s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether c4:54:44:89:f0:82 brd ff:ff:ff:ff:ff:ff
# 无线网卡
3: wlp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 90:48:9a:d9:9a:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.104/24 brd 192.168.0.255 scope global dynamic noprefixroute wlp5s0
       valid_lft 6177sec preferred_lft 6177sec
    inet6 fe80::78e3:ffac:60cb:2bc5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
# docker网络
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:2b:a2:88:cb brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:2bff:fea2:88cb/64 scope link
       valid_lft forever preferred_lft forever

三个网络

# 问题,docker 是如何处理容器网络访问!!
root@curtis-Aspire-E5-471G:/home/curtis# docker run -it --name lzmcentos lzmcentos:2.0
# 查看容器内部网络地址,发现容器启动会得到一个 eth0@if50 ip地址,docker分配
[root@1a700c1c34a1 local]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
49: eth0@if50: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
       
# 思考,linux 能不能 ping 通容器内部!
root@curtis-Aspire-E5-471G:/home/curtis# ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.082 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.084 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.084 ms

# linux 可以ping通docker 容器内部

原理

192.168.0.1 路由器

192.168.0.2 手机连接 -> 在同一个网段

1、每启动一个docker容器,docker就会给docker容器分配一个ip,只要安装了docker,就会有一个网卡docker0

桥接模式,使用的技术是 evth-pair技术!!

# host 再次查看网卡信息
root@curtis-Aspire-E5-471G:/home/curtis# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp4s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether c4:54:44:89:f0:82 brd ff:ff:ff:ff:ff:ff
3: wlp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 90:48:9a:d9:9a:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.104/24 brd 192.168.0.255 scope global dynamic noprefixroute wlp5s0
       valid_lft 4289sec preferred_lft 4289sec
    inet6 fe80::78e3:ffac:60cb:2bc5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:2b:a2:88:cb brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:2bff:fea2:88cb/64 scope link
       valid_lft forever preferred_lft forever
50: veth946e6d6@if49: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether 2a:29:69:1a:44:e8 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::2829:69ff:fe1a:44e8/64 scope link
       valid_lft forever preferred_lft forever

2、再启动个centos容器进行测试,发现又多了一对网卡

root@curtis-Aspire-E5-471G:/home/curtis# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp4s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether c4:54:44:89:f0:82 brd ff:ff:ff:ff:ff:ff
3: wlp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 90:48:9a:d9:9a:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.104/24 brd 192.168.0.255 scope global dynamic noprefixroute wlp5s0
       valid_lft 4101sec preferred_lft 4101sec
    inet6 fe80::78e3:ffac:60cb:2bc5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:2b:a2:88:cb brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:2bff:fea2:88cb/64 scope link
       valid_lft forever preferred_lft forever
50: veth946e6d6@if49: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether 2a:29:69:1a:44:e8 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::2829:69ff:fe1a:44e8/64 scope link
       valid_lft forever preferred_lft forever
52: veth14c1a5e@if51: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether 8e:b7:c7:47:83:80 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::8cb7:c7ff:fe47:8380/64 scope link
       valid_lft forever preferred_lft forever

容器带来的网卡,是一对一对的,evth-pair 就是一对的虚拟设备接口,成对出现,一端连着协议,一端彼此相连,evth-pair 充当一个桥梁,连接各种虚拟网络设备,OpenStack,Docker 容器之间的连接,OVS的连接,都是使用 evth-pair 技术。

3、lzmcentos 和 lzmcentos01是可以相互ping通!

容器和容器之间是可以相互 ping通
在这里插入图片描述

结论:tomcat01 和 tomcat02 是共用的一个路由器,docker0。

所有容器不指定网络的情况下,都是 docker0 路由的,docker 会给容器分配一个默认可以IP

255.255.0.1/16 -> 16 -> 表示域 ,它的值可以是 16 / 24

00000000.00000000.00000000.00000000

255.255.255.255

小结

Docker是通过网络桥接的方式连接,宿主机中是一个docker容器的网桥docker0。
在这里插入图片描述

Docker 中的所有的网络接口都是虚拟的,虚拟的转发效率高!(内网传输文件)

只要容器删除,对应网桥就没了。

编写一个微服务,database url=ip:,项目不重启,数据库ip换掉了,可以通过名字来访问容器?

mysql ip -> 通过服务名来ping通

# 直接 ping 容器名是Ping不通
[root@1a700c1c34a1 local]# ping lzmcentos01
ping: lzmcentos01: Name or service not known

# 如何可以解决呢?-> 直接通过容器名来Ping,通过--link的方式
root@curtis-Aspire-E5-471G:/home/curtis# docker run -it --name lzmcentos02 --link lzmcentos01 lzmcentos:2.0
[root@73089af76381 local]# ping lzmcentos01
PING lzmcentos01 (172.17.0.2) 56(84) bytes of data.
64 bytes from lzmcentos01 (172.17.0.2): icmp_seq=1 ttl=64 time=0.260 ms
64 bytes from lzmcentos01 (172.17.0.2): icmp_seq=2 ttl=64 time=0.108 ms

DockeD

企业实战

Docker Compose

Docker Swarm

CI/CD jenkens

ink
valid_lft forever preferred_lft forever
52: veth14c1a5e@if51: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 8e:b7:c7:47:83:80 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::8cb7:c7ff:fe47:8380/64 scope link
valid_lft forever preferred_lft forever


容器带来的网卡,是一对一对的,evth-pair 就是一对的虚拟设备接口,成对出现,一端连着协议,一端彼此相连,evth-pair 充当一个桥梁,连接各种虚拟网络设备,OpenStack,Docker 容器之间的连接,OVS的连接,都是使用 evth-pair 技术。

3、lzmcentos 和 lzmcentos01是可以相互ping通!

> 容器和容器之间是可以相互 ping通


结论:tomcat01 和 tomcat02 是共用的一个路由器,docker0。

所有容器不指定网络的情况下,都是 docker0 路由的,docker 会给容器分配一个默认可以IP

255.255.0.1/16 -> 16 -> 表示域 ,它的值可以是 16 / 24

00000000.00000000.00000000.00000000

255.255.255.255

> 小结

Docker是通过网络桥接的方式连接,宿主机中是一个docker容器的网桥docker0。

Docker 中的所有的网络接口都是虚拟的,虚拟的转发效率高!(内网传输文件)

只要容器删除,对应网桥就没了。



> 编写一个微服务,database url=ip:,项目不重启,数据库ip换掉了,可以通过名字来访问容器?

mysql ip -> 通过服务名来ping通

```bash
# 直接 ping 容器名是Ping不通
[root@1a700c1c34a1 local]# ping lzmcentos01
ping: lzmcentos01: Name or service not known

# 如何可以解决呢?-> 直接通过容器名来Ping,通过--link的方式
root@curtis-Aspire-E5-471G:/home/curtis# docker run -it --name lzmcentos02 --link lzmcentos01 lzmcentos:2.0
[root@73089af76381 local]# ping lzmcentos01
PING lzmcentos01 (172.17.0.2) 56(84) bytes of data.
64 bytes from lzmcentos01 (172.17.0.2): icmp_seq=1 ttl=64 time=0.260 ms
64 bytes from lzmcentos01 (172.17.0.2): icmp_seq=2 ttl=64 time=0.108 ms

DockeD

企业实战

Docker Compose

Docker Swarm

CI/CD jenkens

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值