Docker认识和深入(二)
容器数据卷
什么是容器数据卷
docker将应用和环境打包成一个镜像!那么数据怎么办,如果数据都在容器中,那么我们容器删除,数据就会丢失!需要:数据可以持久化 Mysql, 容器删了,Mysql数据可以存储在本地,容器之间可以有一个数据共享技术,Docker 容器 产生的数据,同步到本地
这就是卷技术,目录的挂载,将我们容器内的目录,挂载到Linux上面!
容器的持久化和同步操作,容器创建也是可以数据共享的。
使用数据卷
方法一,直接使用命令来挂载 -v
[root@VM-0-15-centos home]# docker run -it -v 主机目录,容器内目录
[root@VM-0-15-centos home]# docker run -it -v /home/ceshi:/home centos /bin/bash
[root@a38b20352b92 /]# cd /home
[root@a38b20352b92 home]# ls
[root@a38b20352b92 home]# [root@VM-0-15-centos home]# docker inspect a38b20352b92
[
{
"Id": "a38b20352b92744182b02f04b54b28b8fecad1c2968c71029fa08a87e38bbcb2",
"Created": "2021-02-27T21:00:01.224038543Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 164311,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-02-27T21:00:01.942921355Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/a38b20352b92744182b02f04b54b28b8fecad1c2968c71029fa08a87e38bbcb2/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a38b20352b92744182b02f04b54b28b8fecad1c2968c71029fa08a87e38bbcb2/hostname",
"HostsPath": "/var/lib/docker/containers/a38b20352b92744182b02f04b54b28b8fecad1c2968c71029fa08a87e38bbcb2/hosts",
"LogPath": "/var/lib/docker/containers/a38b20352b92744182b02f04b54b28b8fecad1c2968c71029fa08a87e38bbcb2/a38b20352b92744182b02f04b54b28b8fecad1c2968c71029fa08a87e38bbcb2-json.log",
"Name": "/funny_williamson",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/home/ceshi:/home"
],
"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/3f20f90abb60811570efc5a8f22eb543c9aa0281c9791a82d48d81271a8a4d72-init/diff:/var/lib/docker/overlay2/d9a9bf7b3335d07d2b17c68fd2740fdb791ee1dad81cf444c899e9fc6950962f/diff",
"MergedDir": "/var/lib/docker/overlay2/3f20f90abb60811570efc5a8f22eb543c9aa0281c9791a82d48d81271a8a4d72/merged",
"UpperDir": "/var/lib/docker/overlay2/3f20f90abb60811570efc5a8f22eb543c9aa0281c9791a82d48d81271a8a4d72/diff",
"WorkDir": "/var/lib/docker/overlay2/3f20f90abb60811570efc5a8f22eb543c9aa0281c9791a82d48d81271a8a4d72/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/home/ceshi",
"Destination": "/home",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "a38b20352b92",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"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": "626aac1eaa5039cb63bd9f45285dceba43bb05a737cec8aa5e131b54b889da1b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/626aac1eaa50",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "43f9d1bfb338f1a0eeaa536a8d2a646845cf9bbc29221109124560ffa84f6c8a",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:04",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "0e9f5f104436d02c097d84143e55cb7db8d8ce04d3ddc724f6c0ba6f9f4f8486",
"EndpointID": "43f9d1bfb338f1a0eeaa536a8d2a646845cf9bbc29221109124560ffa84f6c8a",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
}
}
]
[root@VM-0-15-centos home]# ls
ceshi test.java
用查看容器信息,看到已经挂载成功
共享数据
[root@VM-0-15-centos home]# docker exec -it a38b20352b92 /bin/bash
[root@a38b20352b92 /]# cd /home
[root@a38b20352b92 home]# touch test.java
[root@a38b20352b92 home]# ls
test.java
[root@a38b20352b92 home]# read escape sequence
[root@VM-0-15-centos home]# ls
ceshi test.java
[root@VM-0-15-centos home]# cd ceshi
[root@VM-0-15-centos ceshi]# ls
test.java
[root@VM-0-15-centos ceshi]#
总结,你容器里面修改了数据,挂载到linux的目录下的文件也同步修改了,可以说是双向绑定。
安装Mysql,同步数据
#获取镜像
[root@localhost home] docker pull mysql:5.7
#运行容器 需要做数据挂载出来 配置mysql 密码
#参数
-d 后台运行
-p 端口映射
-v 卷挂载
-e 密码配置
--name 容器名字
[root@localhost home]docker run -d -p 3310:3306 -v /home/grkj/mysql/conf:/etc/mysql/conf.d -v /home/grkj/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mysql5.7 mysql:5.7
具名和匿名挂载
#匿名挂载
-v 容器内路径
-d 后台运行
-P 随机端口
--name 容器名
-v 挂载卷
#匿名挂载,在 -v 只写容器内的路径,没有写容器外的路径
[root@localhost home] docker run -d -P --name nginx01 -v /etc/nginx nginx
#查看所有的volume 的情况
[root@localhost home] docker volume ls
DRIVER VOLUME NAME
local 6998ce93e67d3d3bdd4151425ce507d7f290cf7889c488308f5e0d414c31f4c3
#具名挂载
[root@localhost data]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
5a55f9620a69b10037ab9751a5429a3b65c141d94c8d5b26ea4b9558d0a06cb9
[root@localhost data]# docker volume ls
DRIVER VOLUME NAME
local 6998ce93e67d3d3bdd4151425ce507d7f290cf7889c488308f5e0d414c31f4c3
local juming-nginx
[root@localhost data]# docker volume inspect juming-nginx
[
{
"CreatedAt": "2021-02-28T16:26:58+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
[root@localhost data]# cd /var/lib/docker/volumes/
[root@localhost volumes]# ls
6998ce93e67d3d3bdd4151425ce507d7f290cf7889c488308f5e0d414c31f4c3 juming-nginx
backingFsBlockDev metadata.db
所有的docker 容器内的卷, 没有指定目录的情况下都是在 /var/lib/docker/volumes/XXX/_data
通过具名挂载可以方便的找到一个卷,大多数情况在使用的 具名挂载
#如何确定是具名挂载还是匿名挂载,还是指定路径挂载
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:容器内路径 #指定路径挂载
#通过 -v 容器内路径 : ro rw 改变读写权限
ro readonly #只读
rw readwrite #可读可写
#一旦这个设置了容器权限,容器对我们挂载出来的内容就有限定了;
[root@localhost data]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
[root@localhost data]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx
初始Docker file
Dockerfile 就是用来构建 docker 镜像的构建文件!命令脚本。
通过这个脚本可以生成镜像,镜像是一层一层的,脚本的一个个的命令,每个脚本都是一层
#方式二
[root@localhost /]# cd home
[root@localhost home]# ls
[root@localhost home]mkdir docker-test-volume
[root@localhost docker-test-volume]# vim dockerfile1
#自定义一个 DockerFile 文件
#以 centos为镜像
FROM centos
# 挂载 文件目录
VOLUME ["volume01","volume02"]
#打印输出 结束
CMD echo "----end----"
# 进入 该文件下
CMD /bin/bash
[root@localhost docker-test-volume]# docker build -f dockerfile1 -t kelecc/centos .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
---> 300e315adb2f
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in 470a4f87a1c9
Removing intermediate container 470a4f87a1c9
---> 2e543565a263
Step 3/4 : CMD echo "----end----"
---> Running in fea835265982
Removing intermediate container fea835265982
---> ccb90b7181c0
Step 4/4 : CMD /bin/bash
---> Running in dcaf336e68c1
Removing intermediate container dcaf336e68c1
---> 006a24864b5b
Successfully built 006a24864b5b
Successfully tagged kelecc/centos:latest
[root@localhost docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kelecc/centos latest 006a24864b5b 9 seconds ago 209MB
centos latest 300e315adb2f 2 months ago 209MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
[root@localhost volumes]# docker run -it 006a24864b5b /bin/bash
#这里 volume02 volume01 就是生成的镜像 自动挂载 的 数据卷目录
[root@5c039dbbdce9 /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
#进入 新建的容器 创建一个txt ,测试是否数据同步
[root@5c039dbbdce9 /]# cd volume01
[root@5c039dbbdce9 volume01]# touch controller.txt
[root@5c039dbbdce9 volume01]# exit
#查看容器信息
[root@localhost volumes]# docker inspect 5c039dbbdce9
[
{
"Id": "5c039dbbdce92fd83b06bd33d5811e5da64411d8dd48d4ab944cf9fb90b4bff0",
"Created": "2021-03-01T12:30:19.691745796Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-03-01T12:30:20.051950488Z",
"FinishedAt": "2021-03-01T12:35:24.418866002Z"
},
"Image": "sha256:006a24864b5b1cc76c64aade84ea5175cf8e92020b6d09f7760343c9da56f86a",
"ResolvConfPath": "/var/lib/docker/containers/5c039dbbdce92fd83b06bd33d5811e5da64411d8dd48d4ab944cf9fb90b4bff0/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/5c039dbbdce92fd83b06bd33d5811e5da64411d8dd48d4ab944cf9fb90b4bff0/hostname",
"HostsPath": "/var/lib/docker/containers/5c039dbbdce92fd83b06bd33d5811e5da64411d8dd48d4ab944cf9fb90b4bff0/hosts",
"LogPath": "/var/lib/docker/containers/5c039dbbdce92fd83b06bd33d5811e5da64411d8dd48d4ab944cf9fb90b4bff0/5c039dbbdce92fd83b06bd33d5811e5da64411d8dd48d4ab944cf9fb90b4bff0-json.log",
"Name": "/fervent_ardinghelli",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"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/794eb5f4aae4619a66167528d17a82ca04d776563f84442c753b6fb6d413041c-init/diff:/var/lib/docker/overlay2/3efff6b907dcb3da9be0cbc7ba90df5a44c4840ca26d46faeab7aa1aa9b0b433/diff",
"MergedDir": "/var/lib/docker/overlay2/794eb5f4aae4619a66167528d17a82ca04d776563f84442c753b6fb6d413041c/merged",
"UpperDir": "/var/lib/docker/overlay2/794eb5f4aae4619a66167528d17a82ca04d776563f84442c753b6fb6d413041c/diff",
"WorkDir": "/var/lib/docker/overlay2/794eb5f4aae4619a66167528d17a82ca04d776563f84442c753b6fb6d413041c/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "90e784b99f887388a753f90701a9bd3440c64eaf915f3d5f7c7cb338af7fbd10",
"Source": "/var/lib/docker/volumes/90e784b99f887388a753f90701a9bd3440c64eaf915f3d5f7c7cb338af7fbd10/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "7ddd200bfab2e4b4e240475d5aaf7d6fc507de2c7f993a7cccccc1bc15c28dec",
"Source": "/var/lib/docker/volumes/7ddd200bfab2e4b4e240475d5aaf7d6fc507de2c7f993a7cccccc1bc15c28dec/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "5c039dbbdce9",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "006a24864b5b",
"Volumes": {
"volume01": {},
"volume02": {}
},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"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": "3e2c3eb0ab443678fc9a72b48cfb131d9cca6acdb2ed0d6355deb45866d514aa",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/3e2c3eb0ab44",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "1fc5103aeb1133b747d30d9efc566cca7c0ce9a27cba8ccb279ee2fa100d224e",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
可以看到 匿名挂载已经成功
#进入该目录下 可以看到数据同步成功
[root@localhost volumes]# clear
[root@localhost volumes]# cd /var/lib/docker/volumes/90e784b99f887388a753f90701a9bd3440c64eaf915f3d5f7c7cb338af7fbd10/_data
[root@localhost _data]# ls
controller.txt
[root@localhost _data]#
数据卷容器
多个容器同步数据:
#启动父容器 也就是数据卷容器
[root@localhost _data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kelecc/centos 1.0 006a24864b5b 30 hours ago 209MB
kelecc/centos latest 006a24864b5b 30 hours ago 209MB
registry.cn-hangzhou.aliyuncs.com/kelecc/kele 1.1 006a24864b5b 30 hours ago 209MB
mysql 5.7 a70d36bc331a 5 weeks ago 449MB
nginx latest f6d0b4767a6c 6 weeks ago 133MB
centos latest 300e315adb2f 2 months ago 209MB
hello-world 1.1 bf756fb1ae65 14 months ago 13.3kB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
[root@localhost _data]# docker run -it --name centos01 kelecc/centos:1.0
[root@26579f0654ed /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
#ctrl+p+q 退出当前容器 保持运行容器
[root@26579f0654ed /]# Unsupported signal: <nil>. Discarding.
[root@localhost _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
26579f0654ed kelecc/centos:1.0 "/bin/sh -c /bin/bash" About a minute ago Up About a minute centos01
[root@localhost _data]#
#启动子容器 继承父容器的数据卷
[root@localhost _data]# docker run -it --name centos02 --volumes-from centos01 kelecc/centos:1.0
#可以看到 ["volume01","volume02"] 数据卷已经同步过来
[root@c3a5d5ec65fb /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
#ctrl+p+q 退出并运行容器
# docker attch 进入当前运行的容器 centos
[root@c3a5d5ec65fb /]# [root@localhost _data]# docker attach 26579f0654ed
[root@26579f0654ed /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
#打开 volume01 文件夹 创建 一个txt
[root@26579f0654ed /]# cd volume01
[root@26579f0654ed volume01]# ls
[root@26579f0654ed volume01]# touch centos01.txt
[root@26579f0654ed volume01]# ls
centos01.txt
[root@26579f0654ed volume01]# read escape sequence
#ctrl+p+q 退出并运行容器
# docker attch 进入当前运行的容器 centos
[root@localhost _data]# docker attach c3a5d5ec65fb
[root@c3a5d5ec65fb /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
[root@c3a5d5ec65fb /]# cd volume01
#这里我们可以发现数据已经同步过来了
[root@c3a5d5ec65fb volume01]# ls
centos01.txt
[root@c3a5d5ec65fb volume01]#
top:小提示,删除 centos 01 的文件之后, centos 的文件依然可以访问, 这里面是一个虚拟文件系统挂载,挂载到一个路径!!!
#多个mysql同步数据
[root@localhost home]docker run -d -p 3310:3306 -v /home/grkj/mysql/conf:/etc/mysql/conf.d -v /home/grkj/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mysql01 mysql:5.7
[root@localhost home]docker run -d -p 3310:3306 -v /home/grkj/mysql/conf:/etc/mysql/conf.d -v /home/grkj/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mysql02 --volumes-from mysql01 mysql:5.7
#这个时候,可以实现两个容器数据同步!
总结:容器之间配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止!!!
但是一旦你持久化到本地,这个时候,本地的数据是不会删除的 !!!
Docker file
docker file 是用来构建 docker镜像的文件,命令参数脚本!
构建步骤!
- 编写一个docker file 文件
- docker build 构建成为一个镜像
- docker run 运行镜像
- docker push 发布镜像
查看官方是怎么构建的
官方地址:https://hub.docker.com/_/centos
我们可以看到 github 上面 dockerfile 这些指令
很多官方镜像都是基础包,很多功能没有,我们通常会自己搭自己的镜像!
DockerFile 构建过程
基础知识:
- 每个保留字关键字(指令)都是必须是大写字母
- 执行从上到下顺序执行
- #表示注释
- 每一个指令都会创建提交一个新的镜像层,并提交
docker file 是面向开发的,那我们以后要发布项目,做镜像,就需要编写 docker file 文件,这个文件是否简单!
docker file :构建文件,定义了一切的步骤,源代码
dockerImages: 通过DockerFile 构建生成的镜像,最终发布和运行的产品
docker容器: 容器就是镜像运行起来提供服务器
DockerFile 的指令
FROM #基础镜像,一切从这里开始构建
MAINTAINER #镜像是谁写的,姓名+邮箱
RUN #镜像构建的时候需要运行的命令
ADD #步骤: tomcat 镜像 这个tomcat压缩包! 添加内容
WORKDIR #镜像的工作目录
VOLUME #挂载的目录
EXPOSE #保留端口配置 相当于 docker里面的指令 -p
CMD #指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT #指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD #当构建一个被继承 DockerFile 这个时候就会运行 OBUILD 的指令,触发指定
COPY #类似ADD ,将我们文件拷贝到镜像中
ENV #构建的时候设置环境变量!
自定义一个 centos镜像
Docker hub 中99%镜像都是从这个镜像过来的 FROM scratch ,然后配置需要的软件和配置来进行的构建
创建一个 centos
FROM centos
MAINTAINER kelecc<770149701@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yun -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD ["/bin/bash"]
#开始构建
[root@localhost grkj]# vim myDockerFile-centos
[root@localhost grkj]# cat myDockerFile-centos
FROM centos
MAINTAINER kelecc<770149701@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
#构建镜像 结尾有 个 【.】 这个很重要
[root@localhost grkj]# docker build -f myDockerFile-centos -t mycentos:2.0 .
Sending build context to Docker daemon 207.2MB
Step 1/10 : FROM centos
---> 300e315adb2f
Step 2/10 : MAINTAINER kelecc<770149701@qq.com>
---> Using cache
---> b284ad2d3c26
Step 3/10 : ENV MYPATH /usr/local
---> Using cache
---> cc219c7e22dd
Step 4/10 : WORKDIR $MYPATH
---> Using cache
---> fb746bb2482a
Step 5/10 : RUN yum -y install vim
---> Running in c0b8f57ff1cd
CentOS Linux 8 - AppStream 1.7 MB/s | 6.3 MB 00:03
CentOS Linux 8 - BaseOS 504 kB/s | 2.3 MB 00:04
CentOS Linux 8 - Extras 3.1 kB/s | 9.0 kB 00:02
Last metadata expiration check: 0:00:01 ago on Mon Mar 1 14:23:40 2021.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-15.el8 appstream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-15.el8 appstream 39 k
vim-common x86_64 2:8.0.1763-15.el8 appstream 6.3 M
vim-filesystem noarch 2:8.0.1763-15.el8 appstream 48 k
which x86_64 2.21-12.el8 baseos 49 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 312 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 606 kB/s | 48 kB 00:00
(3/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 1.6 MB/s | 1.4 MB 00:00
(4/5): vim-common-8.0.1763-15.el8.x86_64.rpm 1.8 MB/s | 6.3 MB 00:03
(5/5): which-2.21-12.el8.x86_64.rpm 9.3 kB/s | 49 kB 00:05
--------------------------------------------------------------------------------
Total 844 kB/s | 7.8 MB 00:09
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : which-2.21-12.el8.x86_64 1/5
Installing : vim-filesystem-2:8.0.1763-15.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-15.el8.x86_64 3/5
Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5
Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5
Installing : vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-15.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-15.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-15.el8.noarch 4/5
Verifying : which-2.21-12.el8.x86_64 5/5
Installed:
gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-15.el8.x86_64
vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch
which-2.21-12.el8.x86_64
Complete!
Removing intermediate container c0b8f57ff1cd
---> 22f5df8ebf2d
Step 6/10 : RUN yum -y install net-tools
---> Running in 4514c605be68
Last metadata expiration check: 0:00:17 ago on Mon Mar 1 14:23:40 2021.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.52.20160912git.el8 baseos 322 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 603 kB/s | 322 kB 00:00
--------------------------------------------------------------------------------
Total 322 kB/s | 322 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.52.20160912git.el8.x86_64
Complete!
Removing intermediate container 4514c605be68
---> 2a8485ded39b
Step 7/10 : EXPOSE 80
---> Running in f5d7ba1eb1ed
Removing intermediate container f5d7ba1eb1ed
---> b8f330154e88
Step 8/10 : CMD echo $MYPATH
---> Running in db3a63473535
Removing intermediate container db3a63473535
---> b87d77efb0ae
Step 9/10 : CMD echo "----end----"
---> Running in fdea3e39ca30
Removing intermediate container fdea3e39ca30
---> 7da6ea558318
Step 10/10 : CMD /bin/bash
---> Running in 674ee6bb3970
Removing intermediate container 674ee6bb3970
---> bf128d87affe
Successfully built bf128d87affe
Successfully tagged mycentos:2.0
#构建成功 查看镜像
[root@localhost grkj]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 2.0 bf128d87affe 4 minutes ago 291MB
[root@localhost grkj]#
查看官方镜像如何生成
[root@localhost grkj]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 2.0 bf128d87affe 9 minutes ago 291MB
kelecc/centos 1.0 006a24864b5b 31 hours ago 209MB
kelecc/centos latest 006a24864b5b 31 hours ago 209MB
registry.cn-hangzhou.aliyuncs.com/kelecc/kele 1.1 006a24864b5b 31 hours ago 209MB
mysql 5.7 a70d36bc331a 5 weeks ago 449MB
nginx latest f6d0b4767a6c 6 weeks ago 133MB
centos latest 300e315adb2f 2 months ago 209MB
hello-world 1.1 bf756fb1ae65 14 months ago 13.3kB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
[root@localhost grkj]# docker history a70d36bc331a
IMAGE CREATED CREATED BY SIZE COMMENT
a70d36bc331a 5 weeks ago /bin/sh -c #(nop) CMD ["mysqld"] 0B
<missing> 5 weeks ago /bin/sh -c #(nop) EXPOSE 3306 33060 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 5 weeks ago /bin/sh -c ln -s usr/local/bin/docker-entryp… 34B
<missing> 5 weeks ago /bin/sh -c #(nop) COPY file:a209112a748b68e0… 13.1kB
<missing> 5 weeks ago /bin/sh -c #(nop) VOLUME [/var/lib/mysql] 0B
<missing> 5 weeks ago /bin/sh -c { echo mysql-community-server m… 313MB
<missing> 5 weeks ago /bin/sh -c echo 'deb http://repo.mysql.com/a… 55B
<missing> 5 weeks ago /bin/sh -c #(nop) ENV MYSQL_VERSION=5.7.33-… 0B
<missing> 6 weeks ago /bin/sh -c #(nop) ENV MYSQL_MAJOR=5.7 0B
<missing> 6 weeks ago /bin/sh -c set -ex; key='A4A9406876FCBD3C45… 2.61kB
<missing> 6 weeks ago /bin/sh -c apt-get update && apt-get install… 52.2MB
<missing> 6 weeks ago /bin/sh -c mkdir /docker-entrypoint-initdb.d 0B
<missing> 6 weeks ago /bin/sh -c set -eux; savedAptMark="$(apt-ma… 4.17MB
<missing> 6 weeks ago /bin/sh -c #(nop) ENV GOSU_VERSION=1.12 0B
<missing> 6 weeks ago /bin/sh -c apt-get update && apt-get install… 9.34MB
<missing> 6 weeks ago /bin/sh -c groupadd -r mysql && useradd -r -… 329kB
<missing> 6 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 6 weeks ago /bin/sh -c #(nop) ADD file:422aca8901ae3d869… 69.2MB
[root@localhost grkj]# a70d36bc331a
构建TomCat镜像
1,准备镜像文件 tomcat 压缩包,jdk的压缩包!
2,编写 DockerFile 文件 官方命名 DockerFile
FROM centos
MAINTAINER kelecc<770149701@qq.com>
ADD jdk-8u161-linux-x64.tar.gz /usr/local
ADD apache-tomcat-8.5.63.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_161
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.63
ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.63
ENV PATH $PATH:$JAVA_HOME/bin:CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-8.5.63/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.63/logs/catalina.out
3,构建镜像
[root@localhost grkj]docker build -t tomcat8.5:1.0 .
4, 启动镜像 ,挂载文件
-d #后台运行
-p #绑定端口
--name #镜像名称
-v #挂载数据卷
[root@localhost grkj]docker run -d -p 8080:8080 -v /home/grkj/tomcat8.5/webpps:/usr/local/apache-tomcat-8.5.63/webapps -v /home/grkj/tomcat8.5/logs:/usr/local/apache-tomcat-8.5.63/logs --name tomcat8.5 tomcat8.5
发布镜像到 Docker hub
1.地址:https://hub.docker.com/ 注册自己的账号!
2,在我们的服务器上提交自己的镜像
[root@localhost volumes]# docker login --help
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
[root@localhost volumes]# docker login -u xxxx -p xxxx
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@localhost volumes]# docker pull keleyo/kelecc/hello-world:1.1
发布镜像到阿里云容器镜像服务
创建命名空间
创建镜像仓库
帮助文档。push镜像所有步骤
点击仓库名称进入 帮助文档