Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
基础名词:
镜像(image):
docker镜像就好比是一个模板,可以通过这个模板来创建容器服务,tomcat镜像 => run =>tomcat01容器(提供服务),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。
容器(container):
Docker利用容器技术可以独立运行一个或者一组应用,通过镜像来创建的,启动,停止、删除、基本命令
目前就可以把这个容器理解为一个简易的linux系统
仓库(repository)
仓库就是存放镜像的地方!仓库分为公有和私有仓库!
Docker hub(默认是国外的) 阿里云…都有容器服务器(配置镜像加速!)
# 卸载旧版本的docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 下载需要的安装包
yum install -y yum-utils
# 设置镜像的仓库
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 更新yum软件包索引
yum makecache fast
# 安装Docker引擎
yum install docker-ce docker-ce-cli containerd.io
# 启动Docker
systemctl start docker
# 测试helloWorld
docker run hello-world
# 查看一下下载的这个hello-word 镜像
dcoker images
# 卸载旧版本Docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 卸载引擎
yum remove docker-ce docker-ce-cli containerd.io
# 删除目录
rm -rf /var/lib/docker
1、Docker有着比虚拟机更少的抽象层。
2、Docker利用的是虚拟主机的内核,vm需要是Guest OS。
所以说,新建的容器的时候,docker不需要虚拟机一样重新加载一个操作系统内核,避免引导。虚拟机是加载Guest OS,分钟级别,而docker是利用虚拟机主机的操作系统,忽略了这个复杂的过程,秒级别。
Docker 是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问!
DockerServer接收到Docker-Client的指令,就会执行这个命令!
docker version # 显示docker信息
docker info # 显示docker系统的信息, 宝库啊镜像和容器的数量
docker 命令 --help # 万能命令
docker search mysql # 搜索命令
[root@VM-4-8-centos ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13974 [OK]
mariadb MariaDB Server is a high performing open sou… 5327 [OK]
percona Percona Server is a fork of the MySQL relati… 602 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 768 [OK]
circleci/mysql MySQL is a widely used, open-source relation… 29
bitnami/mysql Bitnami MySQL Docker Image 80 [OK]
bitnami/mysqld-exporter 4
ubuntu/mysql MySQL open source fast, stable, multi-thread… 43
cimg/mysql 0
rapidfort/mysql RapidFort optimized, hardened image for MySQL 14
google/mysql MySQL server for Google Compute Engine 23 [OK]
ibmcom/mysql-s390x Docker image for mysql-s390x 2
rapidfort/mysql8-ib RapidFort optimized, hardened image for MySQ… 0
hashicorp/mysql-portworx-demo 0
rapidfort/mysql-official RapidFort optimized, hardened image for MySQ… 0
newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 82
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 38
mirantis/mysql 0
docksal/mysql MySQL service images for Docksal - https://d… 0
vitess/mysqlctld vitess/mysqlctld 1 [OK]
linuxserver/mysql-workbench 48
bitnamicharts/mysql 0
eclipse/mysql Mysql 5.7, curl, rsync 0 [OK]
drud/mysql 0
# 可选项,通过搜索赖过滤
--filter=STARS=3000 # 搜索出来的镜像就是STARS大于3000的
docker imgaes 查看所有镜像
[root@VM-4-8-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 18 months ago 13.3kB
canal/canal-server v1.1.5 0c7f1d62a7d8 23 months ago 874MB
mysql 5.7.25 98455b9624a9 4 years ago 372MB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
-a, --all # 列出所有镜像
-f, --filter # 过滤
-q, --quiet # 只显示镜像的id
docker pull 拉取镜像
# 指定版本拉取镜像 docker pull 镜像名[:tag] 不加标签,默认拉取最新版
[root@VM-4-8-centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
2ec521688c56: Pull complete
f911f9b90db6: Pull complete
38eb4fe7ed26: Pull complete
01f772a6b25f: Pull complete
499456357ebf: Pull complete
274f3ad0dcec: Pull complete
3e6a5b12e1de: Pull complete
de0c5c82dbbc: Pull complete
c9bd281f4600: Pull complete
3289c4277a83: Pull complete
8b66f10a11fa: Pull complete
Digest: sha256:1780318bdabc0edd36907bf91b47632eb912e8ea91258eca3590f8aca6f54836
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
# docker 执行删除镜像,根据id 或者名称
docker rmi id
# rmi删除不了正在使用的镜像,加-f强制删除
docker rmi -f id
# docker 执行批量删除镜像 通过递归方式进行删除
docker rmi -f $(docker images -aq)
先拉取一个镜像
docker pull centos
docker run [可选参数] image 新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名称 tomcat01 tomcat02 ,用来区分容器
-d 后端方式运行
-it 使用交互方式运行,进入容器查看内容
-小p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
-大p 随机指定端口
# 测试,启动并且进入容器
[root@VM-4-8-centos ~]# docker run -p 9999 -it 5d0da3dc9764
[root@4a098b3bac03 /]# ls
bin dev etc home lib lib64 lost+found media mnt
opt proc root run sbin srv sys tmp usr var
# 使用exit退出容器命令
[root@4a098b3bac03 /]# exit
exit
docker ps 命令 列出所有容器
# 列出当前正在运行的容器
docker ps 命令
-a # 列出当前正在运行的容器+带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
[root@VM-4-8-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
acdfa099761b 5d0da3dc9764 "/bin/bash" 4 seconds ago Exited (0) 1 second ago thirsty_colden
退出容器
exit # 直接容器停止并退出
Ctrl + P + Q # 容器不停止退出
删除容器
docker rm 容器id # 删除指定的容器,不能删除正在运行的容器,如果需要删除,则需要 rm -f 强制删除
docker rm -f $(docker ps -aq) # 删除所有的容器
docker ps -a -q | xargs docker rm # 删除所有的容器
启动和停止容器的操作
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止容器
docker kill 容器id # 强制杀死容器
# 命令 docker run -d 镜像名!
[root@VM-4-8-centos ~]# docker run -d centos
964f2309ef3cfcb07b534059502323db66b23953ac44da286b7ca0b247fda327
[root@VM-4-8-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
964f2309ef3c centos "/bin/bash" 23 seconds ago Exited (0) 22 seconds ago wonderful_wilbur
# 问题docker ps. 发现 centos 停止了
# 常见的坑:docker 容器使用后台运行,就必须有要一个前台进行,docker发现没有应用,就会自动的停止
# 容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
docker logs 容器id 查看日志的命令
docker logs 容器id
#参数
-f # 跟踪日志输出
-n=? # 显示多少条日志,不写默认为all
-t # 显示时间戳
docker top 容器id 查看容器中进程信息
#docker top 容器id 是查看docker内部的信息
[root@VM-4-8-centos ~]# docker top 52f808ba7f1a
UID PID PPID C STIME TTY TIME CMD
root 22004 21983 0 10:39 pts/0 00:00:00 /bin/bash
docker inspect 容器id 镜像的元数据
# 命令 docker inspect 容器id
# 测试
[root@VM-4-8-centos ~]# docker inspect 52f808ba7f1a
[
{
"Id": "52f808ba7f1a2419694fc0cd31d2fd8810a4456e5c8b0917096f5c80af164dff",
"Created": "2023-03-27T02:39:42.598864277Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 22004,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-03-27T02:39:42.943573356Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/52f808ba7f1a2419694fc0cd31d2fd8810a4456e5c8b0917096f5c80af164dff/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/52f808ba7f1a2419694fc0cd31d2fd8810a4456e5c8b0917096f5c80af164dff/hostname",
"HostsPath": "/var/lib/docker/containers/52f808ba7f1a2419694fc0cd31d2fd8810a4456e5c8b0917096f5c80af164dff/hosts",
"LogPath": "/var/lib/docker/containers/52f808ba7f1a2419694fc0cd31d2fd8810a4456e5c8b0917096f5c80af164dff/52f808ba7f1a2419694fc0cd31d2fd8810a4456e5c8b0917096f5c80af164dff-json.log",
"Name": "/suspicious_wright",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"9999/tcp": [
{
"HostIp": "",
"HostPort": ""
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
25,
188
],
"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",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"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/a8ec94947d108db6d6865b996ba0e1f9f29862e5a20741062d43e0f40bd33d92-init/diff:/var/lib/docker/overlay2/e20884d4e1b741ba886a96cdb9aacd515a0dc873e13e9ab457ba80dbeebe7ff6/diff",
"MergedDir": "/var/lib/docker/overlay2/a8ec94947d108db6d6865b996ba0e1f9f29862e5a20741062d43e0f40bd33d92/merged",
"UpperDir": "/var/lib/docker/overlay2/a8ec94947d108db6d6865b996ba0e1f9f29862e5a20741062d43e0f40bd33d92/diff",
"WorkDir": "/var/lib/docker/overlay2/a8ec94947d108db6d6865b996ba0e1f9f29862e5a20741062d43e0f40bd33d92/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "52f808ba7f1a",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"ExposedPorts": {
"9999/tcp": {}
},
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "5d0da3dc9764",
"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": "47c564b6df75a68cbfbc3f707f4a86896008e565583fdf3a1b858ce4340431b0",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"9999/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32772"
},
{
"HostIp": "::",
"HostPort": "32772"
}
]
},
"SandboxKey": "/var/run/docker/netns/47c564b6df75",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "71b4819e81e4bf1d2475fee4fe982537dd6974f76f853deeafe704af0ceca649",
"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": "0a61313f17fe621a78c4756bb9b0461723747bd050c2ff8e2465736d17b103ce",
"EndpointID": "71b4819e81e4bf1d2475fee4fe982537dd6974f76f853deeafe704af0ceca649",
"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
}
}
}
}
]
进入当前正在运行的容器
# 我们通过从容器都是使用后台运行的,需要进入容器,修改一些配置
# 方式一
# docker exec -it 容器id bashShell
[root@VM-4-8-centos ~]# docker exec -it 52f808ba7f1a /bin/bash
[root@52f808ba7f1a /]# ls
bin dev etc home lib lib64 lost+found media
mnt opt proc root run sbin srv sys tmp usr var
# 方式二
docker attach 容器id
# 测试
[root@VM-4-8-centos ~]# docker attach 6cb0fea026f8
正在执行的代码.....
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机上
# docker cp 容器id:容器内路径 目的的主机路径
[root@VM-4-8-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52f808ba7f1a 5d0da3dc9764 "/bin/bash" 4 hours ago Up 4 hours 0.0.0.0:32772->9999/tcp, :::32772->9999/tcp suspicious_wright
# 进入到容器内部
[root@VM-4-8-centos ~]# docker attach 52f808ba7f1a
[root@52f808ba7f1a /]# cd /home
[root@52f808ba7f1a home]# ls
# 在容器内部创建一个 test.java文件
[root@52f808ba7f1a home]# touch test.java
[root@52f808ba7f1a home]# ls
test.java
# 退出容器
[root@52f808ba7f1a home]# exit
exit
[root@VM-4-8-centos ~]# cd /www
[root@VM-4-8-centos www]# ls
xxl-job
# 将容器内部的文件进行拷贝到当前文件夹下面
[root@VM-4-8-centos www]# docker cp 52f808ba7f1a:/home/test.java /www
Preparing to copy...
Successfully copied 1.536kB to /www
[root@VM-4-8-centos www]# ls
test.java xxl-job
# 拷贝是一个手动过程,未来我们使用 -v 的技术,可以实现自动同步 /home /home
# 1、搜索镜像 search 去docker官网下载
[root@VM-4-8-centos ~]# docker search nginx
# 2、下载nginx
[root@VM-4-8-centos ~]# docker pull nginx
[root@VM-4-8-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ac232364af84 4 days ago 142MB
centos latest 5d0da3dc9764 18 months ago 231MB
# 3、启动nginx
# docker run -d --name nginx01 -p 3344:80 nginx
# -d 后台运行
#--name 给容器命名
# -p 宿主机端口
[root@VM-4-8-centos ~]# docker run -d --name nginx01 -p 3344:80 nginx
758560e9f1544d39c50ef4cad8f936523c5a523bab44a73b771c1b774761be4f
[root@VM-4-8-centos ~]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@VM-4-8-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
758560e9f154 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
# 进入nginx内部
[root@VM-4-8-centos ~]# docker exec -it 758560e9f154 /bin/bash
root@758560e9f154:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@758560e9f154:/# cd /etc/nginx/
# 这里是nginx的配置文件
root@758560e9f154:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
端口暴露的原理
1、完成以后在浏览器输入: http://43.xxx.xxx.xxx:3344/ 进行访问
思考问题: 我们每次改动nginx配置文件,都需要进入容器内部。十分的麻烦,要是可以在容器外部提供一个映射路径,达到在容器修改文件名,容器内部就可以自动修改? -v 数据卷
# 容器停止就删除 docker run -it --rm ,一般用来测试,用完即删除
docker run -it --rm tomcat:9.0
# 下载再启动
docker pull tomcat:9.0
# 启动运行
docker run -d -p 3355:8080 --name tomcat01 tomcat
# 测试外网可以访问了
# 进入容器
docker run -d -p 3355:8080 --name tomcat01 tomcat
# 发现问题:1、Linux命令少了, 2、没有webapps。 阿里云镜像的原因。默认是最小的镜像,所有不必要的都剔除掉。
# 保证最小可运行的环境
思考问题: 我们以后要部署项目,如果每次都要进入容器是不是十分麻烦?要是可以在外部提供一个映射路径,webapps,在外部放置项目,就自动同步到内部就好了!
# es 暴露的端口很多
# es 十分的耗内存
# es数据一般需要挂载到安全目录
# --net somenetwork ? 网络配置
# 启动 elasticsearch
[root@VM-4-8-centos ~]# docker run -d --name elasticsearc -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
# 发现问题: 启动了就会发现服务器就卡住了
# docker stats 查看cpu状态
# 测试es是否能够成功
[root@VM-4-8-centos ~]# curl localhost:9200
{
"name" : "01cd3b077bf4",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "z6GHnaQCT4m_o9Mfrp1_dQ",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
# 查看cpu使用
[root@VM-4-8-centos ~]# docker top d90ee7c1f92e
UID PID PPID C STIME TTY TIME CMD
lightho+ 21288 21268 99 14:17 ? 00:00:38 /usr/share/elasticsearch/jdk/bin/java -Des.networkaddress.cache.ttl=60
# 增加内存限制,修改配置文件 -e 环境配置修改
[root@VM-4-8-centos ~]# docker run -d --name elasticsearc02 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64 -Xmx512" elasticsearch:7.6.2
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
镜像是什么
UnionFs (联合文件系统)
Union文件系统是 Docker 镜像的基础
Docker镜像加载原理
Docker 的镜像实际上由一层一层的文件系统组成,这种层级的文件系统叫 UnionFS
平时我们安装进虚拟机的 CentOS 都是好几个G,为什么 Docker 这里才200M?
例子:
下图中举了一个简单的例子,每个镜像层包含3个文件,而镜像包含了来自两个镜像层的6个文件
上图中的镜像层跟之前的图的有所却别,主要目的是便于展示文件。
下图展示了一个稍微负责的三层镜像,在外部看来整个镜像只有6个文件,这是因为最上层中的文件7是文件5的一个更新版本。
这种情況下,上层镜像层中的文件覆盖了底层镜像层中的文件
这样就使得文件的更新版本作为一个新镜像层添加到镜像当中
下图展示了与系统显示相同的三层镜像。所有镜像层堆叠并合并,对外提供统一的视图。
特点
docker commit # 提交容器成为一个新的副本
# 命令和git原理类似
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名: [TAG]
拿tomcat为例
# 启动一个默认的tomcat
[root@VM-4-8-centos ~]# docker run -d -p 8080:8080 tomcat
417070f84c5c474bb2a56c5e7ee9cc98ba2d6f972e07ae921c29f9945bd245a5
[root@VM-4-8-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
417070f84c5c tomcat "catalina.sh run" 2 seconds ago Up 2 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp lucid_cray
# 404,默认镜像webapps没有项目
[root@VM-4-8-centos ~]# curl localhost:8080
# 自己开始制作镜像,让webapps下有默认项目
[root@VM-4-8-centos ~]# docker exec -it 417070f84c5c /bin/bash
# 复制webapps.dist目录下的项目到webapps
root@417070f84c5c:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@417070f84c5c:/usr/local/tomcat# cd webapps
root@417070f84c5c:/usr/local/tomcat/webapps# ls
docs examples host-manager manager ROOT
# 再访问就可以访问到tomcat画面了
[root@VM-4-8-centos ~]# curl localhost:8080
# 将修改完的容器提交为新的镜像
[root@VM-4-8-centos ~]# docker commit -a="cong" -m="test commit images" 417070f84c5c tomcat02
sha256:ee4d42c1b35cea4ab07d90eb5007fc7623b190877e59f224712ef45cc0f15b56
[root@VM-4-8-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat02 latest ee4d42c1b35c 7 seconds ago 479MB
tomcat latest 608294908754 2 days ago 475MB
# 启动提交的新镜像
[root@VM-4-8-centos ~]# docker run -d -p 8082:8080 tomcat02
dac8bdcf97192ac54220cc315dd2f2de2330322dcceafd99006234c701dc6847
[root@VM-4-8-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dac8bdcf9719 tomcat02 "catalina.sh run" 2 seconds ago Up 1 second 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp mystifying_tu
417070f84c5c tomcat "catalina.sh run" 14 minutes ago Up 14 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp lucid_cray
5a65cf8c930d f29a1ee41030 "/usr/local/bin/dock…" 44 hours ago Exited (1) 44 hours ago elasticsearc02
# 访问
[root@VM-4-8-centos ~]# curl localhost:8082
什么是数据容器卷
docker启动一个镜像,数据都是在容器中的,那么我们容器删除,数据就会丢失! 需求: 数据可以持久化
MySQL,容器删了,删除跑路!需求:MySQL数据可以存储到本地!
容器之间可以有一个数据共享的技术!Docker容器中产生的数据,同步到本地!
这就是卷技术!目录的挂在门将我们容器内的目录,挂在到Linux上面!
总结:容器的持久化和同步化操作!容器间也可是可以数据共享的!
方式一:直接使用命令来挂载 -v
docker run -it -v 主机目录:容器目录
docker run -it -v /home/ceshi:/home centos /bin/bash
# 启动起来时候外面可以通过 docker inspect 容器id
docker inspect 232f0b69a892
# 启动一个容器 并将本地目录/www/test和容器目录/home进行挂载
[root@VM-4-8-centos www]# docker run -it -v /www/test:/home centos /bin/bash
[root@232f0b69a892 /]# cd /home
# 容器中目录下新建一个文件
[root@232f0b69a892 home]# touch centos.java
# ctrl+P+Q 退出
[root@VM-4-8-centos www]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
232f0b69a892 centos "/bin/bash" 3 minutes ago Up 3 minutes condescending_wu
[root@VM-4-8-centos www]# cd test
# 容器中新建的文件已经同步到本地了
[root@VM-4-8-centos test]# ls
centos.java
# linux中编辑这个文件
[root@VM-4-8-centos test]# vim centos.java
[root@VM-4-8-centos test]# cat centos.java
nihao
# linux中新建一个文件
[root@VM-4-8-centos test]# touch linux.java
# 进入容器
[root@VM-4-8-centos test]# docker exec -it 232f0b69a892 /bin/bash
[root@232f0b69a892 /]# cd /home
# 可以看到数据同步过来了
[root@232f0b69a892 home]# ls
centos.java linux.java
[root@232f0b69a892 home]# cat centos.java
nihao
好处: 以后修改文件配置只需要在本地修改即可,因为启动容器后数据会自动同步
使用mysql测试
# 运行容器,需要做数据挂载! # 安装启动mysql , 需要配置密码的,这是需要注意的!
# 官方测试: docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
# 启动mysql5.7
-d 后台运行
-p 端口映射
-v 数据卷挂载
-e 环境配置
--name 容器名字
[root@VM-4-8-centos test]# docker run -d -p 3306:3306 -v /www/mysql:/etc/mysql/conf.d
-v /www/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=456789 --name mysql01 mysql:5.7
# 启动成功之后,查看linux发现文件已经关联上了
# 使用Navicat连接到服务器3306 密码456789 --- 3306 和容器内部的 3306映射, 这个时候我们基于开源连接上了!
# 在本地创建一个test数据库查看是否可以自动同步,看到本地已经出现test数据库的文件夹了,证明同步成功
[root@VM-4-8-centos test]# cd /www/mysql/
[root@VM-4-8-centos mysql]# ls
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql performance_schema public_key.pem server-key.pem test
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 mysql.sock private_key.pem server-cert.pem sys
结论: 我们发现,挂载到本地的数据卷一九没有丢失,这就实现了容器数据持久化!
# 匿名挂载, -v 容器内路径
[root@VM-4-8-centos mysql]# docker run -d --name nginx01 -v /etc/nginx nginx
2b1779594c759c6c1fca56587cdf158574c9ce66177780610830e9df199421f5
[root@VM-4-8-centos mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2b1779594c75 nginx "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 80/tcp nginx01
232f0b69a892 centos "/bin/bash" 7 hours ago Up 7 hours condescending_wu
[root@VM-4-8-centos mysql]# docker inspect 2b1779594c75
# 具名挂载, -v 卷名:容器内路径
[root@VM-4-8-centos mysql]# docker run -d -it --name nginx02 -v /www/nginx:/etc/nginx nginx /bin/bash
# 查看volume的情况,匿名挂载会随机分配一个volume name,如下图
[root@VM-4-8-centos mysql]# docker volume ls
DRIVER VOLUME NAME
local e70ec5b66da4371555c8069458549cde704ea99118715c7d2e8f334a81dff3c5
所有的docker容器内的卷,没有指定目录的情况下都是在/var/lib/docker/volumes/_data
# :ro 表示只读,说明这个路径只能通过宿主机来操作,容器内部无法操作
# :rw 表示可读可写
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
[root@VM-4-8-centos mysql]# docker run -it -P --name nginx01 -v /www/nginx:/etc/nginx:ro nginx /bin/bash
root@47b4c158c8ed:/# cd /etc/nginx/
root@47b4c158c8ed:/etc/nginx# touch java.java
touch: cannot touch 'java.java': Read-only file system
dockerfile 就是用来构建docker镜像的构建文件!命令脚本!
通过这个脚本可以生成一个镜像,镜像是一层一层的,脚本一个个的命令组成,每个命令就是一层
# 创建dockerfile文件,名字随机,建议Dockerfile
# 文件中的内容 指令(大写) 参数
FROM centos
VOLUME ["valume01","volume02"]
CMD echo "---end----"
CMD /bin/bash
# 这里的每一个命令,就是镜像的一层
# 执行命令 注意后面需要添加一个 .
docker build -f dockerfile -t mycentos .
# 启动自己的容器
docker run -it 镜像id /bin/bash
# 查看挂载信息
docker inspect 容器id
VOLUME ["valume01","volume02"] --> 匿名挂载
# 启动一个mysql01容器
[root@VM-4-8-centos www]# docker run -d -p 3306:3306 --name mysql01 -e MYSQL_ROOT_PASSWORD=456789 -v /www/mysql:/etc/mysql/conf.d mysql:5.7
f76ddf858ab3311cb4f4781d3e14ad84b653b5bcb0b041387398d622e70cea7e
[root@VM-4-8-centos www]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f76ddf858ab3 mysql:5.7 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql01
# 进入mysql01容器内部
[root@VM-4-8-centos www]# docker exec -it f76ddf858ab3 /bin/bash
# 进入到conf.d目录下,此时这个目录下什么也没有,新建一个a.txt文件
bash-4.2# cd /etc/mysql/conf.d
bash-4.2# ls
bash-4.2# touch a.txt
# ctre+P+Q 不停止退出
# 查看,此时mysql01和宿主机挂载上了
[root@VM-4-8-centos www]# cd /www/mysql
[root@VM-4-8-centos mysql]# ls
a.txt
# 再启动一个mysql02 ,使用--volumes-from 让其数据卷继承mysql01
[root@VM-4-8-centos mysql]# docker run -d -p 3307:3306 --name mysql02 -e MYSQL_ROOT_PASSWORD=456789 --volumes-from mysql01 mysql:5.7
54da9c0381594d33dc879890cc641e3151016216225d0aed69fa7d4cdeda0dca
[root@VM-4-8-centos mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54da9c038159 mysql:5.7 "docker-entrypoint.s…" 11 seconds ago Up 10 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql02
f76ddf858ab3 mysql:5.7 "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql01
# 进入mysql02内部,切换到cd /etc/mysql/conf.d/,可以看到a.txt已经从mysql01同步到mysql02了,
# 然后新建一个b.txt文件
[root@VM-4-8-centos mysql]# docker exec -it 54da9c038159 /bin/bash
bash-4.2# cd /etc/mysql/conf.d/
bash-4.2# ls
a.txt
bash-4.2# touch b.txt
# ctrl+P+Q 不停止退出
# 再进入,mysql01容器,查看,b.txt从mysql02同步到mysql01了
[root@VM-4-8-centos mysql]# docker exec -it f76ddf858ab3 /bin/bash
bash-4.2# cd /etc/mysql/conf.d/
bash-4.2# ls
a.txt b.txt
# ctrl+P+Q 不停止退出
# 宿主机也同步过来了
[root@VM-4-8-centos mysql]# cd /www/mysql
[root@VM-4-8-centos mysql]# ls
a.txt b.txt
测试停止mysql01,宿主机和mysql02数据会不会同步?然后再启动mysql01,看看这段时间的数据会不会同步?
# 停止mysql01
[root@VM-4-8-centos mysql]# docker stop f76ddf858ab3
[root@VM-4-8-centos mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54da9c038159 mysql:5.7 "docker-entrypoint.s…" 11 minutes ago Up About a minute 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql02
f76ddf858ab3 mysql:5.7 "docker-entrypoint.s…" 16 minutes ago Exited (0) About a minute ago mysql01
# 宿主机创建d.txt文件
[root@VM-4-8-centos mysql]# touch d.txt
[root@VM-4-8-centos mysql]# ls
a.txt b.txt d.txt
# 进入mysql02容器,看到d.txt也可以同步过来,然后创建一个a.java文件
[root@VM-4-8-centos mysql]# docker exec -it 54da9c038159 /bin/bash
bash-4.2# cd /etc/mysql/conf.d/
bash-4.2# ls
a.txt b.txt d.txt
bash-4.2# touch e.java
# ctrl+P+Q 不停止退出
# 宿主机同步了mysql02容器的数据
[root@VM-4-8-centos mysql]# ls
a.txt b.txt d.txt e.java
# 再启动mysql01 看看d.txt和e.java能不能同步过来
[root@VM-4-8-centos mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54da9c038159 mysql:5.7 "docker-entrypoint.s…" 16 minutes ago Up 6 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql02
f76ddf858ab3 mysql:5.7 "docker-entrypoint.s…" 21 minutes ago Exited (0) 6 minutes ago mysql01
# 启动mysql01,看到d.txt和e.java已经同步过来了
[root@VM-4-8-centos mysql]# docker start f76ddf858ab3
f76ddf858ab3
[root@VM-4-8-centos mysql]# docker exec -it f76ddf858ab3 /bin/bash
bash-4.2# cd /etc/mysql/conf.d/
bash-4.2# ls
a.txt b.txt d.txt e.java
结论:
容器之间配置信息的传递,数据卷容器的声明周期一直持续到没有容器使用为止。
但是一旦持久化到了本地,这个时候,本地的数据是不会删除的!
DockerFile介绍
dockerfile是用来构建docker镜像的文件!命令参数脚本!
构建步骤:
1、编写一个dockerfile文件
2、doucker build 构建成为一个镜像
3、docker run 运行镜像
4、docker push 发布镜像(DockerHbu、阿里云镜像仓库)
DockerFile的构建过程
1、每个保留关键字(指令)都是必须是大写字母
2、执行上到下顺序执行
3、# 表示注释
4、每一个指令都会创建提交一个新的镜像层,并提交!
dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简单!
Docker镜像逐渐成为企业交付的标准,必须要掌握!
步骤: 开发、部署、运维缺一不可!
DockerFile:构建文件,定义了一切的步骤,源代码
DockerImages:通过 DockerFile 构建生成的镜像,最终发布和运行的产品,原来是jar包,war包
Docker容器: 容器就是镜像运行起来提供服务
FROM :指定基础镜像。
MAINTAINER:指定镜像的作者和联系方式。
RUN :在镜像中执行命令,用于安装和配置应用程序。
CMD :指定容器启动时要运行的命令,可以被 Docker run 命令覆盖。
ENTRYPOINT:指定容器启动时要运行的命令,不会被 Docker run 命令覆盖。而是追加。
EXPOSE :声明容器运行时监听的端口。
ENV :设置环境变量。
ADD :将文件复制到镜像中。
COPY :将文件复制到镜像中,与 ADD 的区别是 COPY 只支持本地文件,而 ADD 可以从远程 URL 下载并解压文件。
VOLUME :在镜像中创建一个挂载点,用于持久化数据。
WORKDIR :镜像的工作目录
ONBUILD :当构建一个被继承 DockerFile 这个时候就会运行 ONBUILD 的指令。触发指令。
案例:构建一个mycentos
# 1、编写dockerfile文件
FROM centos:7
MAINTAINER cong
ENV MYPATH /user/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
# 2、构建镜像
docker build -f mydockerfile -t mycentos .
对比: 原生的官方,自己可以增加新的配置,可以使用vim,ifconfig等命令
[root@VM-4-8-centos www]# docker history mycentos
IMAGE CREATED CREATED BY SIZE COMMENT
71b968c996bf 3 minutes ago CMD ["/bin/sh" "-c" "/bin/bash"] 0B buildkit.dockerfile.v0
<missing> 3 minutes ago CMD ["/bin/sh" "-c" "echo \"----end------\""] 0B buildkit.dockerfile.v0
<missing> 3 minutes ago CMD ["/bin/sh" "-c" "echo $MYPATH"] 0B buildkit.dockerfile.v0
<missing> 3 minutes ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0
<missing> 3 minutes ago RUN /bin/sh -c yum -y install net-tools # bu… 176MB buildkit.dockerfile.v0
<missing> 4 minutes ago RUN /bin/sh -c yum -y install vim # buildkit 259MB buildkit.dockerfile.v0
<missing> 4 minutes ago WORKDIR /user/local 0B buildkit.dockerfile.v0
<missing> 4 minutes ago ENV MYPATH=/user/local 0B buildkit.dockerfile.v0
<missing> 4 minutes ago MAINTAINER cong 0B buildkit.dockerfile.v0
<missing> 18 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 18 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 18 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
CMD测试
# 构建dockerfile
[root@VM-4-8-centos www]# cat cmd-test
FROM centos:7
CMD ["ls","-a"]
# 构建镜像
[root@VM-4-8-centos www]# docker build -f cmd-test -t cmdtest .
# 运行镜像,自动运行ls -a
[root@VM-4-8-centos www]# docker run -it cmdtest
. .dockerenv bin etc lib media opt root sbin sys usr
.. anaconda-post.log dev home lib64 mnt proc run srv tmp var
# 运行镜像 ls -a 被替换为-l,-l不是命令报错
[root@VM-4-8-centos www]# docker run -it cmdtest -l
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
ENTRYPOINT测试
# 构建dockerfile
[root@VM-4-8-centos www]# cat ent-test
FROM centos:7
ENTRYPOINT ["ls","-a"]
# 构建镜像
[root@VM-4-8-centos www]# docker build -f ent-test -t enttest .
# 运行镜像,自动运行ls -a
[root@VM-4-8-centos www]# docker run -it enttest
. .dockerenv bin etc lib media opt root sbin sys usr
.. anaconda-post.log dev home lib64 mnt proc run srv tmp var
# 运行镜像 ls -a 追加-l,变成ls -al
[root@VM-4-8-centos www]# docker run -it enttest -l
total 64
drwxr-xr-x 1 root root 4096 Mar 31 05:58 .
drwxr-xr-x 1 root root 4096 Mar 31 05:58 ..
-rwxr-xr-x 1 root root 0 Mar 31 05:58 .dockerenv
-rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log
lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Mar 31 05:58 dev
drwxr-xr-x 1 root root 4096 Mar 31 05:58 etc
drwxr-xr-x 2 root root 4096 Apr 11 2018 home
lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
drwxr-xr-x 2 root root 4096 Apr 11 2018 media
drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x 2 root root 4096 Apr 11 2018 opt
dr-xr-xr-x 130 root root 0 Mar 31 05:58 proc
dr-xr-x--- 2 root root 4096 Nov 13 2020 root
drwxr-xr-x 11 root root 4096 Nov 13 2020 run
lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Mar 31 01:17 sys
drwxrwxrwt 7 root root 4096 Nov 13 2020 tmp
drwxr-xr-x 13 root root 4096 Nov 13 2020 usr
drwxr-xr-x 18 root root 4096 Nov 13 2020 var
1.获取jdk和tomcat的包
wget https://packages.baidu.com/app/jdk-8/jdk-8u121-linux-x64.tar.gz
wget https://mirrors.cnnic.cn/apache/tomcat/tomcat-9/v9.0.72/bin/apache-tomcat-9.0.72.tar.gz
2.编写Dockerfile脚本
FROM centos:7
# 指定作者信息
MAINTAINER cong<110@qq.com>
# 拷贝本地文件到镜像
COPY readme.txt /usl/local
# 将本地文件加到镜像中并解压
ADD jdk-8u121-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.72.tar.gz /usr/local/
# 给镜像安装vim命令
RUN yum install -y vim
# 指定工作目录
ENV MYPATH /usrl/local
WORKDIR $MYPATH
# 配置环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_121
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.72
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.72
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
# 暴露端口
EXPOSE 8080
# 执行启动命令和查看日志
CMD /usr/local/apache-tomcat-9.0.72/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.72/bin/logs/catalins.out
3.开始构建镜像
docker build -f mytomcat -t mytomcat01 .
4.运行
docker run -d -p 8080:8080 --name congtomcat -v /www/tomcat/test:/usr/local/apache-tomcat-9.0.72/webapps/test/ -v /www/tomcat/logs/:/usr/local/apache-tomcat-9.0.72/logs/ 4018ebf5cb5c
5./www/tomcat/test下增加web.xml和index.jsp
宿主机/www/tomcat/test和容器中/usr/local/apache-tomcat-9.0.72/webapps/test/进行了挂载
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
web-app>
index.jsp
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>snowtitle>
<style>
body,html{
margin: 0;
padding: 0;
overflow: hidden;
height: 100%;
background: black;
}
.snow{
background: white;
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
}
style>
head>
<body>
<script>
//获取屏幕宽高
var windowWidth = window.screen.width;
var windowHeight = window.screen.height;
//创建雪花
function createSnow(){
var left = 0;
var top = 0;
//定义一个初始化随机数,使雪花在屏幕中
var left_random = Math.random() * windowWidth;
var top_random = Math.random()* windowHeight;
var div = document.createElement('div');
div.className = 'snow';
div.style.transform = 'scale('+(Math.random())+')'
document.body.appendChild(div);
//雪花飘落
setInterval(function () {
div.style.left = left_random + left +'px';
div.style.top = top_random + top +'px'
left += 0.2;
top += 0.2;
//如果雪花跑到屏幕外面了,让雪花重新返回屏幕顶部
if(left_random + left >= windowWidth){
left_random = Math.random();
left = 0;
}
if(top_random + top >= windowHeight){
top_random = Math.random();
top = 0;
}
},10)
}
for(var i = 0 ; i < 200 ; i++){
createSnow()
}
script>
body>
html>
6.访问http://150.158.164.135:8080/test/
参考:https://blog.csdn.net/HDthe_shy_boy/article/details/127412457
https://hub.docker.com/ 上注册自己的账号
# 登录
[root@VM-4-8-centos test]# docker login -u filwod
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@VM-4-8-centos test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat01 latest 4018ebf5cb5c 3 hours ago 849MB
# 给镜像打标签
[root@VM-4-8-centos test]# docker tag mytomcat01 filwod/mytomcat01:1.0
[root@VM-4-8-centos test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
filwod/mytomcat01 1.0 4018ebf5cb5c 3 hours ago 849MB
mytomcat01 latest 4018ebf5cb5c 3 hours ago 849MB
# push到docker hub上
[root@VM-4-8-centos test]# docker push filwod/mytomcat01:1.0
1.登录阿里云,找到容器镜像服务,创建个人实例
2.创建命名空间
3.创建镜像仓库,这里选的本地仓库,公司一般都是gitlab
4.测试
官方文档:
注意:tag给镜像打标签的时候名字要严格按照官方文档给的写,否则报错denied: requested access to the resource is denied
# 登录阿里云镜像仓库
[root@VM-4-8-centos ~]# docker login --username=filwaod1 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
# 想拉取个nginx测试,报错,这里登录了阿里云,就是从阿里云的仓库拉取,但是阿里云的仓库没有这个镜像,所以报错
# 解决方法,重新登录docker hub ,然后拉取nginx,再登录阿里云进行测试
[root@VM-4-8-centos ~]# docker pull ngixn
Using default tag: latest
Error response from daemon: pull access denied for ngixn, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
# 登录docker hub 拉取nginx镜像
[root@VM-4-8-centos ~]# docker login -u filwod
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@VM-4-8-centos ~]# docker pull nginx
[root@VM-4-8-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 080ed0ed8312 5 days ago 142MB
# tag打标签
[root@VM-4-8-centos ~]# docker tag 080ed0ed8312 registry.cn-hangzhou.aliyuncs.com/filwaod/test:1.0
[root@VM-4-8-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 080ed0ed8312 5 days ago 142MB
registry.cn-hangzhou.aliyuncs.com/filwaod/test 1.0 080ed0ed8312 5 days ago 142MB
# 推送阿里云
[root@VM-4-8-centos ~]# docker push registry.cn-hangzhou.aliyuncs.com/filwaod/test:1.0
其实xxxx.xxxx.xxxx.xxxx是一个十进制的数,我们就拿192.168.0.0/16来举例,192.168.0.0对应的二进制应该是11000000.10101000.00000000.00000000,那么后面的/16的意思就是前16位是不变的,只有后面16位是变动的,那么从11000000.10101000.00000000.00000000-11000000.10101000.11111111.11111111,对应的ip地址范围就是192.168.0.0-192.168.255.255,总共65534个可用ip(最后一位192.168.255.255为广播地址)。
如果是192.168.0.0/24,意思就是前24位是不变的,所对应的二进制的ip范围就是11000000.10101000.00000000.00000000-11000000.10101000.00000000.11111111,对应的ip地址就是192.168.0.0-192.168.0.0-192.168.0.255,共254个可用ip(最后一位192.168.0.255为广播地址)。
# 启动一个容器
# 注意这里的tomcat镜像都是阉割版的镜像,
# 执行docker exec -it tomcat01 ip addr会报错"OCI runtime exec failed: exec failed: unable to start container process: exe"
docker run -d -P --name tomcat01 tomcat
# 进入容器内部安装环境
docker exec -it tomcat01 /bin/bash
# apt 版本升级
root@e4487b29b91d:/usr/local/tomcat# apt update
# 安装ip命令
root@e4487b29b91d:/usr/local/tomcat# apt install -y iproute2
# 安装ping命令
root@e4487b29b91d:/usr/local/tomcat# apt-get install -y iputils-ping
# 提交此镜像,后续启动都用这个镜像启动,就不用重新安装ip和ping命令了
[root@VM-4-8-centos www]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4487b29b91d tomcat "catalina.sh run" 9 minutes ago Up 9 minutes 0.0.0.0:32770->8080/tcp, :::32770->8080/tcp tomcat01
# 提交镜像
[root@VM-4-8-centos www]# docker commit -a="cong" -m="test" e4487b29b91d mytomcat
sha256:ea532f63ff6404af91f7791e090458dbab8cea2b923e5d68471031376c0399f9
[root@VM-4-8-centos www]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat latest ea532f63ff64 7 seconds ago 524MB
tomcat latest 608294908754 6 days ago 475MB
# 查看容器内部的网络地址 ip addr
[root@VM-4-8-centos www]# docker exec -it tomcat01 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
135: eth0@if136: <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
# 思考: Linux能不能ping痛 容器内部 ? 是可以的
原理
我们每启动一个docker容器, docker就会给docker容器分配一个ip, 我们只要安装了docker,就会有一个网卡 docker0桥接模式,使用的技术是veth-pair技术!
再启动一个容器测试, 发现又多了一对网卡,再次访问ip addr 命令
docker run -d -P --name tomcat02 mytomcat
ip addr
我们发现这个容器带来网卡,都是一对对的
veth-pair 就是一对的虚拟设备接口,他们都是成对出现的,一端连着协议,一端彼此相连
正因为有这个特性,veth-pair充当一个桥梁, 连接各种虚拟网络设备
OpenStac, Docker容器之间的链接,OVS的链接, 都是使用veth-pair技术
[root@VM-4-8-centos www]# docker exec -it tomcat01 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
135: eth0@if136: <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
[root@VM-4-8-centos www]# 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.070 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.052 ms
结论: 容器和容器之间是可以互相 ping 通的
绘制网络模型图
结论:tomcat01和tomcat02是共用的一个路由器,docker0
所有容器不指定网络的情况下,都是docker0路由的,doucker会给我们的容器分配一个默认的可用IP
Docker使用的是Linux的桥接,宿主机中是一个Docker容器的网桥docker0.
Docker中的所有的网络接口都是虚拟的,虚拟的转发效率高!(内网传递文件!)
跟微服务注册中心一样,注册的是服务名,不管ip如何变化都能ping通
# 通过服务名ping
[root@VM-4-8-centos ~]# docker exec -it tomcat02 ping tomcat01
ping: tomcat01: Name or service not known
# 如何可以解决呢?
# 通过--link既可以解决网络连通问题
[root@VM-4-8-centos ~]# docker run -d -P --name tomcat03 --link tomcat02 mytomcat
3467d3c48f997062580ceef87c6a219315106c61cea903e94b4258d314e2ee66
[root@VM-4-8-centos ~]# 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.085 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.048 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.046 ms
^C
--- tomcat02 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.046/0.059/0.085/0.017 ms
# 反向ping不通吗
[root@VM-4-8-centos ~]# docker exec -it tomcat02 ping tomcat03
ping: tomcat03: Name or service not known
# 查看--link原理
# hosts中增加了一个映射172.17.0.3 tomcat02 16dbd88c4c3e
# 当ping tomcat02的时候,就会解析出ip为172.17.0.3,转而ping 172.17.0.3
[root@VM-4-8-centos ~]# docker exec -it tomcat03 cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 tomcat02 16dbd88c4c3e
172.17.0.4 3467d3c48f99
# 查看docker所有的网络
[root@VM-4-8-centos ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
ca96fb03bd7e bridge bridge local
9b8064b311dd host host local
c41f634e7423 none null local
网络模式
bridge: 桥接模式,桥接 docker 默认,自己创建的也是用brdge模式
none: 不配置网络
host: 和宿主机共享网络
container:容器网络连通!(用的少, 局限很大)
测试
# 我们直接启动的命令默认有一个 --net bridge,而这个就是我们的docker0
docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat01 --net bridge tomcat
# docker0特点,默认,容器名不能访问, --link可以打通连接!
# 我们可以自定义一个网络!
# --driver bridge
# --subnet 192.168.0.0/16 可以支持255*255个网络 192.168.0.2 ~ 192.168.255.254
# --gateway 192.168.0.1
[root@VM-4-8-centos ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
8ad5de56f61189496ec5a13cca116254c08daf7b13d1b67bb1699ceb17fde821
[root@VM-4-8-centos ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
ca96fb03bd7e bridge bridge local
9b8064b311dd host host local
8ad5de56f611 mynet bridge local
c41f634e7423 none null local
[root@VM-4-8-centos ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "8ad5de56f61189496ec5a13cca116254c08daf7b13d1b67bb1699ceb17fde821",
"Created": "2023-04-04T13:33:26.663075239+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
# 在自己创建的网络中启动两个容器
[root@VM-4-8-centos ~]# docker run -d -P --name tomcat-net-01 --net mynet mytomcat
587f553c019dd42fe763af7dd695882165542254c38f3268aff29de584a31c93
[root@VM-4-8-centos ~]# docker run -d -P --name tomcat-net-02 --net mynet mytomcat
8da25fd4d62a6c218683ffae302fc241e3f900e7c4c10bf0e860b61eeefc1dfb
[root@VM-4-8-centos ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "8ad5de56f61189496ec5a13cca116254c08daf7b13d1b67bb1699ceb17fde821",
"Created": "2023-04-04T13:33:26.663075239+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"587f553c019dd42fe763af7dd695882165542254c38f3268aff29de584a31c93": {
"Name": "tomcat-net-01",
"EndpointID": "450a64fea96fe830010ff9bcf63bbc7ec60201174bd4e75128b4733adafaeee6",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"8da25fd4d62a6c218683ffae302fc241e3f900e7c4c10bf0e860b61eeefc1dfb": {
"Name": "tomcat-net-02",
"EndpointID": "de8c0dca95e17cd1b6419db00c08b38bf20dd8717d944e40c91543ccef302ca4",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
# 现在ping名字可以ping通
[root@VM-4-8-centos ~]# docker exec -it tomcat-net-01 ping tomcat-net-02
PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.060 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.046 ms
我们自定义的网络docker都已经帮我们维护好了对应的关系,推荐我们平时这样使用网络
好处:
redis - 不同的集群使用不同的网络,保证集群时安全和健康的
mysql - 不同的集群使用不同的网络,保证集群时安全和健康的
[root@VM-4-8-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8da25fd4d62a mytomcat "catalina.sh run" 33 minutes ago Up 33 minutes 0.0.0.0:32774->8080/tcp, :::32774->8080/tcp tomcat-net-02
587f553c019d mytomcat "catalina.sh run" 33 minutes ago Up 33 minutes 0.0.0.0:32773->8080/tcp, :::32773->8080/tcp tomcat-net-01
16dbd88c4c3e mytomcat "catalina.sh run" 21 hours ago Up 21 hours 0.0.0.0:32771->8080/tcp, :::32771->8080/tcp tomcat02
e4487b29b91d tomcat "catalina.sh run" 21 hours ago Up 21 hours 0.0.0.0:32770->8080/tcp, :::32770->8080/tcp tomcat01
# 此时tomcat01和tomcat-net-01属于两个网络,所以ping不通
[root@VM-4-8-centos ~]# docker exec -it tomcat01 ping tomcat-net-01
ping: tomcat-net-01: Name or service not known
# 将tomcat01与mynet网络连通
[root@VM-4-8-centos ~]# docker network connect mynet tomcat01
[root@VM-4-8-centos ~]# docker exec -it tomcat01 ping tomcat-net-01
PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.059 ms
# 原理,将tomcat01的ip存到了mynet网络一份
[root@VM-4-8-centos ~]# docker network inspect mynet
"Containers": {
"587f553c019dd42fe763af7dd695882165542254c38f3268aff29de584a31c93": {
"Name": "tomcat-net-01",
"EndpointID": "450a64fea96fe830010ff9bcf63bbc7ec60201174bd4e75128b4733adafaeee6",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"8da25fd4d62a6c218683ffae302fc241e3f900e7c4c10bf0e860b61eeefc1dfb": {
"Name": "tomcat-net-02",
"EndpointID": "de8c0dca95e17cd1b6419db00c08b38bf20dd8717d944e40c91543ccef302ca4",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"e4487b29b91daac4dcf314bb866dfe6194ff5664c1e0ebbb90e2f74c2bf5a661": {
"Name": "tomcat01",
"EndpointID": "28905d59c5d141ef960f9234811c3379b308d2e7a0291e4fc5a03ed10a589427",
"MacAddress": "02:42:c0:a8:00:04",
"IPv4Address": "192.168.0.4/16",
"IPv6Address": ""
}
},
结论: 假设要跨网络操作别人,就需要使用 docker network connect 连通!
redis集群操作看我这篇文章:https://blog.csdn.net/qq_42665745/article/details/128844130
# 新建网络
[root@VM-4-8-centos ~]# docker network create redis --subnet 172.38.0.0/16
100cebc259381e6cd201a4ee523dbe908be83c6154327f95c2ec34a382bc2b93
# 创建文件
[root@VM-4-8-centos ~]# 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
# 创建结点1
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /mydata/redis/node-1/data:/data \
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点2
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v /mydata/redis/node-2/data:/data \
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点3
docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v /mydata/redis/node-3/data:/data \
-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点4
docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v /mydata/redis/node-4/data:/data \
-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点5
docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v /mydata/redis/node-5/data:/data \
-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点6
docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
-v /mydata/redis/node-6/data:/data \
-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
# 进入redis-1内部
[root@VM-4-8-centos ~]# docker exec -it redis-1 /bin/sh
# 创建集群
/data # /data # 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
# 查看集群配置
/data # redis-cli cluster nodes
8fd5ddc910e1dddd079d31e6993bd1eb9f555da4 172.38.0.13:6379@16379 master - 0 1680591047000 3 connected 10923-16383
e977cd6987f9b45817c5926fd5c3a71635f5a0a0 172.38.0.12:6379@16379 master - 0 1680591048502 2 connected 5461-10922
2ffb5d03f17e30c825aca538ab2ee144c9223374 172.38.0.16:6379@16379 slave e977cd6987f9b45817c5926fd5c3a71635f5a0a0 0 1680591048501 6 connected
5056347fe5605be313fe71861252a867e7880c16 172.38.0.15:6379@16379 slave 6df54e443a343bfa9a0339f10c10674b35bc418f 0 1680591048000 5 connected
e323a154edaab137e51bc9ea6f428578534a9601 172.38.0.14:6379@16379 slave 8fd5ddc910e1dddd079d31e6993bd1eb9f555da4 0 1680591047000 4 connected
6df54e443a343bfa9a0339f10c10674b35bc418f 172.38.0.11:6379@16379 myself,master - 0 1680591047000 1 connected 0-5460
# 测试一下集群故障恢复
# 连接redis-1
/data # redis-cli -c -p 6379
# 设置值,值设置在了172.38.0.13这个容器上
127.0.0.1:6379> set a 111
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
# 新开一个窗口,停止172.38.0.13容器
[root@VM-4-8-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
488a48dc91fd redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 35 minutes ago Up 35 minutes 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6
6b0916ec55cf redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 35 minutes ago Up 35 minutes 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5
f9db236a6dba redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 35 minutes ago Up 35 minutes 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4
bd47655a3409 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 35 minutes ago Up 35 minutes 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-3
5ce5de87486c redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 36 minutes ago Up 36 minutes 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2
c6f20dcbd108 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 36 minutes ago Up 36 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-1
[root@VM-4-8-centos ~]# docker stop bd47655a3409
bd47655a3409
# 再查看集群配置
/data # redis-cli cluster nodes
8fd5ddc910e1dddd079d31e6993bd1eb9f555da4 172.38.0.13:6379@16379 master,fail - 1680591459812 1680591458000 3 connected
e977cd6987f9b45817c5926fd5c3a71635f5a0a0 172.38.0.12:6379@16379 master - 0 1680591488000 2 connected 5461-10922
2ffb5d03f17e30c825aca538ab2ee144c9223374 172.38.0.16:6379@16379 slave e977cd6987f9b45817c5926fd5c3a71635f5a0a0 0 1680591487575 6 connected
5056347fe5605be313fe71861252a867e7880c16 172.38.0.15:6379@16379 slave 6df54e443a343bfa9a0339f10c10674b35bc418f 0 1680591488979 5 connected
e323a154edaab137e51bc9ea6f428578534a9601 172.38.0.14:6379@16379 master - 0 1680591489480 8 connected 10923-16383
6df54e443a343bfa9a0339f10c10674b35bc418f 172.38.0.11:6379@16379 myself,master - 0 1680591488000 1 connected 0-5460
/data # redis-cli -c -p 6379
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"111"
1.随便找个springboot项目
2.打包并编写Dockerfile
FROM openjdk:8-jre-slim
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]
3.复制jar和Dockerfile到同一目录下
4.打包
[root@VM-4-8-centos springboot]# docker build -t springboot .
5.启动
[root@VM-4-8-centos springboot]# docker run -d -p 8082:8080 springboot
090845ed70e6f14bb12a4cd1e5db5e560e5e3ffd3c6adf8d52ced2d824ba1d80