将应用和环境打包成一个镜像!
数据?如果数据都在容器中,那么我们容器删除,数据就会丢失!需求:数据可以持久化
MySQL,容器删除了,删库跑路!需求:MySQL数据可以存储在本地!
容器之间可以有一个数据共享的技术!Docker容器中产生的数据,同步到本地!
这就是卷技术!目录的挂载,将我们容器内的目录,挂载到Linux上面!
总结一句话:容器的持久化和同步操作!容器间也是可以数据共享的!
方式一 :直接使用命令挂载 -v
-v, --volume list Bind mount a volume
docker run -it -v 主机目录:容器内目录 -p 主机端口:容器内端口
# /home/ceshi:主机home目录下的ceshi文件夹 映射:centos容器中的/home
[root@wanghailin /]# docker run -it -v /home/ceshi:/home centos /bin/bash
#这时候主机的/home/ceshi文件夹就和容器的/home文件夹关联了,二者可以实现文件或数据同步了
#通过 docker inspect 容器id 查看
[root@wanghailin /]# docker inspect e0d4db489b9d
测试文件的同步
再来测试!
1、停止容器
2、宿主机修改文件
3、启动容器
4、容器内的数据依旧是同步的
好处:我们以后修改只需要在本地修改即可,容器内会自动同步!
思考:MySQL的数据持久化的问题
# 获取mysql镜像
[root@iz2zeak7sgj6i7hrb2g862z home]# docker pull mysql:5.7
# 运行容器,需要做数据挂载 #安装启动mysql,需要配置密码的,这是要注意点!
# 参考官网hub
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
#启动我们得
-d 后台运行
-p 端口映射
-v 卷挂载
-e 环境配置
-- name 容器名字
$ docker run -d -p 9091:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql03 mysql:5.7
# 启动成功之后,我们在本地使用sqlyog来测试一下
# sqlyog-连接到服务器的3306--和容器内的3306映射
# 在本地测试创建一个数据库,查看一下我们映射的路径是否ok!
测试连接:注意9091端口要在阿里云服务器的安全组中打开,否则无法连接。
当我们在本地用SQLyog新建名称为test的数据库时候,容器容器也会创建
root@05f2f3c72e4f:/# cd var/lib/mysql
root@05f2f3c72e4f:/var/lib/mysql# ls
auto.cnf ca.pem client-key.pem ib_logfile0 ibdata1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile1 ibtmp1 performance_schema public_key.pem server-key.pem test
root@05f2f3c72e4f:/var/lib/mysql#
[root@wanghailin mysql]# cd data
[root@wanghailin data]# ls
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem test
[root@wanghailin data]#
#可以看到 镜像容器与Linux目录下都同步了test文件
假设我们将包含mysql的容器删除后,本地的数据依旧保存在本地!
# 匿名挂载
-v 容器内路径!
$ docker run -d -P --name nginx01 -v /etc/nginx nginx
# 查看所有的volume(卷)的情况
$ docker volume ls
DRIVER VOLUME NAME # 容器内的卷名(匿名卷挂载)
local 21159a8518abd468728cdbe8594a75b204a10c26be6c36090cde1ee88965f0d0
local b17f52d38f528893dd5720899f555caf22b31bf50b0680e7c6d5431dbda2802c
# 这里发现,这种就是匿名挂载,我们在 -v只写了容器内的路径,没有写容器外的路径!
# 具名挂载 -P:表示随机映射端口
$ docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
9663cfcb1e5a9a1548867481bfddab9fd7824a6dc4c778bf438a040fe891f0ee
# 查看所有的volume(卷)的情况
$ docker volume ls
DRIVER VOLUME NAME
local 21159a8518abd468728cdbe8594a75b204a10c26be6c36090cde1ee88965f0d0
local b17f52d38f528893dd5720899f555caf22b31bf50b0680e7c6d5431dbda2802c
local juming-nginx #多了一个名字
# 通过 -v 卷名:查看容器内路径
# 查看一下这个卷
[root@wanghailin ~]# docker volume inspect juming-nginx
[
{
"CreatedAt": "2022-01-25T09:39:12+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/www/server/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
所有的docker容器内的卷,没有指定目录的情况下都是在**/var/lib/docker/volumes/自定义的卷名/_data**下
注意:这里如果是使用宝塔面板进行安装的话:其所有的docker容器内的卷,没有指定目录的情况下都是在**/www/server/docker/volumes/自定义的卷名/_data**下
如果指定了目录,docker volume ls 是查看不到的。
区分三种挂载方式
# 三种挂载: 匿名挂载、具名挂载、指定路径挂载
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:容器内路径 #指定路径挂载 docker volume ls 是查看不到的
拓展:
# 通过 -v 容器内路径: ro rw 改变读写权限
ro #readonly 只读
rw #readwrite 可读可写
$ docker run -d -P --name nginx05 -v juming:/etc/nginx:ro nginx
$ docker run -d -P --name nginx05 -v juming:/etc/nginx:rw nginx
# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法操作!
Dockerfile 就是用来构建docker镜像的构建文件!命令脚本!先体验一下!
通过这个脚本可以生成镜像,镜像是一层一层的,脚本是一个个的命令,每个命令都是一层!
# 创建一个dockerfile文件,名字可以随便 建议Dockerfile
[root@wanghailin ~]# cd /home
[root@wanghailin home]# ls
ceshi mysql redis springboot test.java www
[root@wanghailin home]# mkdir docker-test-volume
[root@wanghailin home]# ls
ceshi docker-test-volume mysql redis springboot test.java www
# 文件中的内容: 指令(大写) + 参数
$ vim dockerfile1
FROM centos # 当前这个镜像是以centos为基础的
VOLUME ["volume01","volume02"] # 挂载卷的卷目录列表(多个目录)
CMD echo "-----end-----" # 输出一下用于测试
CMD /bin/bash # 默认走bash控制台
# 这里的每个命令,就是镜像的一层!
# 构建出这个镜像
-f dockerfile1 # f代表file,指这个当前文件的地址(这里是当前目录下的dockerfile1)
-t caoshipeng/centos # t就代表target,指目标目录(注意caoshipeng镜像名前不能加斜杠‘/’)
. # 表示生成在当前目录下
[root@wanghailin home]# docker build -f dockerfile1 -t whl/centos .
Sending build context to Docker daemon 2.56kB
Step 1/4 : FROM centos
latest: Pulling from library/centos
8a29a15cefae: Already exists
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest
---> 470671670cac
Step 2/4 : VOLUME ["volume01","volume02"] # 卷名列表
---> Running in c18eefc2c233
Removing intermediate container c18eefc2c233
---> 623ae1d40fb8
Step 3/4 : CMD echo "-----end-----" # 输出 脚本命令
---> Running in 70e403669f3c
Removing intermediate container 70e403669f3c
---> 0eba1989c4e6
Step 4/4 : CMD /bin/bash
---> Running in 4342feb3a05b
Removing intermediate container 4342feb3a05b
---> f4a6b0d4d948
Successfully built f4a6b0d4d948
Successfully tagged caoshipeng/centos:latest
# 查看自己构建的镜像
[root@wanghailin home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
whl/centos latest f51fcb54edbc About a minute ago 231MB
启动自己写的容器镜像
$ docker run -it f4a6b0d4d948 /bin/bash # 运行自己写的镜像
$ ls -l # 查看目录
[root@wanghailin home]# docker run -it f51fcb54edbc /bin/bash
[root@801da7caaff6 /]# ls -l
total 56
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Jan 25 02:12 dev
drwxr-xr-x 1 root root 4096 Jan 25 02:12 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 14:17 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 125 root root 0 Jan 25 02:12 proc
dr-xr-x--- 2 root root 4096 Sep 15 14:17 root
drwxr-xr-x 11 root root 4096 Sep 15 14:17 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 Jan 25 02:12 sys
drwxrwxrwt 7 root root 4096 Sep 15 14:17 tmp
drwxr-xr-x 12 root root 4096 Sep 15 14:17 usr
drwxr-xr-x 20 root root 4096 Sep 15 14:17 var
drwxr-xr-x 2 root root 4096 Jan 25 02:12 volume01 #这个目录就是我们生成镜像的时候自动挂载的,数据卷目录
drwxr-xr-x 2 root root 4096 Jan 25 02:12 volume02 #这个目录就是我们生成镜像的时候自动挂载的,数据卷目录
这个卷和外部一定有一个同步的目录
查看一下卷挂载
# docker inspect 容器id
$ docker inspect ca3b45913df5
试一下刚才的文件是否同步出去了!
#我们在容器卷内新建一个文件
[root@801da7caaff6 /]# cd /volume01
[root@801da7caaff6 volume01]# touch container.md
[root@801da7caaff6 volume01]# ls
container.md
[root@801da7caaff6 volume01]#
接着我们回到宿主机进行查看测试:
[root@wanghailin ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
801da7caaff6 f51fcb54edbc "/bin/bash" 7 minutes ago Up 7 minutes pedantic_moser
df156cfeb15c nginx "/docker-entrypoint.…" 40 minutes ago Up 40 minutes 0.0.0.0:49153->80/tcp nginx02
[root@wanghailin ~]# docker inspect 801da7caaff6
[
{
"Id": "801da7caaff63c74a0d44d3db11542b7f0f1da97463908c9bc34420dc3e409bd",
"Created": "2022-01-25T02:12:05.697045646Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4412,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-25T02:12:06.405716552Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:f51fcb54edbc6afc345cbde9233622d3c9da11f9f13dd7e1657990cef4cb0de6",
"ResolvConfPath": "/www/server/docker/containers/801da7caaff63c74a0d44d3db11542b7f0f1da97463908c9bc34420dc3e409bd/resolv.c
"HostnamePath": "/www/server/docker/containers/801da7caaff63c74a0d44d3db11542b7f0f1da97463908c9bc34420dc3e409bd/hostname",
"HostsPath": "/www/server/docker/containers/801da7caaff63c74a0d44d3db11542b7f0f1da97463908c9bc34420dc3e409bd/hosts",
"LogPath": "/www/server/docker/containers/801da7caaff63c74a0d44d3db11542b7f0f1da97463908c9bc34420dc3e409bd/801da7caaff63c7g",
"Name": "/pedantic_moser",
"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": "/www/server/docker/overlay2/e2a02ff8ab317acb073a55106691dec2fc6046597f04f13208fb725965071029-init/dif3cdbe8167a0bbe5ce8b09cbf3f784dfb78dd6/diff",
"MergedDir": "/www/server/docker/overlay2/e2a02ff8ab317acb073a55106691dec2fc6046597f04f13208fb725965071029/merged"
"UpperDir": "/www/server/docker/overlay2/e2a02ff8ab317acb073a55106691dec2fc6046597f04f13208fb725965071029/diff",
"WorkDir": "/www/server/docker/overlay2/e2a02ff8ab317acb073a55106691dec2fc6046597f04f13208fb725965071029/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "361107787e72acbfe00278354240191c37eeb7531778cbcc9413faba8b1d4ef9",
"Source": "/www/server/docker/volumes/361107787e72acbfe00278354240191c37eeb7531778cbcc9413faba8b1d4ef9/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "c38d7c3e20dfda413b31ed2ae0cada07e3c9a0079ceaa923c67f3483fadd815d",
"Source": "/www/server/docker/volumes/c38d7c3e20dfda413b31ed2ae0cada07e3c9a0079ceaa923c67f3483fadd815d/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "801da7caaff6",
"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": "f51fcb54edbc",
"Volumes": {
"volume01": {},
"volume02": {}
},
"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": "ba93690e89f196beea6f70d0683bf8213d156ee4c887cb2c637953b70bcbd8ed",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/ba93690e89f1",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "f1fbcc96c0eaff7022b17266db8fbff3d44ffd42a51f44aa697fa314934ae919",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "dca1bc7b3838e3cf1895a027ec2d96b36773b754ce2e56ecf056515747ba270c",
"EndpointID": "f1fbcc96c0eaff7022b17266db8fbff3d44ffd42a51f44aa697fa314934ae919",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
[root@wanghailin ~]# cd /www/server/docker/volumes/361107787e72acbfe00278354240191c37eeb7531778cbcc9413faba8b1d4ef9/_data
[root@wanghailin _data]# ls
container.md #可以发现,我们在容器卷中写入的文件,宿主机中也同步了过来。
[root@wanghailin _data]#
这种方式使用的十分多,因为我们通常会构建自己的镜像!
假设构建镜像时候没有挂载卷,要手动镜像挂载 -v 卷名:容器内路径!
多个MySQL同步数据!
命名的容器挂载数据卷
# 测试 启动3个容器,通过刚才自己写的镜像启动
# 创建docker01:因为我本机是最新版,故这里用latest
[root@wanghailin home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
whl/centos latest f51fcb54edbc 31 minutes ago 231MB
nginx latest 605c77e624dd 3 weeks ago 141MB
tomcat latest fb5657adc892 4 weeks ago 680MB
mysql 5.7 c20987f18b13 5 weeks ago 448MB
centos latest 5d0da3dc9764 4 months ago 231MB
portainer/portainer latest 580c0e4e98b0 10 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 22 months ago 791MB
[root@wanghailin home]# docker run -it --name docker01 whl/centos
# 查看容器docekr01内容
[root@af4c3682d4c1 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
# 不关闭该容器退出
CTRL + Q + P
# 创建docker02: 并且让docker02 继承 docker01
[root@wanghailin home]# docker run -it --name docker02 --volumes-from docker01 whl/centos
# 查看容器docker02内容
[root@4c9e3216c9f4 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
测试:我们在docker02的volume01中创建一个文件docker02.txt
#查看docker02中的文件
[root@4c9e3216c9f4 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
[root@4c9e3216c9f4 /]# cd volume01
[root@4c9e3216c9f4 volume01]# ls
#在docker02的volume01中创建一个文件docker02.txt
[root@4c9e3216c9f4 volume01]# touch docker02.txt
[root@4c9e3216c9f4 volume01]# [root@wanghailin home]#
#进入docker01中来进行查看
[root@wanghailin home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c9e3216c9f4 whl/centos "/bin/sh -c /bin/bash" 2 minutes ago Up 2 minutes docker02
af4c3682d4c1 whl/centos "/bin/sh -c /bin/bash" 4 minutes ago Up 4 minutes docker01
[root@wanghailin home]# docker attach af4c3682d4c1
[root@af4c3682d4c1 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
[root@af4c3682d4c1 /]# cd volume01
[root@af4c3682d4c1 volume01]# ls
docker02.txt #可以发现docker02中创建的文件,被同步到了docker01中
# 再新建一个docker03同样继承docker01
$ docker run -it --name docker03 --volumes-from docker01 docker01 whl/centos
$ cd volume01 #进入volume01 查看是否也同步docker01的数据
$ ls
docker01.txt
# 测试:可以删除docker01,查看一下docker02和docker03是否可以访问这个文件
# 测试发现:数据依旧保留在docker02和docker03中没有被删除
多个mysql实现数据共享
$ docker run -d -p 3306: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 run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7
# 这个时候,可以实现两个容器数据同步!
结论:
容器之间的配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止。
但是一旦你持久化到了本地,这个时候,本地的数据是不会删除的!
dockerfile是用来构建docker镜像的文件!命令参数脚本!
构建步骤:
1、 编写一个dockerfile文件
2、 docker build 构建称为一个镜像
3、 docker run运行镜像
4、 docker push发布镜像(DockerHub 、阿里云仓库)
点击后跳到一个Dockerfile
很多官方镜像都是基础包,很多功能没有,我们通常会自己搭建自己的镜像!
官方既然可以制作镜像,那我们也可以!
基础知识:
1、每个保留关键字(指令)都是必须是大写字母
2、执行从上到下顺序
3、#表示注释
4、每一个指令都会创建提交一个新的镜像曾,并提交!
Dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简单!
Docker镜像逐渐成企业交付的标准,必须要掌握!
DockerFile:构建文件,定义了一切的步骤,源代码
DockerImages:通过DockerFile构建生成的镜像,最终发布和运行产品。
Docker容器:容器就是镜像运行起来提供服务。
FROM # from:基础镜像,一切从这里开始构建
MAINTAINER # maintainer:镜像是谁写的, 姓名+邮箱
RUN # run:镜像构建的时候需要运行的命令
ADD # add:步骤,tomcat镜像,这个tomcat压缩包!添加内容 添加同目录
WORKDIR # workdir:镜像的工作目录
VOLUME # volume:挂载的目录
EXPOSE # expose:保留端口配置
CMD # cmd:指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT # entrypoint:指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD # onbuild:当构建一个被继承DockerFile这个时候就会运行onbuild的指令,触发指令
COPY # copy:类似ADD,将我们文件拷贝到镜像中
ENV # env:构建的时候设置环境变量!
scratch 镜像
FROM scratch
ADD centos-7-x86_64-docker.tar.xz /
LABEL \
org.label-schema.schema-version="1.0" \
org.label-schema.name="CentOS Base Image" \
org.label-schema.vendor="CentOS" \
org.label-schema.license="GPLv2" \
org.label-schema.build-date="20200504" \
org.opencontainers.image.title="CentOS Base Image" \
org.opencontainers.image.vendor="CentOS" \
org.opencontainers.image.licenses="GPL-2.0-only" \
org.opencontainers.image.created="2020-05-04 00:00:00+01:00"
CMD ["/bin/bash"]
Docker Hub 中 99%的镜像都是从这个基础镜像过来的 FROM scratch,然后配置需要的软件和配置来进行构建。
创建一个自己的centos
# 1./home下新建dockerfile目录
$ mkdir dockerfile
# 2. dockerfile目录下新建mydockerfile-centos文件
$ vim mydockerfile-centos
# 3.编写Dockerfile配置文件
FROM centos # 基础镜像是官方原生的centos
MAINTAINER whl<2076312724@qq.com> # 作者
ENV MYPATH /usr/local # 配置环境变量的目录
WORKDIR $MYPATH # 将工作目录设置为 MYPATH
RUN yum -y install vim # 给官方原生的centos 增加 vim指令
RUN yum -y install net-tools # 给官方原生的centos 增加 ifconfig命令
EXPOSE 80 # 暴露端口号为80
CMD echo $MYPATH # 输出下 MYPATH 路径
CMD echo "-----end----"
CMD /bin/bash # 启动后进入 /bin/bash
# 4.通过这个文件构建镜像
# 命令: docker build -f 文件路径 -t 镜像名:[tag] .
$ docker build -f mydockerfile-centos -t mycentos:0.1 .
# 5.出现下图后则构建成功
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 cbf5110a646d 2 minutes ago 311MB
# 6.测试运行
$ docker run -it mycentos:0.1 # 注意带上版本号,否则每次都回去找最新版latest
$ pwd
/usr/local # 与Dockerfile文件中 WORKDIR 设置的 MYPATH 一致
$ vim # vim 指令可以使用
$ ifconfig # ifconfig 指令可以使用
# docker history 镜像id #我们可以列出本地进行的变更历史,查看镜像构建历史步骤
$ docker history 镜像id
CMD 和 ENTRYPOINT区别
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代。
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
测试cmd
# 编写dockerfile文件
$ vim dockerfile-test-cmd
FROM centos
CMD ["ls","-a"] # 启动后执行 ls -a 命令
# 构建镜像
$ docker build -f dockerfile-test-cmd -t cmd-test:0.1 .
# 运行镜像
$ docker run cmd-test:0.1 # 由结果可得,运行后就执行了 ls -a 命令
.
..
.dockerenv
bin
dev
etc
home
# 想追加一个命令 -l 成为ls -al:展示列表详细数据
$ docker run cmd-test:0.1 -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-l\":
executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled
# cmd的情况下 -l 替换了CMD["ls","-l"] 而 -l 不是命令所以报错
测试ENTRYPOINT
# 编写dockerfile文件
$ vim dockerfile-test-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
# 构建镜像
$ docker build -f dockerfile-test-entrypoint -t cmd-test:0.1 .
# 运行镜像
$ docker run entrypoint-test:0.1
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found ...
# 我们的命令,是直接拼接在我们得ENTRYPOINT命令后面的
$ docker run entrypoint-test:0.1 -l
total 56
drwxr-xr-x 1 root root 4096 May 16 06:32 .
drwxr-xr-x 1 root root 4096 May 16 06:32 ..
-rwxr-xr-x 1 root root 0 May 16 06:32 .dockerenv
lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x 5 root root 340 May 16 06:32 dev
drwxr-xr-x 1 root root 4096 May 16 06:32 etc
drwxr-xr-x 2 root root 4096 May 11 2019 home
lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64 ....
Dockerfile中很多命令都十分的相似,我们需要了解它们的区别,我们最好的学习就是对比他们然后测试效果!
准备tomcat 和 jdk 到当前目录,编写好README
$ vim dockerfile
FROM centos # 基础镜像centos
MAINTAINER whl<2076312724@qq.com> # 作者
COPY README /usr/local/README # 复制README文件
ADD jdk-8u231-linux-x64.tar.gz /usr/local/ # 添加jdk,ADD 命令会自动解压
ADD apache-tomcat-9.0.35.tar.gz /usr/local/ # 添加tomcat,ADD 命令会自动解压
RUN yum -y install vim # 安装 vim 命令
ENV MYPATH /usr/local # 环境变量设置 工作目录
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_231 # 环境变量: JAVA_HOME环境变量
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.35 # 环境变量: tomcat环境变量
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.35
# 设置环境变量 分隔符是:
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080 # 设置暴露的端口
CMD /usr/local/apache-tomcat-9.0.35/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.35/logs/catalina.out # 设置默认命令
# 因为dockerfile命名使用默认命名 因此不用使用-f 指定文件
$ docker build -t mytomcat:0.1 .
# -d:后台运行 -p:暴露端口 --name:别名 -v:绑定路径
$ docker run -d -p 8080:8080 --name tomcat01
-v /home/kuangshen/build/tomcat/test:/usr/local/apache-tomcat-9.0.35/webapps/test
-v /home/kuangshen/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.35/logs mytomcat:0.1
$ docker exec -it 自定义容器的id /bin/bash
$ cul localhost:8080
(由于做了卷挂载,我们直接在本地编写项目就可以发布了!)
发现:项目部署成功,可以直接访问!
我们以后开发的步骤:需要掌握Dockerfile的编写!我们之后的一切都是使用docker镜像来发布运行!
发布到 Docker Hub
1、地址 https://hub.docker.com/
2、确定这个账号可以登录
3、登录
$ 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
#登录dockerhub
$ docker login -u 你的用户名 -p 你的密码
#提交本地的镜像到 dockerhub
[root@wanghailin ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 6efb7d573f6f 3 hours ago 326MB
whl/centos latest f51fcb54edbc 4 hours ago 231MB
nginx latest 605c77e624dd 3 weeks ago 141MB
tomcat latest fb5657adc892 4 weeks ago 680MB
mysql 5.7 c20987f18b13 5 weeks ago 448MB
centos latest 5d0da3dc9764 4 months ago 231MB
portainer/portainer latest 580c0e4e98b0 10 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 22 months ago 791MB
# 会发现push不上去,因为如果没有前缀的话默认是push到 官方的library
# 解决方法:
# 第一种 build的时候添加你的dockerhub用户名,然后在push就可以放到自己的仓库了
$ docker build -t kuangshen/mytomcat:0.1 .
# 第二种 使用docker tag #然后再次push
$ docker tag 容器id kuangshen/mytomcat:1.0 #然后再次push
$ docker push kuangshen/mytomcat:1.0
#经过测试:第二种方法有效!!!
[root@wanghailin ~]# docker tag 6efb7d573f6f iswhl/mycentos:0.1
[root@wanghailin ~]# docker push iswhl/mycentos:0.1
发布到 阿里云镜像服务上
看官网 很详细https://cr.console.aliyun.com/repository/
1、登录阿里云
2、找到容器镜像服务
3、创建命名空间
1. 登录阿里云Docker Registry
$ docker login --username=demo**** registry.cn-hangzhou.aliyuncs.com
2. 从Registry中拉取镜像
$ docker pull registry.cn-hangzhou.aliyuncs.com/iswhl/iswhl_test:[镜像版本号]
3. 将镜像推送到Registry
$ docker login --username=demo**** registry.cn-hangzhou.aliyuncs.com
$ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/iswhl/iswhl_test:[镜像版本号]
$ docker push registry.cn-hangzhou.aliyuncs.com/iswhl/iswhl_test:[镜像版本号]
#列子:
[root@wanghailin ~]# clear
[root@wanghailin ~]# docker login --username=demo_*** registry.cn-hangzhou.aliyuncs.com
Password:
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@wanghailin ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 6efb7d573f6f 4 hours ago 326MB
iswhl/mycentos 0.1 6efb7d573f6f 4 hours ago 326MB
whl/centos latest f51fcb54edbc 5 hours ago 231MB
nginx latest 605c77e624dd 3 weeks ago 141MB
tomcat latest fb5657adc892 4 weeks ago 680MB
mysql 5.7 c20987f18b13 5 weeks ago 448MB
centos latest 5d0da3dc9764 4 months ago 231MB
portainer/portainer latest 580c0e4e98b0 10 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 22 months ago 791MB
[root@wanghailin ~]# docker push iswhl/mycentos:0.1
The push refers to repository [docker.io/iswhl/mycentos]
148383061f98: Layer already exists
59c7a2c71fb0: Layer already exists
74ddd0ec08fa: Pushing [===========> ] 51.67MB/231.3MB
学习之前清空下前面的docker 镜像、容器
# 删除全部容器
$ docker rm -f $(docker ps -aq)
# 删除全部镜像
$ docker rmi -f $(docker images -aq)
测试
三个网络
问题: docker 是如果处理容器网络访问的?
# 测试 运行一个tomcat
$ docker run -d -P --name tomcat01 tomcat
# 查看容器内部网络地址
$ docker exec -it 容器id ip addr
# 发现容器启动的时候会得到一个 eth0@if91 ip地址,docker分配!
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
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
261: eth0@if91: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.18.0.2/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
# 思考? linux能不能ping通容器内部! 可以 容器内部可以ping通外界吗? 可以!
$ ping 172.18.0.2
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.069 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.074 ms
原理
1、我们每启动一个docker容器,docker就会给docker容器分配一个ip,我们只要按照了docker,就会有一个docker0桥接模式,使用的技术是veth-pair技术!
https://www.cnblogs.com/bakari/p/10613710.html
再次测试 ip addr
2 、再启动一个容器测试,发现又多了一对网络
# 我们发现这个容器带来网卡,都是一对对的
# veth-pair 就是一对的虚拟设备接口,他们都是成对出现的,一端连着协议,一端彼此相连
# 正因为有这个特性 veth-pair 充当一个桥梁,连接各种虚拟网络设备的
# OpenStac,Docker容器之间的连接,OVS的连接,都是使用evth-pair技术
3、我们来测试下tomcat01和tomcat02是否可以ping通
# 获取tomcat01的ip 172.17.0.2
$ docker-tomcat docker exec -it tomcat01 ip addr
550: eth0@if551: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
# 让tomcat02 ping tomcat01
$ docker-tomcat docker exec -it tomcat02 ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.098 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.071 ms
# 结论:容器和容器之间是可以互相ping通
网络模型图
结论:tomcat01和tomcat02公用一个路由器,docker0。
所有的容器不指定网络的情况下,都是docker0路由的,docker会给我们的容器分配一个默认的可用ip。
小结
Docker使用的是Linux的桥接,宿主机是一个Docker容器的网桥 docker0
Docker中所有网络接口都是虚拟的,虚拟的转发效率高(内网传递文件)
只要容器删除,对应的网桥一对就没了!
思考一个场景:我们编写了一个微服务,database url=ip: 项目不重启,数据ip换了,我们希望可以处理这个问题,可以通过名字来进行访问容器?
$ docker exec -it tomcat02 ping tomca01 # ping不通
ping: tomca01: Name or service not known
# 运行一个tomcat03 --link tomcat02
$ docker run -d -P --name tomcat03 --link tomcat02 tomcat
5f9331566980a9e92bc54681caaac14e9fc993f14ad13d98534026c08c0a9aef
# 3连接2
# 用tomcat03 ping tomcat02 可以ping通
$ docker exec -it tomcat03 ping tomcat02
PING tomcat02 (172.17.0.3) 56(84) bytes of data.
64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.115 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.080 ms
# 2连接3
# 用tomcat02 ping tomcat03 ping不通
探究:
docker network inspect 网络id 网段相同
docker inspect tomcat03
查看tomcat03里面的/etc/hosts发现有tomcat02的配置
–link 本质就是在hosts配置中添加映射
现在使用Docker已经不建议使用–link了!
自定义网络,不适用docker0!
docker0问题:不支持容器名连接访问!
docker network
connect -- Connect a container to a network
create -- Creates a new network with a name specified by the
disconnect -- Disconnects a container from a network
inspect -- Displays detailed information on a network
ls -- Lists all the networks created by the user
prune -- Remove all unused networks
rm -- Deletes one or more networks
查看所有的docker网络
bridge :桥接 docker(默认,自己创建也是用bridge模式)
none :不配置网络,一般不用
host :和所主机共享网络
container :容器网络连通(用得少!局限很大)
测试
# 我们直接启动的命令 --net bridge,而这个就是我们得docker0
# bridge就是docker0
$ docker run -d -P --name tomcat01 tomcat
等价于 => docker run -d -P --name tomcat01 --net bridge tomcat
# docker0,特点:默认,域名不能访问。 --link可以打通连接,但是很麻烦!
# 我们可以 自定义一个网络
$ docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
$ docker network inspect mynet;
启动两个tomcat,再次查看网络情况
在自定义的网络下,服务可以互相ping通,不用使用–link
我们自定义的网络docker当我们维护好了对应的关系,推荐我们平时这样使用网络!
好处:
redis -不同的集群使用不同的网络,保证集群是安全和健康的
mysql-不同的集群使用不同的网络,保证集群是安全和健康的
# 测试两个不同的网络连通 再启动两个tomcat 使用默认网络,即docker0
$ docker run -d -P --name tomcat01 tomcat
$ docker run -d -P --name tomcat02 tomcat
# 此时ping不通
# 要将tomcat01 连通 tomcat—net-01 ,连通就是将 tomcat01加到 mynet网络
# 一个容器两个ip(tomcat01)
# 01连通 ,加入后此时,已经可以tomcat01 和 tomcat-01-net ping通了
# 02是依旧不通的
结论:假设要跨网络操作别人,就需要使用docker network connect 连通!
# 创建网卡
docker network create redis --subnet 172.38.0.0/16
# 查看一下 我们所创建的网络配置
[root@wanghailin ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
dca1bc7b3838 bridge bridge local
de35e59afa90 host host local
84678695ec47 none null local
1a69d6919ea7 redis bridge local
[root@wanghailin ~]# docker network inspect redis
[
{
"Name": "redis",
"Id": "1a69d6919ea76b99b4b7df4e88493aeebd266aca718809c51df76c8e8ef3c0bb",
"Created": "2022-01-25T18:18:30.511004408+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.38.0.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
# 通过脚本创建六个redis配置
for port in $(seq 1 6);\
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >> /mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
#演示:
[root@wanghailin ~]# for port in $(seq 1 6);\
> do \
> mkdir -p /mydata/redis/node-${port}/conf
> touch /mydata/redis/node-${port}/conf/redis.conf
> cat << EOF >> /mydata/redis/node-${port}/conf/redis.conf
> port 6379
> bind 0.0.0.0
> cluster-enabled yes
> cluster-config-file nodes.conf
> cluster-node-timeout 5000
> cluster-announce-ip 172.38.0.1${port}
> cluster-announce-port 6379
> cluster-announce-bus-port 16379
> appendonly yes
> EOF
> done
[root@wanghailin ~]#
#查看
[root@wanghailin ~]# cd mydata
-bash: cd: mydata: No such file or directory
[root@wanghailin ~]# cd /mydata/
[root@wanghailin mydata]# ls
redis
[root@wanghailin mydata]# cd redis
[root@wanghailin redis]# ls
node-1 node-2 node-3 node-4 node-5 node-6
[root@wanghailin redis]# cd node-1
[root@wanghailin node-1]# ls
conf
[root@wanghailin node-1]# cd conf
[root@wanghailin conf]# ls
redis.conf
[root@wanghailin conf]# cat redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.11
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
# 通过脚本运行六个redis(这段脚本由我自己编写,若不想用可以自己一个一个写)
for port in $(seq 1 6);\
do \
docker run -p 637${port}:6379 -p 1667${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
EOF
done
docker exec -it redis-1 /bin/sh #redis默认没有bash
#创建集群
redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
#测试:
[root@wanghailin conf]# redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: c4e889f08d85854071e1a2713b237cd63a3fcccc 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: be6d0e76181e747c2069322ef61411402cdd0954 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: efd4ed358a324444a6490d47f7765a9b3bf406a1 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: b9776ab90d56c766bb5a8cc2c600173796c77f89 172.38.0.14:6379
replicates efd4ed358a324444a6490d47f7765a9b3bf406a1
S: 440cc03f2316ba2e696b75617f81d3b375949afe 172.38.0.15:6379
replicates c4e889f08d85854071e1a2713b237cd63a3fcccc
S: b96120233c996b173a5508723d1b974e24610e25 172.38.0.16:6379
replicates be6d0e76181e747c2069322ef61411402cdd0954
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 172.38.0.11:6379)
M: c4e889f08d85854071e1a2713b237cd63a3fcccc 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 440cc03f2316ba2e696b75617f81d3b375949afe 172.38.0.15:6379
slots: (0 slots) slave
replicates c4e889f08d85854071e1a2713b237cd63a3fcccc
M: efd4ed358a324444a6490d47f7765a9b3bf406a1 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: b9776ab90d56c766bb5a8cc2c600173796c77f89 172.38.0.14:6379
slots: (0 slots) slave
replicates efd4ed358a324444a6490d47f7765a9b3bf406a1
M: be6d0e76181e747c2069322ef61411402cdd0954 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: b96120233c996b173a5508723d1b974e24610e25 172.38.0.16:6379
slots: (0 slots) slave
replicates be6d0e76181e747c2069322ef61411402cdd0954
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. #到这里docker搭建redis集群完成!
#测试:查看 所搭建的redis集群
#master 主机
#slave 从机
[root@wanghailin /]# docker exec -it redis-1 /bin/sh
/data # redis-cli -c
127.0.0.1:6379> cluster nodes
c4e889f08d85854071e1a2713b237cd63a3fcccc 172.38.0.11:6379@16379 myself,master - 0 1643107664000 1 connected 0-5460
440cc03f2316ba2e696b75617f81d3b375949afe 172.38.0.15:6379@16379 slave c4e889f08d85854071e1a2713b237cd63a3fcccc 0 1643107665382 1 connected
efd4ed358a324444a6490d47f7765a9b3bf406a1 172.38.0.13:6379@16379 master - 0 1643107666384 3 connected 10923-16383
b9776ab90d56c766bb5a8cc2c600173796c77f89 172.38.0.14:6379@16379 slave efd4ed358a324444a6490d47f7765a9b3bf406a1 0 1643107666083 4 connected
be6d0e76181e747c2069322ef61411402cdd0954 172.38.0.12:6379@16379 master - 0 1643107666000 2 connected 5461-10922
b96120233c996b173a5508723d1b974e24610e25 172.38.0.16:6379@16379 slave be6d0e76181e747c2069322ef61411402cdd0954 0 1643107665000 6 connected
127.0.0.1:6379>
#docker测试搭建redis集群完成!
我们使用docker之后,所有的技术都会慢慢变得简单起来!
1、构建SpringBoot项目
2、打包运行
mvn package
3、编写Dockerfile 可以在idea中下载docker的插件,可以进行文件的高亮
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
4、构建镜像 将jar包和Dockerfile一起上传到服务器中
#注:关闭docker镜像服务的命令
# docker rm -f $(docker ps -qa)
# 1.复制jar和DockerFIle到服务器
# 2.构建镜像
$ docker build -t xxxxx:xx .
#测试:
[root@wanghailin idea]# docker build -t iswhlboot .
Sending build context to Docker daemon 17.56MB
Step 1/5 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
---> bd2575e14c91
Step 3/5 : CMD ["--server.port=8080"]
---> Running in d7e917e85a8b
Removing intermediate container d7e917e85a8b
---> b6f4b0f1ac5f
Step 4/5 : EXPOSE 8080
---> Running in fcdb1047574b
Removing intermediate container fcdb1047574b
---> bd2a8f3d7777
Step 5/5 : ENTRYPOINT ["java","-jar","app.jar"]
---> Running in d89c6008d2cd
Removing intermediate container d89c6008d2cd
---> 728973f587bb
Successfully built 728973f587bb
Successfully tagged iswhlboot:latest
[root@wanghailin idea]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
iswhlboot latest 728973f587bb 12 seconds ago 661MB
mycentos 0.1 6efb7d573f6f 8 hours ago 326MB
iswhl/mycentos 0.1 6efb7d573f6f 8 hours ago 326MB
whl/centos latest f51fcb54edbc 9 hours ago 231MB
nginx latest 605c77e624dd 3 weeks ago 141MB
tomcat latest fb5657adc892 4 weeks ago 680MB
mysql 5.7 c20987f18b13 5 weeks ago 448MB
centos latest 5d0da3dc9764 4 months ago 231MB
portainer/portainer latest 580c0e4e98b0 10 months ago 79.1MB
redis 5.0.9-alpine3.11 3661c84ee9d0 21 months ago 29.8MB
elasticsearch 7.6.2 f29a1ee41030 22 months ago 791MB
java 8 d23bdf5b1b1b 5 years ago 643MB
# -P 这里的P为大写,指的是容器内的端口地址与我们的宿主的的端口随机进行映射,我们也可以自己进行自定义的配置
[root@wanghailin idea]# docker run -d -P --name iswhl-springboot-web-test iswhlboot
5a489b8a77ba0506a42fc45ef738ffd4531f7ccd2a8fa8ca1a7cf65bc76532c7
[root@wanghailin idea]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 这里的0.0.0.0:49154->8080/tcp 指我们宿主机的49154端口 映射到容器的8080端口上,我们可以通过49154来访问容器8080的端口
5a489b8a77ba iswhlboot "java -jar app.jar -…" 57 seconds ago Up 55 seconds 0.0.0.0:49154->8080/tcp iswhl-springboot-web-test
[root@wanghailin idea]# curl localhost:49154
{"timestamp":"2022-01-25T11:29:32.416+00:00","status":404,"error":"Not Found","path":"/"}
[root@wanghailin idea]# curl localhost:49154/hello
hello world[root@wanghailin idea]#
5、发布运行
以后我们使用了Docker之后,给别人交付就是一个镜像即可!
1 6efb7d573f6f 8 hours ago 326MB
whl/centos latest f51fcb54edbc 9 hours ago 231MB
nginx latest 605c77e624dd 3 weeks ago 141MB
tomcat latest fb5657adc892 4 weeks ago 680MB
mysql 5.7 c20987f18b13 5 weeks ago 448MB
centos latest 5d0da3dc9764 4 months ago 231MB
portainer/portainer latest 580c0e4e98b0 10 months ago 79.1MB
redis 5.0.9-alpine3.11 3661c84ee9d0 21 months ago 29.8MB
elasticsearch 7.6.2 f29a1ee41030 22 months ago 791MB
java 8 d23bdf5b1b1b 5 years ago 643MB
[root@wanghailin idea]# docker run -d -P --name iswhl-springboot-web-test iswhlboot
5a489b8a77ba0506a42fc45ef738ffd4531f7ccd2a8fa8ca1a7cf65bc76532c7
[root@wanghailin idea]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a489b8a77ba iswhlboot “java -jar app.jar -…” 57 seconds ago Up 55 seconds 0.0.0.0:49154->8080/tcp iswhl-springboot-web-test
[root@wanghailin idea]# curl localhost:49154
{“timestamp”:“2022-01-25T11:29:32.416+00:00”,“status”:404,“error”:“Not Found”,“path”:"/"}
[root@wanghailin idea]# curl localhost:49154/hello
hello world[root@wanghailin idea]#
5、发布运行
以后我们使用了Docker之后,给别人交付就是一个镜像即可!