赞
踩
内容主要基于B站狂神老师的Docker基础视频与部分网络docker博客总结,按照个人理解所整理总结。
尴尬了,图床用的是github,由于网络原因图片加载不出来,嗯,我直接发个PDF吧,需要的自取
https://yxmiaoyu.lanzouu.com/b01dm3ehg
密码:1234
相对于虚拟机来说,Docker非常的轻,几百k~几十Mb
基于Go开发,开源项目
文档地址:https://docs.docker.com/
仓库地址:https://hub.docker.com/
容器化技术不是模拟一个完整的操作系统。
左面是Docker。容器内的应用直接运行在 宿主机的内核上,容器是没有自己的内核的,也没有虚拟我们的硬件,轻便了,每个容器都是互相隔离的,每个容器内都有一个属于自己的文件系统,互不影响。
右面是虚拟机,虚拟出一条硬件,运行一个完整的操作系统,然后在这个系统上安装运行软件。
四个特点:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3ILhkuAI-1654497083788)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202205191534611.png)]
镜像就是类,容器就是实例化后的实体类,仓库就是用来存放镜像的地方
==官网安装教程:==https://docs.docker.com/engine/install/centos/
[root@abc ~]# uname -r
3.10.0-1160.59.1.el7.x86_64
[root@abc ~]# cat /etc/os-release
NAME=“CentOS Linux”
VERSION=“7 (Core)”
ID=“centos”
ID_LIKE=“rhel fedora”
VERSION_ID=“7”
PRETTY_NAME=“CentOS Linux 7 (Core)”
ANSI_COLOR=“0;31”
CPE_NAME=“cpe:/o:centos:centos:7”
HOME_URL=“https://www.centos.org/”
BUG_REPORT_URL=“https://bugs.centos.org/”
CENTOS_MANTISBT_PROJECT=“CentOS-7”
CENTOS_MANTISBT_PROJECT_VERSION=“7”
REDHAT_SUPPORT_PRODUCT=“centos”
REDHAT_SUPPORT_PRODUCT_VERSION=“7”
- 卸载旧版本Docker
yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 下载需要的安装包
yum install -y yum-utils
- 1
- 设置镜像的仓库
国外的,慢 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo 国内的 yum-config-manager\ --add-repo \ http://mirrors.aliyun.com/dockerce/linux/centos/docker-ce.repo
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 更新软件包索引
yum makecache fast
- 1
安装Docker引擎
docker-ce: 社区版 docker-ee企业版
yum install docker-ce docker-ce-cli containerd.io
- 1
- 启动Docker
systemctl start docker
- 1
查看Docker版本 docker version
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LeyUD0fq-1654497083789)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202205231013385.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WZRJocuw-1654497083790)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061425815.png)]
- 测试helloWorld
docker run hello-world
- 1
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0sVvpRv6-1654497083790)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061425364.png)]
此界面仅表示docker安装成功
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AilIlQEj-1654497083791)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202205231016964.png)]
- 查看下载的Hello-world镜像
docker images
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JABQu4m2-1654497083791)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202205231018433.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VIvLuF8Y-1654497083792)(E:\WorkSpace.md\images\image-20220523132005635.png)]
sudo yum remove docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
#/var/lib/docker 默认工作路径
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7VXRIev2-1654497083793)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061425493.png)]
#
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://jfqibt7a.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
- 创建文件夹哎
- 设置阿里云镜像加速
- 重启服务
- 重启Docker
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aALqsr1O-1654497083793)(E:\WorkSpace.md\images\image-20220523133242436.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5IN9fklT-1654497083794)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061425291.png)]
Docker Engine是一个客户端-服务器(C/S,Client/Server)应用程序,具有以下主要组件:
Docker是一个Client Server结构的系统,Docker守护进程运行在主机上,然后通过Socket连接从客户端访问,守护进程从客户端接受命令并管理运行在主机上的容器。
容器,是一个运行时环境就是我们所说的集装箱。
客户端命令通过socker到达守护进程
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8Ftoizdw-1654497083794)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061425935.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ckuziWNX-1654497083800)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061425357.png)]
docker version #显示docker的版本信息。
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令
帮助文档的地址:https://docs.docker.com/engine/reference/commandline/docker/
docker images [-a,-q,-aq]
---
仓库 标签 ID 创建时间 大小
---
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 7 months ago 13.3kB
---
# 可选项
--all , -a # 列出所有镜像
--quiet , -q # 只显示镜像的id
docker search mysql [-f=AAA=BBB] --- --- NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12618 [OK] --- # 可选项 --filter=STARS=3000 # 搜素出来的镜像就是STARS大于3000的 ---------------- [root@localhost ~]# docker search mysql -f=STARS=4000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12618 [OK] mariadb MariaDB Server is a high performing open sou… 4847 [OK]
# 下载镜像,docker pull 镜像名[:tag] [root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker pull mysql Using default tag: latest # 如果不写tag,默认就是latest latest: Pulling from library/mysql bf5952930446: Pull complete # 分层下载,docker images 的核心,联合文件系统 8254623a9871: Pull complete 938e3e06dac4: Pull complete ea28ebf28884: Pull complete f3cef38785c2: Pull complete 894f9792565a: Pull complete 1d8a57523420: Pull complete 6c676912929f: Pull complete ff39fdb566b4: Pull complete fff872988aba: Pull complete 4d34e365ae68: Pull complete 7886ee20621e: Pull complete Digest: sha256:c358e72e100ab493a0304bda35e6f239db2ec8c9bb836d8a427ac34307d074ed # 签名 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest # 真实地址 # 等价于,下面两个命令是等价的。 docker pull mysql docker pull docker.io/library/mysql:latest --------------------------------------------------------------------- # 指定版本下载 docker pull mysql:5.7 5.7: Pulling from library/mysql bf5952930446: Already exists # 已存在,表示共用,可以极大的节省内存 8254623a9871: Already exists 938e3e06dac4: Already exists ea28ebf28884: Already exists f3cef38785c2: Already exists 894f9792565a: Already exists 1d8a57523420: Already exists 5f09bf1d31c1: Pull complete # 没有的才需要下载更新 1b6ff254abe7: Pull complete 74310a0bf42d: Pull complete d398726627fd: Pull complete Digest: sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7 # 查看本地镜像 [root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 718a6da099d8 6 days ago 448MB mysql latest 0d64f46acfd1 6 days ago 544MB hello-world latest bf756fb1ae65 7 months ago 13.3kB
-f 是全部删除,可以通过imageID来删除,也可以通过name来删除。
[root@abc ~]# docker rmi -f 镜像ID #删除指定的镜像
[root@abc ~]# docker rmi -f 镜像ID 镜像ID 镜像ID #删除多个镜像
[root@abc ~]# docker rmi -f $(docker images -aq) #删除所有镜像
docker pull centos ---------------------------- [root@localhost ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 3218b38490ce 5 months ago 516MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos latest 5d0da3dc9764 8 months ago 231MB
docker run 镜像id #新建容器并启动
docker ps 列出所有运行的容器 docker container list
docker rm 容器id #删除指定容器
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
docker run [可选参数] image | docker container run [可选参数] image # 参数说明 --name=“Name” 容器名字 tomcat01 tomcat02 用来区分容器 -d 后台方式运行 -it 使用交互方式运行,进入容器查看内容,不用这个参数进不去容器 -p(小写) 指定容器的端口 -p 8080:8080 1. -p ip:主机端口:容器端口 2. -p 主机端口:容器端口(常用)(映射) 3. -p 容器端口 4. 容器端口 -P(大写) 随机指定端口 ------------------------------------------------------------- # 测试,启动并进入容器 [root@localhost ~]# docker run -it centos /bin/bash 主机名就是镜像ID [root@7e77b4c56100 /]# ls # 查看容器内的centos,基础版本,很多命令是不完善的 bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr # 从容器中退回主机 [root@77969f5dcbf9 /]# exit exit
docker ps 命令 #列出当前正在运行的容器
-a, --all #列出当前 正在 运行的容器 + 带出历史运行过的容器
-n=?, --last int #列出最近创建的?个容器 ?为1则只列出最近创建的一个容器,为2则列出2个
-q, --quiet #只列出容器的编号
--------------------------------------------------------
[root@abc ~]# docker ps -a -n=1
[root@abc ~]# docker ps #看看
[root@abc ~]# docker ps -a
[root@abc ~]# docker ps -aq
exit #容器直接退出
ctrl +P +Q #容器不停止退出 ---注意:这个很有用的操作, 这是命令不是键盘快捷键
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -rf
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@localhost ~]# docker run -d centos
77ac366130abee70553194e77889bbc9d988859cee730aead8829efafa1ae40d
# 问题 docker ps, 发现centos停止了
# 常见的坑, docker 容器使用后台运行, 就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx, 容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了
docker logs -tf --tail number 容器id
-tf # 显示日志
--tail number # 显示日志条数
-------------------------------------------------------------------------
[root@localhost ~]# docker logs -tf --tail 10 6c0558cbcd09
2022-05-24T02:10:29.433468463Z [root@6c0558cbcd09 /]# exit # 日志输出
# 自己编写一段shell脚本
docker run -d centos /bin/sh -c "while true;do echo xiaofan;sleep 1;done"
#命令 docker top 容器ID
--------------
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c55c1a083e2e centos "/bin/sh -c 'while t…" 12 minutes ago Up 12 minutes fervent_beaver
[root@localhost ~]# docker top c55c1a083e2e
用户ID 进程ID 父进程ID
UID PID PPID C STIME TTY TIME CMD
root 11132 11113 0 10:33 ? 00:00:00 /bin/sh -c while true;do echo 123YX;sleep 1;done
root 12498 11132 0 10:47 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
#命令 docker inspect 容器ID ---------------------------------------------------------- # 测试 [root@localhost ~]# docker inspect c55c1a083e2e [ { 镜像id只是全路径id的一个缩写 "Id": "c55c1a083e2e5e33d6a5577e4ac9d953718f746bcf79841a7b9fa7deb643c5a2", 创建时间 "Created": "2022-05-24T02:33:45.796548118Z", 控制台 "Path": "/bin/sh", 传递的参数 "Args": [ "-c", "while true;do echo 123YX;sleep 1;done" ], 状态 "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 11132, "ExitCode": 0, "Error": "", "StartedAt": "2022-05-24T02:33:46.224199282Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6", "ResolvConfPath": "/var/lib/docker/containers/c55c1a083e2e5e33d6a5577e4ac9d953718f746bcf79841a7b9fa7deb643c5a2/resolv.conf", "HostnamePath": "/var/lib/docker/containers/c55c1a083e2e5e33d6a5577e4ac9d953718f746bcf79841a7b9fa7deb643c5a2/hostname", "HostsPath": "/var/lib/docker/containers/c55c1a083e2e5e33d6a5577e4ac9d953718f746bcf79841a7b9fa7deb643c5a2/hosts", "LogPath": "/var/lib/docker/containers/c55c1a083e2e5e33d6a5577e4ac9d953718f746bcf79841a7b9fa7deb643c5a2/c55c1a083e2e5e33d6a5577e4ac9d953718f746bcf79841a7b9fa7deb643c5a2-json.log", "Name": "/fervent_beaver", "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/0c335307a36317a110766faea36b3c7c868ae5ee9b8c27a989edacf3b6892829-init/diff:/var/lib/docker/overlay2/b62e37aaada82eaec082715523ac39af7267a00be9af7f268d0cee3645b702e4/diff", "MergedDir": "/var/lib/docker/overlay2/0c335307a36317a110766faea36b3c7c868ae5ee9b8c27a989edacf3b6892829/merged", "UpperDir": "/var/lib/docker/overlay2/0c335307a36317a110766faea36b3c7c868ae5ee9b8c27a989edacf3b6892829/diff", "WorkDir": "/var/lib/docker/overlay2/0c335307a36317a110766faea36b3c7c868ae5ee9b8c27a989edacf3b6892829/work" }, "Name": "overlay2" }, 挂载 "Mounts": [], "Config": { "Hostname": "c55c1a083e2e", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh", "-c", "while true;do echo 123YX;sleep 1;done" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20210915", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, 网络命令 "NetworkSettings": { "Bridge": "", "SandboxID": "9aed795b5d2eb0a3d6b31cd48f29acabf338aa036703b40e063df5af1b564490", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/9aed795b5d2e", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "4e0e7bcc423265a62a278215bf9200d8ed34e68a8a82e95b97390915831c6ba4", "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": "8d26d8f19fe79975f51b98bb21730e285b04a26e21cd0b5b4cbc02c01ea370e8", "EndpointID": "4e0e7bcc423265a62a278215bf9200d8ed34e68a8a82e95b97390915831c6ba4", "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 } } } } ]
# 我们通常容器都是在后台运行,需要进入容器,修改一些配置 # 命令 docker exec -it 容器id bashshell # 方式一 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c55c1a083e2e centos "/bin/sh -c 'while t…" 53 minutes ago Up 53 minutes fervent_beaver [root@localhost ~]# docker exec -it c55c1a083e2e /bin/bash [root@c55c1a083e2e /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var ------------------------------------------------------ # 方式二 dcoker attach 容器id # 测试 Docker version 20.10.16, build aa7e414 [root@localhost ~]# docker attach c55c1a083e2e 123YX 123YX 123YX 123YX 123YX 正在执行当前的代码... ================================================================== # 区别 docker exec #进入容器后开启一个新的终端 docker attach # 进入容器正在执行的终端,不会启动新的进程
容器之间是互相隔离状态
#命令 docker cp 容器ID:容器内路径 目的主机路径 ---------------------------------------------------------------------- [root@localhost /]# docker run -it centos WARNING: IPv4 forwarding is disabled. Networking will not work. [root@44e56a21f70f /]# pwd / [root@44e56a21f70f /]# touch cp.java [root@44e56a21f70f /]# exit exit [root@localhost /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 44e56a21f70f centos "/bin/bash" 57 seconds ago Exited (0) 18 seconds ago beautiful_beaver [root@localhost /]# docker cp 44e56a21f70f:/cp.java / [root@localhost /]# ls bin cp.java etc lib media opt root sbin sys tmp var boot dev home lib64 mnt proc run srv test.java usr #拷贝是一个手动过程,未来我们使用 -v卷的技术,可以实现自动
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zDk4dqtM-1654497083800)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061426956.png)]
attach Attach local standard input, output, and error streams to a running container #当前shell下 attach连接指定运行的镜像 build Build an image from a Dockerfile # 通过Dockerfile定制镜像 commit Create a new image from a container's changes #提交当前容器为新的镜像 cp Copy files/folders between a container and the local filesystem #拷贝文件 create Create a new container #创建一个新的容器 diff Inspect changes to files or directories on a container's filesystem #查看docker容器的变化 events Get real time events from the server # 从服务获取容器实时时间 exec Run a command in a running container # 在运行中的容器上运行命令 export Export a container's filesystem as a tar archive #导出容器文件系统作为一个tar归档文件[对应import] history Show the history of an image # 展示一个镜像形成历史 images List images #列出系统当前的镜像 import Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像 info Display system-wide information # 显示全系统信息 inspect Return low-level information on Docker objects #查看容器详细信息 kill Kill one or more running containers # kill指定docker容器 load Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save] login Log in to a Docker registry # logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes
# 1. 搜索镜像 search 建议去docker hub搜索,可以看到帮助文档 docker search nginx # 2. 下载镜像 pull docker pull nginx # 3. 查看镜像 docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 4 months ago 141MB mysql latest 3218b38490ce 5 months ago 516MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos latest 5d0da3dc9764 8 months ago 231MB # 4. 启动 # -d 后台运行 # -name 给容器命名 # -p 宿主机端口:容器内部端口 docker run -d --name nginx01 -p 3344:80 nginx 496d49e1b1d932865039b5d4436defbe91439a2c8c82ec3f551edb0aa0d5b524 ==外网访问主机3344端口时,可以访问到容器的80端口== # 5. 查看运行状态 [root@iZ2zeg4ytp0whqtmxbsqiiZ home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 496d49e1b1d9 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01 # 6. 本地访问测试 [root@localhost /]# 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> # 7、进入容器,修改配置 [root@localhost /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 496d49e1b1d9 nginx "/docker-entrypoint.…" 55 minutes ago Up 55 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01 [root@localhost /]# docker exec -it nginx01 /bin/bash root@496d49e1b1d9:/# whereis nginx ==# 找nginx== nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx root@496d49e1b1d9:/# cd /etc/nginx/ root@496d49e1b1d9:/etc/nginx# ls conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params root@496d49e1b1d9:/etc/nginx#
思考问题:我们每次都需要改动nginx配置,都需要进入容器内部?十分的麻烦,我们要是可以在容器外部提供一个映射路径,达到可以在容器外部修改文件名,容器内部就可以进行自动修改?-数据卷
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eOoCPhtX-1654497083801)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061426632.png)]
# 官网下载 tomcat9.0 docker run -it --rm tomcat:9.0 # 之前的启动都是后台,停止了容器,容器还是可以查到, docker run -it --rm 镜像名 一般是用来测试,用完就删除 [root@iz2zeak7sgj6i7hrb2g862z ~]# docker run -it --rm tomcat:9.0 --rm Automatically remove the container when it exits 用完即删 #下载 最新版 [root@abc ~]# docker pull tomcat:9.0 9.0: Pulling from library/tomcat Digest: sha256:cd96d4f7d3f5fc4d3bc1622ec678207087b8215d55021a607ecaefba80b403ea Status: Image is up to date for tomcat:9.0 docker.io/library/tomcat:9.0 #查看下载的镜像 [root@abc ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 4 months ago 141MB tomcat 9.0 b8e65a4d736d 5 months ago 680MB mysql 5.7 c20987f18b13 5 months ago 448MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos latest 5d0da3dc9764 8 months ago 231MB #以后台方式,暴露端口方式,启动运行 # docker run -d -p 3355:8080 --name tomcat02 tomcat #测试访问有没有问题 curl localhost:3355 报出404错误,说明可以访问,但是tomcat有问题,不完整 #根据容器id进入tomcat容器 [root@iz2zeak7sgj6i7hrb2g862z ~]# docker exec -it 645596565d3f /bin/bash root@645596565d3f:/usr/local/tomcat# #查看tomcat容器内部内容: root@645596565d3f:/usr/local/tomcat# ls -l total 152 -rw-r--r-- 1 root root 18982 May 5 20:40 BUILDING.txt -rw-r--r-- 1 root root 5409 May 5 20:40 CONTRIBUTING.md -rw-r--r-- 1 root root 57092 May 5 20:40 LICENSE -rw-r--r-- 1 root root 2333 May 5 20:40 NOTICE -rw-r--r-- 1 root root 3255 May 5 20:40 README.md -rw-r--r-- 1 root root 6898 May 5 20:40 RELEASE-NOTES -rw-r--r-- 1 root root 16262 May 5 20:40 RUNNING.txt drwxr-xr-x 2 root root 4096 May 16 12:05 bin drwxr-xr-x 1 root root 4096 May 21 11:04 conf drwxr-xr-x 2 root root 4096 May 16 12:05 lib drwxrwxrwx 1 root root 4096 May 21 11:04 logs drwxr-xr-x 2 root root 4096 May 16 12:05 native-jni-lib drwxrwxrwx 2 root root 4096 May 16 12:05 temp drwxr-xr-x 2 root root 4096 May 16 12:05 webapps drwxr-xr-x 7 root root 4096 May 5 20:37 webapps.dist drwxrwxrwx 2 root root 4096 May 5 20:36 work root@645596565d3f:/usr/local/tomcat# ------------------------------------------- #进入webapps目录 root@645596565d3f:/usr/local/tomcat# cd webapps root@645596565d3f:/usr/local/tomcat/webapps# ls root@645596565d3f:/usr/local/tomcat/webapps# # 发现问题:1、linux命令少了。 2.webapps目录为空 # 原因:阿里云镜像的原因,阿里云默认是最小的镜像,所以不必要的都剔除掉 # 保证最小可运行的环境! # 解决方案: # 将webapps.dist下的文件都拷贝到webapps下即可 root@645596565d3f:/usr/local/tomcat# ls 找到webapps.dist BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work root@645596565d3f:/usr/local/tomcat# cd webapps.dist/ # 进入webapps.dist root@645596565d3f:/usr/local/tomcat/webapps.dist# ls # 查看内容 ROOT docs examples host-manager manager root@645596565d3f:/usr/local/tomcat/webapps.dist# cd .. root@645596565d3f:/usr/local/tomcat# cp -r webapps.dist/* webapps # 拷贝webapps.dist 内容给webapps root@645596565d3f:/usr/local/tomcat# cd webapps #进入webapps root@645596565d3f:/usr/local/tomcat/webapps# ls #查看拷贝结果 ROOT docs examples host-manager manager
这样tomcat就没问题了,外网访问时候直接 主机地址:3355 即可
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JOdSXe21-1654497083801)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061426549.png)]
# es 暴露的端口很多! # es 十分耗内存 # es 的数据一般需要放置到安全目录!挂载 # --net somenetwork ? 网络配置 # 启动elasticsearch [root@iz2zeak7sgj6i7hrb2g862z ~]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2 安装后会很卡 # 测试一下es是否成功启动 ➜ ~ curl localhost:9200 { "name" : "d73ad2f22dd3", "cluster_name" : "docker-cluster", "cluster_uuid" : "atFKgANxS8CzgIyCB8PGxA", "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" } #测试成功就关掉elasticSearch,防止耗内存 [root@iz2zeak7sgj6i7hrb2g862z ~]# docker stats # 查看docker容器使用内存情况 ------------------------------------------------------------------------------------------------------- #测试成功就关掉elasticSearch,可以添加内存的限制,修改配置文件 -e 环境配置修改(看自己的容器号,这个可能也是copy别人的,所以号不一样) ➜ ~ docker rm -f d73ad2f22dd3 # stop命令也行 ➜ ~ docker run -d --name elasticsearch01 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2 docker stats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 56d4fde935d2 elasticsearch01 0.52% 395.9MiB / 1.795GiB 21.54% 656B / 0B 106MB / 729kB 43 ------------------------------------------------------------ [root@abc ~]# curl localhost:9200 { "name" : "56d4fde935d2", "cluster_name" : "docker-cluster", "cluster_uuid" : "VCzCca-fRmCpeINIBPUoTQ", "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" }
# 下载,安装,测试
docker run -d -p 8088:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
# 这时候就可以打开外网地址访问8088接口了
除了外网访问也可以内网访问
curl localhost:8088
进入之后的面板
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PTOrPwxx-1654497083802)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061426508.png)]
镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码、运行时、库、环境变量和配置文件。
所有的应用,直接打包docker镜像,就可以直接跑起来,不需要运维部署环境了
如何得到镜像:
UnionFS(联合文件系统):Union文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem)。Union 文件系统是 Docker 镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录。
git每次操作都有记录,docker pull 下载时候看到一层一层的就是这个
A软件有的部分,B软件需要的话就不用重新下载了
**bootfs(boot file system)**主要包含bootloader和kernel, bootloader主要是引导加载kernel, Linux刚启动时会加载bootfs文件系统,在Docker镜像的最底层是bootfs。这一层与我们典型的Linux/Unix系统是一样的,包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已由bootfs转交给内核,此时系统也会卸载bootfs。
rootfs (root file system),在bootfs之上。包含的就是典型 Linux 系统中的 /dev,/proc,/bin,/etc 等标准目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu,Centos等等。
平时我们安装虚拟机的centos都是好几个G,为什么Docker这里才200M?因为是精简过的,引导没了,只有文件,效率最大化。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4ijxK3Nd-1654497083803)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061427771.png)]
对于一个精简的OS,rootfs 可以很小,只需要包含最基本的命令,工具和程序库就可以了,因为底层直接用Host的kernel,自己只需要提供rootfs就可以了。由此可见对于不同的linux发行版, bootfs基本是一致的,rootfs会有差别,因此不同的发行版可以公用bootfs。
虚拟机是分钟级别的,容器是秒级别的。
任何系统安装需要引导加载,所以Bootfs
黑屏-开机-系统 这过程就是种加载
容器就是小的虚拟机环境
底层的内核是不变的
我们去下载一个镜像,注意观察下载的日志输出,可以看到是一层一层的在下载!
最大的好处,我觉得莫过于是资源共享了,比如有多个镜像都从相同的Base镜像构建而来,那么宿主机只需要在磁盘上保留一份base镜像,同时内存中只需要加载一份base镜像,这样就可以为多有的容器服务了,而且每一层都可以被共享
查看镜像分层的方式可以通过docker image inspect 命令
[root@abc ~]# clear [root@abc ~]# docker pull redis Using default tag: latest latest: Pulling from library/redis a2abf6c4d29d: Already exists c7a4e4382001: Pull complete 4044b9ba67c9: Pull complete c8388a79482f: Pull complete 413c8bb60be2: Pull complete 1abfd3011519: Pull complete Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339 Status: Downloaded newer image for redis:latest docker.io/library/redis:latest [root@abc ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 4 months ago 141MB tomcat 9.0 b8e65a4d736d 5 months ago 680MB tomcat latest fb5657adc892 5 months ago 680MB redis latest 7614ae9453d1 5 months ago 113MB mysql 5.7 c20987f18b13 5 months ago 448MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos latest 5d0da3dc9764 8 months ago 231MB portainer/portainer latest 580c0e4e98b0 14 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB [root@abc ~]# docker image inspect redis:latest [ { "Id": "sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631", "RepoTags": [ "redis:latest" ], "RepoDigests": [ "redis@sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339" ], "Parent": "", "Comment": "", "Created": "2021-12-21T12:42:49.755107412Z", "Container": "13d25f53410417c5220c8dfe8bd49f06abdbcd69faa62a9b877de02464bb04a3", "ContainerConfig": { "Hostname": "13d25f534104", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "6379/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.12", "REDIS_VERSION=6.2.6", "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.2.6.tar.gz", "REDIS_DOWNLOAD_SHA=5b2b8b7a50111ef395bf1c1d5be11e6e167ac018125055daa8b5c2317ae131ab" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD [\"redis-server\"]" ], "Image": "sha256:e093f59d716c95cfce82c676f099b960cc700432ab531388fcedf79932fc81ec", "Volumes": { "/data": {} }, "WorkingDir": "/data", "Entrypoint": [ "docker-entrypoint.sh" ], "OnBuild": null, "Labels": {} }, "DockerVersion": "20.10.7", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "6379/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.12", "REDIS_VERSION=6.2.6", "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.2.6.tar.gz", "REDIS_DOWNLOAD_SHA=5b2b8b7a50111ef395bf1c1d5be11e6e167ac018125055daa8b5c2317ae131ab" ], "Cmd": [ "redis-server" ], "Image": "sha256:e093f59d716c95cfce82c676f099b960cc700432ab531388fcedf79932fc81ec", "Volumes": { "/data": {} }, "WorkingDir": "/data", "Entrypoint": [ "docker-entrypoint.sh" ], "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 112691373, "VirtualSize": 112691373, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/814f9dba4508a65e32865be3b4c39a6b12e613c3bd0e51fb7df567751a56451e/diff:/var/lib/docker/overlay2/0c50d6f9f9c40df14323924274be9f9454cee6887bc010be48aa335bfa4401dc/diff:/var/lib/docker/overlay2/e14651278e9e761c7a453b05f9a710f82297609d3cee6648e5eb162a05672255/diff:/var/lib/docker/overlay2/0ff367cff8b761e37cc58b1e730e22a07b30a41d02b9c6cdc6c5957d3120590f/diff:/var/lib/docker/overlay2/a7649e0248b14af93e37d6a86a45cdabf34ee586ad27f33615784d61978ec2d0/diff", "MergedDir": "/var/lib/docker/overlay2/dd53d8aa6067c0ccf4f7d7695b7a66faf778db164fe126e9ccf7350ef97c4435/merged", "UpperDir": "/var/lib/docker/overlay2/dd53d8aa6067c0ccf4f7d7695b7a66faf778db164fe126e9ccf7350ef97c4435/diff", "WorkDir": "/var/lib/docker/overlay2/dd53d8aa6067c0ccf4f7d7695b7a66faf778db164fe126e9ccf7350ef97c4435/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f", "sha256:9b24afeb7c2f21e50a686ead025823cd2c6e9730c013ca77ad5f115c079b57cb", "sha256:4b8e2801e0f956a4220c32e2c8b0a590e6f9bd2420ec65453685246b82766ea1", "sha256:529cdb636f61e95ab91a62a51526a84fd7314d6aab0d414040796150b4522372", "sha256:9975392591f2777d6bf4d9919ad1b2c9afa12f9a9b4d260f45025ec3cc9b18ed", "sha256:8e5669d8329116b8444b9bbb1663dda568ede12d3dbcce950199b582f6e94952" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } } ]
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f",
"sha256:9b24afeb7c2f21e50a686ead025823cd2c6e9730c013ca77ad5f115c079b57cb",
"sha256:4b8e2801e0f956a4220c32e2c8b0a590e6f9bd2420ec65453685246b82766ea1",
"sha256:529cdb636f61e95ab91a62a51526a84fd7314d6aab0d414040796150b4522372",
"sha256:9975392591f2777d6bf4d9919ad1b2c9afa12f9a9b4d260f45025ec3cc9b18ed",
"sha256:8e5669d8329116b8444b9bbb1663dda568ede12d3dbcce950199b582f6e94952"
]
},
理解:
所有的 Docker|镜像都起始于一个基础镜像层,当进行修改或增加新的内容时,就会在当前镜像层之上,创建新的镜像层。
举一个简单的例子,假如基于 Ubuntu Linux 16.04 创建一个新的镜像,这就是新镜像的第一层;如果在该镜像中添加 Python包,就会在基础镜像层之上创建第二个镜像层;如果继续添加一个安全补丁,就会创建第三个镜像层。
在添加额外的镜像层的同时,镜像始终是保持当前所有镜像的组合,理解这一点是非常重要的。下图距举了一个简单的例子,每个镜像包含三个文件,而镜像包含了来自两个镜像层的6个文件。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pRXOsJ7Y-1654497083804)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061427410.png)]
上图中的镜像层和之前的图中略有区别,主要目的是便于展示文件
下图中展示了一个稍微复杂的三层镜像,在外部看来整个镜像只有6个文件,这是因为最上层的文件7是文件5的一个更新版
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xuR9FT68-1654497083804)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061427985.png)]
这种情况下,上层镜像层中的文件覆盖了底层镜像层中的文件。这样就使得文件的更新版本作为一个新镜像层添加到镜像当中。 Docker 通过存储引擎(新版本采用快照机制)的方式来实现镜像层堆栈,并保证多镜像层对外展示为统一的文件系統。 Linux 上可用的存储引擎有 AUFS、Overlay2、Device Mapper、Btrfs 以及 ZFS。顾名思义,每种存储引擎都基于 Linux 中对应的文件系统或者块设备技术,并且每种存储引擎都有其独有的性能特点。
下图展示了与系统显示相同的三层镜像。所有镜像层堆叠并合井,对外提供统一的视图。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-boPIH6cZ-1654497083805)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061427597.png)]
Docker镜像都是只读的,当容器启动时,一个新的可写层被加载到镜像顶部!
这一层就是我们通常说的容器层,容器之下的都叫镜像层
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bHhJ00z9-1654497083805)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061427990.png)]
镜像层是远程下载过来的,无法改变。但是镜像层+操作可以组合成容器层,可以修改
如果你想要保存当前的状态,就通过commit提交,获得一个镜像,就好比我们以前学习VM时候,快照
# docker commit 提交容器成为一个新的副本 # 命令和git原理类似 docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:[tag] ------------------------------------------------------------ # 启动一个默认的tomcat # 发现这个默认的tomcat是没有webapps应用的, # 镜像的原因:官方镜像默认webapps下面没有文件的!# 我自己拷贝进去了基本的文件 # 将我们自己操作过后的容器通过commit提交为一个镜像,我们以后就可以使用自己修改过的镜像, # 这就是我们自己的一个修改过的镜像[root@localhost ~] ------------------------------------------------------------ # 实战测试 [root@abc ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@abc ~]# docker run -it -p 8080:8080 tomcat Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/openjdk-11 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar …………省略tomcat日志 =同时新建另一个访问窗口= [root@abc ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 33b2b22d068c tomcat "catalina.sh run" 37 seconds ago Up 36 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp awesome_lamarr [root@abc ~]# docker exec -it 33b2b22d068c /bin/bash root@33b2b22d068c:/usr/local/tomcat# cp -r webapps.dist/* webapps root@33b2b22d068c:/usr/local/tomcat# cd webapps root@33b2b22d068c:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager root@33b2b22d068c:/usr/local/tomcat# exit exit [root@abc ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 33b2b22d068c tomcat "catalina.sh run" 8 minutes ago Up 8 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp awesome_lamarr 同时外网测试,可用,可以访问 # 开始提交镜像 docker commit -a="yi_xia" -m="miao_shu" 33b2b22d068c tomcat02 [root@abc ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat02 latest a2f142e3d016 18 seconds ago 684MB nginx latest 605c77e624dd 4 months ago 141MB tomcat 9.0 b8e65a4d736d 5 months ago 680MB tomcat latest fb5657adc892 5 months ago 680MB redis latest 7614ae9453d1 5 months ago 113MB mysql 5.7 c20987f18b13 5 months ago 448MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos latest 5d0da3dc9764 8 months ago 231MB portainer/portainer latest 580c0e4e98b0 14 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB ----------------------------------------------------------------------------- # 例子2: [root@abc ~]# docker commit -a="yi_xia" -m="miao_shu" 33b2b22d068c my_tomcat_1.0 sha256:5bc7ddad0d13316199754a1ccaf59f395996a19424bd7a3361c487c08d12d89c [root@abc ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE my_tomcat_1.0 latest 5bc7ddad0d13 16 seconds ago 684MB # 我的自定义tomcat镜像 tomcat02 latest a2f142e3d016 2 minutes ago 684MB nginx latest 605c77e624dd 4 months ago 141MB tomcat 9.0 b8e65a4d736d 5 months ago 680MB tomcat latest fb5657adc892 5 months ago 680MB redis latest 7614ae9453d1 5 months ago 113MB mysql 5.7 c20987f18b13 5 months ago 448MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos latest 5d0da3dc9764 8 months ago 231MB portainer/portainer latest 580c0e4e98b0 14 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB
将应用环境打包成一个镜像!
数据?如果数据都在容器中,那么我们容器删除,数据就会丢失!需求:数据可以持久化
MySQL,容器删除了,删库跑路!需求:MySQL的数据可以存储在本地
容器之间可以有一个数据共享的技术!docker容器中产生的数据,同步到本地
这就是卷技术,目录的挂载,将容器内的目录挂载在Linux上面
总结一句话:容器的持久化和同步操作,容器间也是可以数据共享的
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9OqHP9A1-1654497083805)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061427157.png)]
docker run -it -v 主机目录:容器的目录 ------------------------------------------------------------- # 测试 运行时候设置挂载,主机目录:容器目录 [root@localhost home]# docker run -it -v /home/ceshi:/home centos /bin/bash 这时候打开新的窗口,查询本地/home目录下有无ceshi文件夹,发现有文件夹, 以后这两个文件夹就会同步,就像是windows的快捷方式一样,位置不同但是内容是同一个。 # 容器启动的时候我们可以通过docker inspect 容器id(主机里查看而不要在容器内查看,能看到个屁) [root@abc home]# docker inspect a0c66f548d9f 中间省略 "Mounts": [ { "Type": "bind", "Source": "/home/ceshi", 主机地址 "Destination": "/home", 容器地址 "Mode": "", "RW": true, "Propagation": "rprivate" } ], 中间省略
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zeOiuts2-1654497083805)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061427936.png)]
# 第二个测试
1.停止容器
2、宿主机上修改文件
3、启动容器
4、容器内的数据依旧是同步的
好处:我们以后修改只需要在本地进行修改即可,容器内部会自动进行同步!
# 下载MySql
docker pull mysql:5.7
# 运行MySql,需要做数据挂载,安装MySQL需要配置密码
-v 挂载
-d 后台
-p 端口映射
-e MYSQL_ROOT_PASSWORD=AAA 密码
# 在启动成功后,我们在本地使用sqlyog来测试连接一下# sqlyog--连接到服务器3310---3310和容器内部的3306进行映射,这个时候我们就可以连接上了# 在本地测试新建一个数据库,查看一下我们映射的路径是否ok
---------------------------------------------------------------
# 测试
[root@abc ceshi]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
728013542f3addfd2cd289e2e3636f599621ec5d18f78d908b8a02fa6216089c
使用本地第三方数据库连接工具进行测试:navicat
navicat 连接到服务器的3310,3310与容器内3306映射 所以可以访问
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ye2rtJ47-1654497083806)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061427826.png)]
新建数据库,本地的/home/mysql/data 里面就会出现新的数据库
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WuaGkxa0-1654497083806)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061427383.png)]
如果容器没了,只要挂载在本地,数据还是会出现在本机的。 这就是容器数据持久化功能。
# 匿名挂载, 注意这里使用-P表示的指定随机的端口号,没有指定主机的位置信息 -v 容器内路径 docker run -d -P --name nginx01 -v /etc/nginx nginx # 查看所有卷的情况 [root@abc _data]# docker volume ls DRIVER VOLUME NAME local 0fc82f426afafb02f2a6809628ceedfddfed2ceea939899840f4d2979a3bf331 local 05d862915f42c1d1858214838e44d9ab8fffd105e8c20bfe442b7a01b54c2f64 local 49c862d44de94dfcb8ddc5d0e5f16f82c777eedc2d2f699d2e1c85c764e2adb6 local 068897006a4793fb993836602acc606ace9f0abc4ef94f7a345a07d8ef9c2f64 local c6dab0a9dff5152873c8e5351f5101495c2e5ffc6fe523e64f70bf8c0de36712 local ccd2589cb34192b3cafa4011addba789eba2709da2d06181a7db28f2897410bc local d52122937d35957bef4138c05c00b4f4dc88a343d6de3030fa52a54bebc2470d local juming-nginx # 这里发现,这种就是匿名挂载,我们在-v只写了容器内的路径,没有写容器外的路径 ----------------------------------------------------------------------------- # 查看卷的具体信息 [root@abc _data]# docker inspect 0fc82f426afafb02f2a6809628ceedfddfed2ceea939899840f4d2979a3bf331 [ { "CreatedAt": "2022-05-26T10:03:48+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/0fc82f426afafb02f2a6809628ceedfddfed2ceea939899840f4d2979a3bf331/_data", "Name": "0fc82f426afafb02f2a6809628ceedfddfed2ceea939899840f4d2979a3bf331", "Options": null, "Scope": "local" } ]
# 具名挂载 docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx --------------------------------------------------------------------- [root@abc ~]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx 086e88b4858f6cca43c9d1237effb543d1e94749046665739bf8039c49d203e3 [root@abc ~]# docker volume ls DRIVER VOLUME NAME local 0fc82f426afafb02f2a6809628ceedfddfed2ceea939899840f4d2979a3bf331 local 05d862915f42c1d1858214838e44d9ab8fffd105e8c20bfe442b7a01b54c2f64 local 49c862d44de94dfcb8ddc5d0e5f16f82c777eedc2d2f699d2e1c85c764e2adb6 local 068897006a4793fb993836602acc606ace9f0abc4ef94f7a345a07d8ef9c2f64 local c6dab0a9dff5152873c8e5351f5101495c2e5ffc6fe523e64f70bf8c0de36712 local ccd2589cb34192b3cafa4011addba789eba2709da2d06181a7db28f2897410bc local d52122937d35957bef4138c05c00b4f4dc88a343d6de3030fa52a54bebc2470d local juming001-nginx local juming-nginx 与上面相比,多了个倒数第二行。也就是自己新加的卷 ------------------------------------------------------------------- # 查看具名nginx信息 [root@abc ~]# docker volume inspect juming-nginx [ { "CreatedAt": "2022-05-26T19:14:50+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",挂载的具体目录的地址 "Name": "juming-nginx", "Options": null, "Scope": "local" } ] Docker容器内的卷,没有指定目录情况下都是在 /var/lib/docker/volumes/XXXX/-data ------------------------------------------------------------------------------ # 进入目录查看dicker卷信息 [root@abc ~]# cd /var/lib/docker [root@abc docker]# ls buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes [root@abc docker]# cd volumes [root@abc volumes]# ls 068897006a4793fb993836602acc606ace9f0abc4ef94f7a345a07d8ef9c2f64 0fc82f426afafb02f2a6809628ceedfddfed2ceea939899840f4d2979a3bf331 49c862d44de94dfcb8ddc5d0e5f16f82c777eedc2d2f699d2e1c85c764e2adb6 backingFsBlockDev c6dab0a9dff5152873c8e5351f5101495c2e5ffc6fe523e64f70bf8c0de36712 ccd2589cb34192b3cafa4011addba789eba2709da2d06181a7db28f2897410bc d52122937d35957bef4138c05c00b4f4dc88a343d6de3030fa52a54bebc2470d juming-nginx metadata.db [root@abc volumes]# pwd /var/lib/docker/volumes [root@abc volumes]# cd juming-nginx [root@abc juming-nginx]# ls _data [root@abc juming-nginx]# cd _data [root@abc _data]# ls conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
Docker容器内的卷,没有指定目录情况下都是在 /var/lib/docker/volumes/XXXX/-data
我们通过具名挂载可以方便找到我们的一个卷,大多数情况下都是使用具名挂载
# 如何确定是匿名挂载还是具名挂载,还是指定路径挂载
-v 容器内路径 # 匿名挂载
-v 卷名:容器内路径 # 具名挂载,卷名没有路径/
-v 宿主机路径:容器内路径 # 指定路径挂载,一般/开头
1234
# 通过 -v 容器内路径:ro rw 改变读写权限
ro readonly # 只读
rw readwrite # 可读可写
# 一旦设置了这个容器权限,容器对我们挂载出来的内容就有了限定了
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx
# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法进行操作的
[root@abc home]# pwd /home [root@abc home]# ls ceshi dockerfile docker-test mysql test1.java test.java [root@abc home]# cd docker-test [root@abc docker-test]# vim testdf # 创建dockerfile文件并编写命令 [root@abc docker-test]# cat testdf # 打印编写的命令 FROM centos:7 VOLUME ["volume01","volume02"] CMD echo "-----------end-------------" CMD /bin/bash ---------------------------------------------------------- [root@abc docker-test]# docker build -f testdf -t centos-test20220527:0.2 . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:7 ---> eeb6ee3f44bd Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in 0fa0f5afa14b Removing intermediate container 0fa0f5afa14b ---> ba32b5f9e5ad Step 3/4 : CMD echo "-----------end-------------" ---> Running in 9dc06ce9febb Removing intermediate container 9dc06ce9febb ---> 28c6848d9ed0 Step 4/4 : CMD /bin/bash ---> Running in dc3f99a148df Removing intermediate container dc3f99a148df ---> 3bb636778c29 Successfully built 3bb636778c29 # 运行成功 Successfully tagged centos-test20220527:0.2 [root@abc docker-test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-test20220527 0.2 3bb636778c29 5 seconds ago 204MB mycentos20220527 0.1 32a73d064967 37 minutes ago 601MB <none> <none> cf1d4afcc58a 57 minutes ago 231MB my_tomcat_1.0 latest 5bc7ddad0d13 20 hours ago 684MB tomcat02 latest a2f142e3d016 20 hours ago 684MB nginx latest 605c77e624dd 4 months ago 141MB tomcat 9.0 b8e65a4d736d 5 months ago 680MB tomcat latest fb5657adc892 5 months ago 680MB redis latest 7614ae9453d1 5 months ago 113MB mysql 5.7 c20987f18b13 5 months ago 448MB docker latest 15a9bc7c6340 5 months ago 216MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos 7 eeb6ee3f44bd 8 months ago 204MB centos latest 5d0da3dc9764 8 months ago 231MB portainer/portainer latest 580c0e4e98b0 14 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB --------------------------------------------------------- [root@abc docker-test]# docker run -it 3bb636778c29 /bin/bash [root@fa0c7a431276 /]# ls anaconda-post.log dev home lib64 mnt proc run srv tmp var volume02 bin etc lib media opt root sbin sys usr volume01 这两个目录就是生成镜像时候自动挂载的目录,外界一定有同步的目录 ---------------------------------------------------------------- #回到主机,查看卷挂载目录路径 exit [root@abc ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21556eceea04 3bb636778c29 "/bin/bash" About a minute ago Up About a minute eloquent_raman [root@abc ~]# docker inspect 21556eceea04 "Mounts": [ { "Type": "volume", "Name": "8415d45427f1ec84c95008dd303fbb7ce49006a6aa780d730786b58971592b78", "Source": "/var/lib/docker/volumes/8415d45427f1ec84c95008dd303fbb7ce49006a6aa780d730786b58971592b78/_data", "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "316156f28050bc03f6539b009dbee43a316578fb3262965f1d6fea09200d59c1", "Source": "/var/lib/docker/volumes/316156f28050bc03f6539b009dbee43a316578fb3262965f1d6fea09200d59c1/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], 这种方式未来的使用非常多,因为我们自己通常会构建自己的镜像 假设构建镜像的时候没有挂载,需要自己手动挂载镜像 -v 卷名:容器内路径
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9rGS83zo-1654497083807)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428069.png)]
6.6 ## 数据卷容器
多个MySQL同步数据
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ad4D7ntp-1654497083807)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428124.png)]
启动3个上面的测试容器
docker01是父容器
[root@abc ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-test20220527 0.2 3bb636778c29 20 minutes ago 204MB
[root@abc ~]# docker run -it --name docker1 centos-test20220527:0.2
[root@90038b87d58f /]# ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var volume02
bin etc lib media opt root sbin sys usr volume01
# 等待docker2启动后在执行
[root@af6e6b4a9e97 /]# cd volume01
[root@af6e6b4a9e97 volume01]# ls
[root@af6e6b4a9e97 volume01]# touch 1.java
[root@90038b87d58f /]# [root@abc ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 90038b87d58f centos-test20220527:0.2 "/bin/sh -c /bin/bash" About a minute ago Up About a minute docker01 [root@abc ~]# docker run -it --name docker02 --volumes-from docker1 centos-test20220527:0.2 [root@b180d1e0df2d /]# ls anaconda-post.log dev home lib64 mnt proc run srv tmp var volume02 bin etc lib media opt root sbin sys usr volume01 [root@af6e6b4a9e97 volume01]# ls 啥都没有,空 # 等待docker1的创建命令执行后 [root@5710c55ca265 volume01]# ls 1.java 这时候就可以查询到了 docker1就是数据卷容器,被挂载
这时候创建docekr3容器,挂载在docker1上,也是可以同步的,且docker1,2,3都同步
[root@abc ~]# docker run -it --name docker03 --volumes-from docker1 centos-test20220527:0.2
[root@e098ca19f89b /]# ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var volume02
bin etc lib media opt root sbin sys usr volume01
[root@e098ca19f89b /]# cd volume01
[root@e098ca19f89b volume01]# ls
1.java
docker2,3都挂载在docker1上,删除docker1时候,docker2,3数据都有,且同步
这是因为docker1,2,3本质都是挂载在主机上某个目录的
docker run -d -p 3310:3306
-v /etc/mysql/conf.d
-v /var/lib/mysql
-e MYSQL_ROOT_PASSWORD=338218
--name mysql01 mysql:5.7
docker run -d -p 3311:3306
-e MYSQL_ROOT_PASSWORD=338218
--name mysql02
--volumes-from mysql01 mysql:5.7
# 这个时候可以实现两个容器数据同步
结论:
容器之间配置信息的传递,数据卷容器的生命周期这一直持续到没有容器使用为止
但是一旦持久化到了本地,这个时候,本地的数据不会被删除
dockerfile是用来构建docker镜像的文件,命令参数脚本
1. 编写一个dockerfile文件
2. docker build构建为一个镜像
3. docker run 运行镜像
4. docker push发布镜像(DockerHub、阿里云镜像仓库)
1、每个保留关键字(指令)都是必须是**大写字母**
2、执行从上到下顺序执行
3、#表示注释
4、每一个指令都会创建提交一个新的镜像层,并提交
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BCbDkMLA-1654497083807)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428365.png)]
dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简单!
Docker镜像逐渐成为了企业交付的标准,必须要掌握
步骤:开发,部署,运维…缺一不可
- DockerFile:构建文件,定义了一切的步骤,源代码
- DockerImages:通过DockerFile构建生成的镜像,最终发布和运行的产品
- Docker容器:容器就是镜像运行起来提供服务的
前面教的是使用Docker,现在学习的是如何创建Docker,先回用,后会制造
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QK8A5u2X-1654497083807)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428331.png)]
FROM # 基础镜像,一切从这里开始
MAINTAINER # 镜像是谁写的,姓名+邮箱
RUN # 镜像构建的时候需要运行的命令
ADD # 步骤:往里面加东西:tomcat镜像,这个tomcat压缩包,添加内容
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOSE # 暴露端口配置
CMD # 指定容器运行时的shell命令,只有最后一个生效,可被替代
ENTRYPOINT # 指定容器运行时的shell命令,可以追加命令
ONBUILD # 当构建一个被继承DockerFile这个时候就会运行ONBUILD的指令,触发指令
COPY # 类似ADD,将我们文件拷贝到镜像中
ENV # 构建的时候设置环境变量
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q01NqHsG-1654497083808)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428828.png)]
Docker Hub中99%的镜像都是从这个基础镜像过来的FROM scratch,然后配置需要的软件和配置来进行构建的
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CeUQ7PLP-1654497083808)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428872.png)]
# 主机根目录下/home 创建dockerfile文件夹 [root@abc ~]# cd /home [root@abc home]# ls ceshi mysql test1.java test.java [root@abc home]# mkdir dockerfile [root@abc home]# ls ceshi dockerfile mysql test1.java test.java [root@abc home]# cd dockerfile [root@abc dockerfile]# :7 1. 编写DockerFile文件 # 官方的centos没有 vim,i'f'con'fig ,自定义镜像的目标就是加上vim # 开始编写自定义DockerFile文件 [root@abc dockerfile]# vim mydf [root@abc dockerfile]# cat mydf FROM centos:7 MAINTAINER YiXia20220527<yxmiaoyu@163.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@abc dockerfile]# 2. 构建镜像 [root@abc dockerfile]# docker build -f mydf -t mycentos20220527:0.1 . Sending build context to Docker daemon 27.65kB ………… Step 10/10 : CMD /bin/bash ---> Running in 3c823cdde8d3 Removing intermediate container 3c823cdde8d3 ---> 32a73d064967 Successfully built 32a73d064967 # 运行成功了 Successfully tagged mycentos20220527:0.1 [root@abc dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mycentos20220527 0.1 32a73d064967 13 minutes ago 601MB # 我的自定义镜像 <none> <none> cf1d4afcc58a 33 minutes ago 231MB my_tomcat_1.0 latest 5bc7ddad0d13 19 hours ago 684MB tomcat02 latest a2f142e3d016 19 hours ago 684MB nginx latest 605c77e624dd 4 months ago 141MB tomcat 9.0 b8e65a4d736d 5 months ago 680MB tomcat latest fb5657adc892 5 months ago 680MB redis latest 7614ae9453d1 5 months ago 113MB mysql 5.7 c20987f18b13 5 months ago 448MB docker latest 15a9bc7c6340 5 months ago 216MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos 7 eeb6ee3f44bd 8 months ago 204MB centos latest 5d0da3dc9764 8 months ago 231MB portainer/portainer latest 580c0e4e98b0 14 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB 3.运行镜像 [root@abc dockerfile]# docker run -it mycentos20220527:0.1 # 不带版本号的话会默认去找最新的 [root@ffe933c501d4 local]# pwd /usr/local [root@ffe933c501d4 local]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 8 bytes 656 (656.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@abc dockerfile]# docker history c20987f18b13 IMAGE CREATED CREATED BY SIZE COMMENT c20987f18b13 5 months ago /bin/sh -c #(nop) CMD ["mysqld"] 0B <missing> 5 months ago /bin/sh -c #(nop) EXPOSE 3306 33060 0B <missing> 5 months ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B <missing> 5 months ago /bin/sh -c ln -s usr/local/bin/docker-entryp… 34B <missing> 5 months ago /bin/sh -c #(nop) COPY file:345a22fe55d3e678… 14.5kB <missing> 5 months ago /bin/sh -c #(nop) VOLUME [/var/lib/mysql] 0B <missing> 5 months ago /bin/sh -c { echo mysql-community-server m… 313MB <missing> 5 months ago /bin/sh -c echo 'deb http://repo.mysql.com/a… 55B <missing> 5 months ago /bin/sh -c #(nop) ENV MYSQL_VERSION=5.7.36-… 0B <missing> 5 months ago /bin/sh -c #(nop) ENV MYSQL_MAJOR=5.7 0B <missing> 5 months ago /bin/sh -c set -ex; key='A4A9406876FCBD3C45… 1.84kB <missing> 5 months ago /bin/sh -c apt-get update && apt-get install… 52.2MB <missing> 5 months ago /bin/sh -c mkdir /docker-entrypoint-initdb.d 0B <missing> 5 months ago /bin/sh -c set -eux; savedAptMark="$(apt-ma… 4.17MB <missing> 5 months ago /bin/sh -c #(nop) ENV GOSU_VERSION=1.12 0B <missing> 5 months ago /bin/sh -c apt-get update && apt-get install… 9.34MB <missing> 5 months ago /bin/sh -c groupadd -r mysql && useradd -r -… 329kB <missing> 5 months ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 5 months ago /bin/sh -c #(nop) ADD file:bd5c9e0e0145fe33b… 69.3MB
Vim
Vim 是从 vi 发展出来的一个文本编辑器。代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用。
简单的来说, vi 是老式的字处理器,不过功能已经很齐全了,但是还是有可以进步的地方。 vim 则可以说是程序开发者的一项很好用的工具。
连 vim 的官方网站 (https://www.vim.org/) 自己也说 vim 是一个程序开发工具而不是文字处理软件。
vi/vim 的使用
基本上 vi/vim 共分为三种模式,分别是命令模式(Command mode),输入模式(Insert mode)和底线命令模式(Last line mode)。 这三种模式的作用分别是:
命令模式:
用户刚刚启动 vi/vim,便进入了命令模式。
此状态下敲击键盘动作会被Vim识别为命令,而非输入字符。比如我们此时按下i,并不会输入一个字符,i被当作了一个命令。
以下是常用的几个命令:
- i 切换到输入模式,以输入字符。
- x 删除当前光标所在处的字符。
- : 切换到底线命令模式,以在最底一行输入命令。
若想要编辑文本:启动Vim,进入了命令模式,按下i,切换到输入模式。
命令模式只有一些最基本的命令,因此仍要依靠底线命令模式输入更多命令。
输入模式
在命令模式下按下i就进入了输入模式。
在输入模式中,可以使用以下按键:
- 字符按键以及Shift组合,输入字符
- ENTER,回车键,换行
- BACK SPACE,退格键,删除光标前一个字符
- DEL,删除键,删除光标后一个字符
- 方向键,在文本中移动光标
- HOME/END,移动光标到行首/行尾
- Page Up/Page Down,上/下翻页
- Insert,切换光标为输入/替换模式,光标将变成竖线/下划线
- ESC,退出输入模式,切换到命令模式
底线命令模式
在命令模式下按下:(英文冒号)就进入了底线命令模式。
底线命令模式可以输入单个或多个字符的命令,可用的命令非常多。
在底线命令模式中,基本的命令有(已经省略了冒号):
- q 退出程序
- w 保存文件
按ESC键可随时退出底线命令模式。
还是按照上面的新建dockerFile一样,同样操作流程,新建自定义的dockerfile文件。
[root@abc ~]# clear [root@abc ~]# cd /home [root@abc home]# ls ceshi dockerfile docker-test mysql test1.java test.java [root@abc home]# cd dockerfile [root@abc dockerfile]# ls mydf [root@abc dockerfile]# vim mydf1 [root@abc dockerfile]# cat mydf1 FROM centos CMD ["ls","-a"] [root@abc dockerfile]# docker build -f mydf1 -t mydf1 . Sending build context to Docker daemon 28.67kB Step 1/2 : FROM centos ---> 5d0da3dc9764 Step 2/2 : CMD ["ls","-a"] ---> Running in 06bab16405d8 Removing intermediate container 06bab16405d8 ---> 184d81c40f1b Successfully built 184d81c40f1b Successfully tagged mydf1:latest # 构建镜像成功后直接执行 [root@abc dockerfile]# docker run 184d81c40f1b . .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var # 发现ls -a命令生效了 [root@abc dockerfile]# docker run 184d81c40f1b -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. 报错了,无法追加-l命令,用CMD情况下,-l替换了CMD命令,所以报错 -l换成 ls -al才可以执行 ------------------------------------------------------------
[root@abc dockerfile]# vim mydf2 [root@abc dockerfile]# cat mydf2 FROM centos:7 ENTRYPOINT ["ls","-a"] [root@abc dockerfile]# docker build -f mydf2 -t mydf2 . Sending build context to Docker daemon 42.5kB Step 1/2 : FROM centos:7 ---> eeb6ee3f44bd Step 2/2 : ENTRYPOINT ["ls","-a"] ---> Running in 0684641436cf Removing intermediate container 0684641436cf ---> 529e9267c719 Successfully built 529e9267c719 Successfully tagged mydf2:latest [root@abc dockerfile]# docker run mydf2 . .. .dockerenv anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@abc dockerfile]# docker run mydf2 -l total 64 drwxr-xr-x 1 root root 4096 May 27 07:43 . drwxr-xr-x 1 root root 4096 May 27 07:43 .. -rwxr-xr-x 1 root root 0 May 27 07:43 .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 340 May 27 07:43 dev drwxr-xr-x 1 root root 4096 May 27 07:43 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 94 root root 0 May 27 07:43 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 May 26 02:23 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 对比最后两次命令效果,就可以发现 ENTRYPOINT 不覆盖,CMD覆盖
要求:tomcat压缩包 , jdk的压缩包
[root@abc:~/tomcat镜像]# ls
apache-tomcat-9.0.8-fulldocs.tar.gz jdk-8u333-linux-x64.tar.gz
[root@abc:~/tomcat镜像]# touch readme.txt [root@abc:~/tomcat镜像]# vim Dockerfile ---------------------------------- ~ FROM centos:7 # 来源,选择版本的原因是因为下面下载yum时候会报错 MAINTAINER yx<yxmiaoyu@163.com> # 作者信息 COPY readme.txt /usr/local/readme.txt # 复制文件 # 添加安装包,ADD命令添加时候会自动解压 ADD apache-tomcat-9.0.8-fulldocs.tar.gz /usr/local/ ADD jdk-8u333-linux-x64.tar.gz /usr/local/ # 添加vim命令 RUN yum -y install vim # 设置环境变量,可以自动进入路径 ENV MYPATH /usr/local # 工作目录 WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_333 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.8 ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.8 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin # 暴露端口 EXPOSE 8080 CMD /usr/local/apache-tomcat-9.0.8/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.8/bin/logs/catalina.out --------------------------------------------------------------------------- [root@abc:~/tomcat镜像]# ll total 145564 -rw-r--r-- 1 root root 1048576 Jun 5 16:53 apache-tomcat-9.0.8-fulldocs.tar.gz -rw-r--r-- 1 root root 637 Jun 5 17:13 Dockerfile -rw-r--r-- 1 root root 148003421 Jun 5 16:56 jdk-8u333-linux-x64.tar.gz -rw-r--r-- 1 root root 0 Jun 5 16:58 readme.txt
docker build -t diytomcat .
[root@abc:~/mytomcatimage]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE diytomcat latest ce1984715920 6 minutes ago 859MB <none> <none> 96e677b452df 19 minutes ago 660MB tomcat04 latest c03fb94df78b About an hour ago 684MB tomcat_image_01 latest ab3d09873dfe 6 days ago 684MB grafana/grafana-oss 8.3.5 12ec1c89101f 4 months ago 274MB nginx latest 605c77e624dd 5 months ago 141MB tomcat 9.0 b8e65a4d736d 5 months ago 680MB tomcat latest fb5657adc892 5 months ago 680MB redis latest 7614ae9453d1 5 months ago 113MB mysql 5.7 c20987f18b13 5 months ago 448MB mysql latest 3218b38490ce 5 months ago 516MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos 7 eeb6ee3f44bd 8 months ago 204MB centos 8 5d0da3dc9764 8 months ago 231MB centos latest 5d0da3dc9764 8 months ago 231MB docker run -d -p 9090:8080 --name diytomcat0605 -v /home/eric/build/tomcat/test:/usr/local/apache-tomcat-9.0.8/webapps/test -v /home/eric/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.8/logs diytomcat ---------------- # 解释,第一个挂载是将webapps下的test映射到本地tomcat下的test -v /home/eric/build/tomcat/test:/usr/local/apache-tomcat-9.0.8/webapps/test # 解释,第二个挂载是将apache-tomcat-9.0.8下的日志映射到本地tomcat下的tomcatlogs -v /home/eric/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.8/logs
挂载成功
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nSfEYP64-1654497083809)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428991.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a4TBdzNh-1654497083809)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428178.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kuGB7eeQ-1654497083810)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428293.png)]
(由于做了卷挂载,我们直接在本地编写项目就可以发布了)
# 首先进入主机同步的那个文件夹 # 在本地tomcat/test文件夹下创建WEB-INF文件夹,在WEB-INF下编写web.xml文, # 在/test 下编写index.jsp文件, # 再访问http://192.168.58.129:9090/test/ [root@abc:/home/eric/build/tomcat/test]# pwd /home/eric/build/tomcat/test [root@abc:/home/eric/build/tomcat/test]# mkdir WEB-INF [root@abc:/home/eric/build/tomcat/test]# ls WEB-INF [root@abc:/home/eric/build/tomcat/test]# cd WEB-INF [root@abc:/home/eric/build/tomcat/test/WEB-INF]# vim web.xml ----------------------------------------------------------------------------------- # 这是web.xml内容 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> </web-app> ------------------------------------------------------------------------------------- [root@abc:/home/eric/build/tomcat/test/WEB-INF]# vim index.jsp ------------------------------------------------------------------------------------- # 这是index.jsp内容 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>hello. xiaofan</title> </head> <body> Hello World!<br/> <% System.out.println("-----my test web logs------"); %> </body> </html> -------------------------------------------------------------------------------------
发现:项目部署成功, 可以直接访问ok!
我们以后开发的步骤:需要掌握Dockerfile的编写! 我们之后的一切都是使用docker进行来发布运行的!
1、地址https://hub.docker.com/
2、注册账号,确定这个账号可以登录 邮箱 + 经典密码 注意首字母大写
3、登录服务器
# 使用docker login -u [用户名],然后输入密码就可以登录成功
[root@abc:~]# docker login -u dockerlinshi
Password: 这里跟linux一样,输入了看不见
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
4、登录完毕后就可以提交上去镜像,就是一步 docker push
docker push 用户名/镜像名:版本号
docker push dockerlinshi/tomcat:1.0
# 如果提示重复了,就在本地给镜像加个标签,然后提交新的镜像
# 解决,增加一个tag
[root@abc:~]# docker tag diytomcat dockerlinshi/tomcat:1.0
diytomcat是我原始的镜像,,dockerlinshi/tomcat是新的镜像
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PSiPNci7-1654497083810)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428338.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qxcvduEa-1654497083810)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428788.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YgVsr0oC-1654497083810)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428962.png)]
1. 登录阿里云Docker Registry $ docker login --username=aliyun4277238300 registry.cn-beijing.aliyuncs.com 用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码。 您可以在访问凭证页面修改凭证密码。 2. 从Registry中拉取镜像 $ docker pull registry.cn-beijing.aliyuncs.com/yx_images_test/blilbili:[镜像版本号] 3. 将镜像推送到Registry $ docker login --username=aliyun4277238300 registry.cn-beijing.aliyuncs.com $ docker tag [ImageId] registry.cn-beijing.aliyuncs.com/yx_images_test/blilbili:[镜像版本号] $ docker push registry.cn-beijing.aliyuncs.com/yx_images_test/blilbili:[镜像版本号] 请根据实际镜像信息替换示例中的[ImageId]和[镜像版本号]参数。 4. 选择合适的镜像仓库地址 从ECS推送镜像时,可以选择使用镜像仓库内网地址。推送速度将得到提升并且将不会损耗您的公网流量。 如果您使用的机器位于VPC网络,请使用 registry-vpc.cn-beijing.aliyuncs.com 作为Registry的域名登录。 5. 示例 使用"docker tag"命令重命名镜像,并将它通过专有网络地址推送至Registry。 $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE registry.aliyuncs.com/acs/agent 0.7-dfb6816 37bb9c63c8b2 7 days ago 37.89 MB $ docker tag 37bb9c63c8b2 registry-vpc.cn-beijing.aliyuncs.com/acs/agent:0.7-dfb6816 使用 "docker push" 命令将该镜像推送至远程。 $ docker push registry-vpc.cn-beijing.aliyuncs.com/acs/agent:0.7-dfb6816 ---------------------------------------------------------------------------------- 下面是自己的测试例子 ---------------------------------------------------------------------------------- [root@abc:~]# docker login --username=aliyun4277238300 registry.cn-beijing.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@abc:~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE dockerlinshi/tomcat 1.0 399ae3410d85 About an hour ago 813MB diytomcat latest 399ae3410d85 About an hour ago 813MB <none> <none> ce1984715920 2 hours ago 859MB <none> <none> 96e677b452df 2 hours ago 660MB tomcat04 latest c03fb94df78b 3 hours ago 684MB tomcat_image_01 latest ab3d09873dfe 6 days ago 684MB grafana/grafana-oss 8.3.5 12ec1c89101f 4 months ago 274MB nginx latest 605c77e624dd 5 months ago 141MB tomcat 9.0 b8e65a4d736d 5 months ago 680MB tomcat latest fb5657adc892 5 months ago 680MB redis latest 7614ae9453d1 5 months ago 113MB mysql 5.7 c20987f18b13 5 months ago 448MB mysql latest 3218b38490ce 5 months ago 516MB hello-world latest feb5d9fea6a5 8 months ago 13.3kB centos 7 eeb6ee3f44bd 8 months ago 204MB centos 8 5d0da3dc9764 8 months ago 231MB centos latest 5d0da3dc9764 8 months ago 231MB [root@abc:~]# docker push dockerlinshi/tomcat:1.0 The push refers to repository [docker.io/dockerlinshi/tomcat] 5cd69635503b: Preparing 1053c7b8f435: Pushing [=========================================> ] 303.7MB/369.1MB 922d03f9ded4: Preparing 955bddf53e59: Preparing 174f56854903: Preparing denied: requested access to the resource is denied
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4Ymtcjs5-1654497083814)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428797.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-13bziCBc-1654497083814)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061428507.png)]
准备工作:清空所有镜像与容器,方便部署
docker rmi -f $(docker images -aq)
docker rm -f $(docker ps -aq
[root@abc:~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:16:3e:08:30:f7 brd ff:ff:ff:ff:ff:ff inet 172.26.211.8/20 brd 172.26.223.255 scope global dynamic eth0 valid_lft 314322933sec preferred_lft 314322933sec inet6 fe80::216:3eff:fe08:30f7/64 scope link valid_lft forever preferred_lft forever 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:88:a1:23:21 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:88ff:fea1:2321/64 scope link valid_lft forever preferred_lft forever ---------------------------------------------------------
有三个网络,代表三种不同环境。
# 问题: docker是如何处理容器网络访问的? [root@abc:~]# docker run -d -P --name tomcat01 tomcat:7 # 注意,新版本的tomcat没有ipaddr命令,需要自己装一个 [root@abc:~]# docker exec -it tomcat02 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 188: eth0@if189: <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 eth0的地址 经过测试可以ping通,服务器生产的东西当然可以ping # Linux可以ping通docker容器内部
1、我们每启动一个docker容器,docker就会给docker容器分配一个ip,我们只要安装了docker,就会有一个网卡 docker0桥接模式 ,使用的技术是 evth-pair技术
再次测试ip addr,多了一对网卡
2、再启动一个容器测试,发现又多了一对189网卡,189网卡就是新建的容器的ip地址
[root@abc:~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:16:3e:08:30:f7 brd ff:ff:ff:ff:ff:ff inet 172.26.211.8/20 brd 172.26.223.255 scope global dynamic eth0 valid_lft 314321289sec preferred_lft 314321289sec inet6 fe80::216:3eff:fe08:30f7/64 scope link valid_lft forever preferred_lft forever 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:88:a1:23:21 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:88ff:fea1:2321/64 scope link valid_lft forever preferred_lft forever 189: veth40237f6@if188: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 32:0f:67:0c:0c:3b brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::300f:67ff:fe0c:c3b/64 scope link valid_lft forever preferred_lft forever
新增的ip地址为 189.188 ,发现于上面进入容器看到的ip地址是相同的。
在启动一个测试
[root@abc:~]# docker run -d -P --name tomcat02 tomcat:7 85f00f77b18217a334a1af9d5ba6da0ccf8b8db2883db6b512836f2dd60bf0d4 [root@abc:~]# docker exec -it tomcat02 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 190: eth0@if191: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever [root@abc:~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:16:3e:08:30:f7 brd ff:ff:ff:ff:ff:ff inet 172.26.211.8/20 brd 172.26.223.255 scope global dynamic eth0 valid_lft 314320917sec preferred_lft 314320917sec inet6 fe80::216:3eff:fe08:30f7/64 scope link valid_lft forever preferred_lft forever 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:88:a1:23:21 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:88ff:fea1:2321/64 scope link valid_lft forever preferred_lft forever 189: veth40237f6@if188: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 32:0f:67:0c:0c:3b brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::300f:67ff:fe0c:c3b/64 scope link valid_lft forever preferred_lft forever 191: veth29c194f@if190: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 9a:d5:01:c2:c6:66 brd ff:ff:ff:ff:ff:ff link-netnsid 1 inet6 fe80::98d5:1ff:fec2:c666/64 scope link valid_lft forever preferred_lft forever 测试发现191.190 与上面的地址是一致的。
我们发现这些网卡都是一对对的
veth-pair 就是一对的虚拟设备接口,它们都是成对出现的,一段连着协议,一段彼此相连
正因为有这个特性veth-pair充当一个桥梁,连接各种虚拟网络设备
OpenStack,Docker容器之间的连接,OVS的连接,都是使用veth-pair技术
[root@abc:~]# docker exec -it tomcat01 ipaddr OCI runtime exec failed: exec failed: unable to start container process: exec: "ipaddr": executable file not found in $PATH: unknown [root@abc:~]# 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 188: eth0@if189: <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@abc:~]# docker exec -it tomcat02 ping 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.035 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.055 ms 两个tomcat容器之间可以ping通。
结论:tomcat01和tomcat02是共用的一个路由器,docker0
所有的容器在不指定网络的情况下,都是由docker0路由的,docker会给我们的容器分配一个默认的可用ip
Docker使用的是Linux桥接,宿主机中是一个Docker容器的网桥 docker0,所有请求都是经过 docker0 的
Docker中所有的网络接口都是虚拟的,虚拟的转发效率比较高(内网传递文件)
只要容器删除,对应的网桥就没了,那一对就没了。
思考一个场景,我们编写了一个微服务,database url=ip:,项目不重启,数据库ip换掉了,我们希望可以处理这个问题,可以通过名字来访问容器?
[root@abc:~]# docker exec -it tomcat01 ping tomcat02
ping: tomcat02: Name or service not known解决方法:两个网络连接就好了
[root@abc:~]# docker exec -it tomcat01 ping tomcat02 ping: tomcat02: Name or service not known # 如何可以解决呢? # 通过--link既可以解决网络连通问题 [root@abc:~]# docker run -d -P --name tomcat03 --link tomcat02 tomcat:7 3eea722652c5d1c6713a105da230bcd373176737b8e246a85b9dbfdadc927b77 [root@abc:~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3eea722652c5 tomcat "catalina.sh run" 11 seconds ago Up 10 seconds 0.0.0.0:49159->8080/tcp, :::49159->8080/tcp tomcat03 85f00f77b182 tomcat:7 "catalina.sh run" 49 minutes ago Up 49 minutes 0.0.0.0:49158->8080/tcp, :::49158->8080/tcp tomcat02 8a6ef428c3eb tomcat:7 "catalina.sh run" 55 minutes ago Up 55 minutes 0.0.0.0:49157->8080/tcp, :::49157->8080/tcp tomcat01 [root@abc:~]# 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.110 ms 64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.060 ms 64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.081 ms # 反向可以ping通吗? [root@abc:~]# docker exec -it tomcat02 ping tomcat03 ping: tomcat03: Name or service not known 说明link是单向的
其实这个tomcat03就是本地配置了tomcat02的配置
本质探究:–link就是我们在hosts配置中增加了一个172.17.0.3 tomcat02 9f6a048802b9
我们现在 玩Docker已经不建议使用–link了!
自定义网络!不适用于docker0!
docker0问题:他不支持容器名连接访问!
# 查看docker所有网络
[root@abc:~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
e0f53dbb0281 bridge bridge local
db8b998a0ea4 host host local
ce321a8c285c none null local
# 查看网络相信信息 docker network inspect docker网络的ID [root@abc:~]# docker network inspect e0f53dbb0281 [ { "Name": "bridge", "Id": "e0f53dbb028144eb8bfb4c3a5bdc45db130c70903a0dbe0139cb7345b9ee03c0", "Created": "2022-05-30T08:29:24.709507436+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.17.0.0/16", "Gateway": "172.17.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "079ed479f01801c0ab3d5221a7a65840cc8fc0265662757505e0e5eee85330aa": { "Name": "tomcat03", "EndpointID": "e6dd165da1823476813a449a41633d02302d54e1fc3fd97d633548bd56afdf68", "MacAddress": "02:42:ac:11:00:04", "IPv4Address": "172.17.0.4/16", "IPv6Address": "" }, "85f00f77b18217a334a1af9d5ba6da0ccf8b8db2883db6b512836f2dd60bf0d4": { "Name": "tomcat02", "EndpointID": "3d7f0f87c69957f4536f7c03bf1ac62f7da2f9538eeb9b7e406188293d6087a5", "MacAddress": "02:42:ac:11:00:03", "IPv4Address": "172.17.0.3/16", "IPv6Address": "" }, "8a6ef428c3eb780c0bcb20dbdff15e50a49cd385082fb3f599e95b883761bf38": { "Name": "tomcat01", "EndpointID": "5c336855a1cbab7ccd15f86331387f2dae079bd2336539df350934ae5b95dbfb", "MacAddress": "02:42:ac:11:00:02", "IPv4Address": "172.17.0.2/16", "IPv6Address": "" } }, "Options": { "com.docker.network.bridge.default_bridge": "true", "com.docker.network.bridge.enable_icc": "true", "com.docker.network.bridge.enable_ip_masquerade": "true", "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", "com.docker.network.bridge.name": "docker0", "com.docker.network.driver.mtu": "1500" }, "Labels": {} } ]
[root@abc:~]# 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 85f00f77b182
172.17.0.4 079ed479f018
本质探究:–link 就是我们在hosts配置中增加了一个172.17.0.3 tomcat02 85f00f77b182
我们现在玩Docker已经不建议使用–link了!,因为太笨了。Docker0的问题:它不支持容器名链接访问!
以后要使用 自定义网络!不使用Docker0!
[root@abc:~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
e0f53dbb0281 bridge bridge local
db8b998a0ea4 host host local
ce321a8c285c none null local
网络模式
bridge: 桥接模式,桥接 docker 默认,自己创建的也是用brdge模式
none: 不配置网络
host: 和宿主机共享网络
container:容器网络连通!(用的少, 局限很大)
# 首先清楚旧的网络,容器 docker rm -f $(docker ps -aq) [root@abc:~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:16:3e:08:30:f7 brd ff:ff:ff:ff:ff:ff inet 172.26.211.8/20 brd 172.26.223.255 scope global dynamic eth0 valid_lft 314315169sec preferred_lft 314315169sec inet6 fe80::216:3eff:fe08:30f7/64 scope link valid_lft forever preferred_lft forever 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:88:a1:23:21 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:88ff:fea1:2321/64 scope link valid_lft forever preferred_lft forever
这时候网络是干净的。
# 我们直接启动的命令默认有一个 --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@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet 26a5afdf4805d7ee0a660b82244929a4226470d99a179355558dca35a2b983ec [root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 30d601788862 bridge bridge local 226019b14d91 host host local 26a5afdf4805 mynet bridge local 7496c014f74b none null local [root@abc:~]# docker network ls NETWORK ID NAME DRIVER SCOPE e0f53dbb0281 bridge bridge local db8b998a0ea4 host host local b08530c7ee8a mynet bridge local # 这个就是上面创建的自定义网络 ce321a8c285c none null local subnet 192.168.0.0/16 可以支持255*255个网络 192.168.0.2 ~ 192.168.255.254 # 查看自己的网络 [root@abc:~]# docker network inspect mynet [ { "Name": "mynet", "Id": "b08530c7ee8a80f4795a9213a4270f5c852dd4e62816c40c3094ab94e88af2fe", "Created": "2022-06-06T11:12:06.231663688+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@abc:~]# docker run -d -P --name tomcat-net-01 --net mynet tomcat:7 3be82f0ce707b91699a8e1077c15fa3a212886f0ad7b78819a53d6ba5e7a96b7 [root@abc:~]# docker run -d -P --name tomcat-net-02 --net mynet tomcat:7 0e378f037a6ded147dec691d750d4b8762cc10a534f3062e59ac140e3927fa7d # 查看自己的network网络情况 [root@abc:~]# docker network inspect mynet [ { "Name": "mynet", "Id": "b08530c7ee8a80f4795a9213a4270f5c852dd4e62816c40c3094ab94e88af2fe", "Created": "2022-06-06T11:12:06.231663688+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": { "0e378f037a6ded147dec691d750d4b8762cc10a534f3062e59ac140e3927fa7d": { "Name": "tomcat-net-02", "EndpointID": "0ba0c9bfa56f8901253c6e2329151ccf8ef036adf216e4b8ab3882bd71a8101c", "MacAddress": "02:42:c0:a8:00:03", "IPv4Address": "192.168.0.3/16", "IPv6Address": "" }, "3be82f0ce707b91699a8e1077c15fa3a212886f0ad7b78819a53d6ba5e7a96b7": { "Name": "tomcat-net-01", "EndpointID": "4de3d3e11cedced52a6bc3a982f4710dd3212a40acd0ac3cd2bdbff0fc334952", "MacAddress": "02:42:c0:a8:00:02", "IPv4Address": "192.168.0.2/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ] ------------------------------------------------------------------------------ 自定义网络是可以自动连接的,*不使用 --link也可以ping名字了! docker exec -it tomcat-net-01 ping tomcat-net-02 docker exec -it tomcat-net-02 ping tomcat-net-01 这两个命令都是可以访问的。
我们自定义的网络docker都已经帮我们维护好了对应的关系,推荐我们平时这样使用网络
好处:
redis - 不同的集群使用不同的网络,保证集群时安全和健康的
mysql - 不同的集群使用不同的网络,保证集群时安全和健康的
docker0 与 mynet是不同的网段,这两个是无法互相访问的,
[root@abc:~]# docker run -d -P --name tomcat01 tomcat:7
39be579ed8f523c9b2adabbf5a41dc84e047d835ccc406a1f7be62523ac0d713
[root@abc:~]# docker run -d -P --name tomcat02 tomcat:7
4fcc99ed702bd7ca5d3601c5a3a64c414d54dbac0dade2532175d0f29f1b7745
[root@abc:~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
4fcc99ed702b tomcat:7 "catalina.sh run" 28 seconds ago Up 27 seconds 0.0.0.0:49165->8080/tcp, ::02
39be579ed8f5 tomcat:7 "catalina.sh run" 32 seconds ago Up 31 seconds 0.0.0.0:49164->8080/tcp, ::01
0e378f037a6d tomcat:7 "catalina.sh run" 11 minutes ago Up 11 minutes 0.0.0.0:49162->8080/tcp, ::-net-02
3be82f0ce707 tomcat:7 "catalina.sh run" 11 minutes ago Up 11 minutes 0.0.0.0:49161->8080/tcp, ::-net-01
[root@abc:~]# docker exec -it tomcat01 ping tomcat-net-01
ping: tomcat-net-01: Name or service not known
两个tomcat之间无法直接连接。为了实现docker01网络中的中的tomcat01可以连接mynet网络中的tomcat-net-01.需要使用网络连通
[root@abc:~]# docker network connect --help Usage: docker network connect [OPTIONS] NETWORK CONTAINER Connect a container to a network Options: --alias strings Add network-scoped alias for the container --driver-opt strings driver options for the network --ip string IPv4 address (e.g., 172.30.100.104) --ip6 string IPv6 address (e.g., 2001:db8::33) --link list Add link to another container --link-local-ip strings Add a link-local address for the container -------------------------------------------------------------------------------------- 测试网络连接: [root@abc:~]# docker network connect mynet tomcat01 (网络 - 容器) [root@abc:~]# docker network inspect mynet [ { "Name": "mynet", "Id": "b08530c7ee8a80f4795a9213a4270f5c852dd4e62816c40c3094ab94e88af2fe", "Created": "2022-06-06T11:12:06.231663688+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": { "0e378f037a6ded147dec691d750d4b8762cc10a534f3062e59ac140e3927fa7d": { "Name": "tomcat-net-02", "EndpointID": "0ba0c9bfa56f8901253c6e2329151ccf8ef036adf216e4b8ab3882bd71a8101c", "MacAddress": "02:42:c0:a8:00:03", "IPv4Address": "192.168.0.3/16", "IPv6Address": "" }, 这个就是上面加入的容器 "39be579ed8f523c9b2adabbf5a41dc84e047d835ccc406a1f7be62523ac0d713": { "Name": "tomcat01", "EndpointID": "21b6673eacf202800746e120fc7cb26ec364006610f4223d548031a4d6c0e24b", "MacAddress": "02:42:c0:a8:00:04", "IPv4Address": "192.168.0.4/16", "IPv6Address": "" }, "3be82f0ce707b91699a8e1077c15fa3a212886f0ad7b78819a53d6ba5e7a96b7": { "Name": "tomcat-net-01", "EndpointID": "4de3d3e11cedced52a6bc3a982f4710dd3212a40acd0ac3cd2bdbff0fc334952", "MacAddress": "02:42:c0:a8:00:02", "IPv4Address": "192.168.0.2/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ] # 再次测试时候可以ping [root@abc:~]# 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.084 ms 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.084 ms 64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.086 ms
就把容器tomcat01 加到了 mynet网络中了。
一个容器两个IP :阿里云服务:公网ip 私网ip
上面是主力机,下面是备用机
如果上面挂了,下面的就要去替代上面的。
[root@abc:~]# docker network create redis --subnet 172.38.0.0/16 3cc89fec59aa31cf59f6728eed12f863de06c52ae4939bba4831146c09d46aba [root@abc:~]# docker network ls NETWORK ID NAME DRIVER SCOPE e0f53dbb0281 bridge bridge local db8b998a0ea4 host host local b08530c7ee8a mynet bridge local ce321a8c285c none null local 3cc89fec59aa redis bridge local 这个就是新建的网络 [root@abc:~]# docker network inspect redis [ { "Name": "redis", "Id": "3cc89fec59aa31cf59f6728eed12f863de06c52ae4939bba4831146c09d46aba", "Created": "2022-06-06T11:48:06.449548045+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@abc:~]# cd /mydata [root@abc:/mydata]# ll total 4 drwxr-xr-x 8 root root 4096 Jun 6 11:51 redis [root@abc:/mydata]# cd redis [root@abc:/mydata/redis]# ll total 24 drwxr-xr-x 3 root root 4096 Jun 6 11:51 node-1 drwxr-xr-x 3 root root 4096 Jun 6 11:51 node-2 drwxr-xr-x 3 root root 4096 Jun 6 11:51 node-3 drwxr-xr-x 3 root root 4096 Jun 6 11:51 node-4 drwxr-xr-x 3 root root 4096 Jun 6 11:51 node-5 drwxr-xr-x 3 root root 4096 Jun 6 11:51 node-6 [root@abc:/mydata/redis]# cd node-1 [root@abc:/mydata/redis/node-1]# ls conf [root@abc:/mydata/redis/node-1]# cd conf [root@abc:/mydata/redis/node-1/conf]# ls redis.conf [root@abc:/mydata/redis/node-1/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 [root@abc:/mydata/redis/node-1/conf]# --------------------------------------------------------------------------------- 上面就是创建的6个redis配置 下面开始运行创建的6个节点 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 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 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 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 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 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 # 创建集群 /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 打印的命令行 >>> 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: ebfbbc88a83fa84c3d73fe048fc37ab82841fa0e 172.38.0.11:6379 slots:[0-5460] (5461 slots) master M: ed7bac9d22ce436c650f9a51c3ae7a3aa84a7537 172.38.0.12:6379 slots:[5461-10922] (5462 slots) master M: 2f1fdb553165c31c599f5cdbac5a42fa15901602 172.38.0.13:6379 slots:[10923-16383] (5461 slots) master S: d2edadd37c2b243ffa3a13ac7bb352062160f4b8 172.38.0.14:6379 replicates 2f1fdb553165c31c599f5cdbac5a42fa15901602 S: 98bec4772bd3cdc7335111f75bc3f6b6ad2fae26 172.38.0.15:6379 replicates ebfbbc88a83fa84c3d73fe048fc37ab82841fa0e S: 6e8dda893836d8182af1a7cbe0ff71b32d0743ef 172.38.0.16:6379 replicates ed7bac9d22ce436c650f9a51c3ae7a3aa84a7537 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: ebfbbc88a83fa84c3d73fe048fc37ab82841fa0e 172.38.0.11:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 98bec4772bd3cdc7335111f75bc3f6b6ad2fae26 172.38.0.15:6379 slots: (0 slots) slave replicates ebfbbc88a83fa84c3d73fe048fc37ab82841fa0e S: d2edadd37c2b243ffa3a13ac7bb352062160f4b8 172.38.0.14:6379 slots: (0 slots) slave replicates 2f1fdb553165c31c599f5cdbac5a42fa15901602 S: 6e8dda893836d8182af1a7cbe0ff71b32d0743ef 172.38.0.16:6379 slots: (0 slots) slave replicates ed7bac9d22ce436c650f9a51c3ae7a3aa84a7537 M: 2f1fdb553165c31c599f5cdbac5a42fa15901602 172.38.0.13:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: ed7bac9d22ce436c650f9a51c3ae7a3aa84a7537 172.38.0.12:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 然后redis就部署完成了。
测试搭建的redis
/data # redis-cli -c 127.0.0.1:6379> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 集群节点数量3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:360 cluster_stats_messages_pong_sent:359 cluster_stats_messages_sent:719 cluster_stats_messages_ping_received:354 cluster_stats_messages_pong_received:360 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:719 127.0.0.1:6379> cluster nodes e3b0f799f788ea426dcf1c19e34c745fb66cae69 172.38.0.16:6379@16379 slave cabf766d1ce0954661eff7e7c02f2f3a7978f2b5 0 1654488329895 6 connected cabf766d1ce0954661eff7e7c02f2f3a7978f2b5 172.38.0.12:6379@16379 master - 0 1654488329595 2 connected 5461-10922 82e39cc9fe0402dd2b221a7730a2ca5a56a65e5e 172.38.0.11:6379@16379 myself,master - 0 1654488328000 1 connected 0-5460 ea39590e677f3ead82b33573dc1704ba29312d01 172.38.0.13:6379@16379 master - 0 1654488329595 3 connected 10923-16383 b30f555ac1dbaed6102d574d3ca3bc6bc00c46a3 172.38.0.14:6379@16379 slave ea39590e677f3ead82b33573dc1704ba29312d01 0 1654488329000 4 connected c6659b50a87703ff83950e78363fdaac4f5ad761 172.38.0.15:6379@16379 slave 82e39cc9fe0402dd2b221a7730a2ca5a56a65e5e 0 1654488328893 5 connected 127.0.0.1:6379>
没学习完,需要redis基础;
10:28
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-q6vkaW9L-1654497083815)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061429565.png)]
# 基础镜像来自java8
FROM java:8
# 拷贝当前路径下所有jar包,到项目的,重命名app.jar
COPY *.jar /app.jar
# 启动名命令
CMD ["--server.port=8080"]
# 暴露端口
EXPOSE 8080
# 执行命令
ENTRYPOINT ["java", "-jar", "/app.jar"]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FeGxs5jf-1654497083815)(https://raw.githubusercontent.com/yxmiaoyu/yx/main/pingo/202206061429091.png)]
[root@abc:/home]# cd idea [root@abc:/home/idea]# ll total 0 [root@abc:/home/idea]# docker build -t boot-220606 . Sending build context to Docker daemon 17.89MB 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 ---> 58501b52a4d8 Step 3/5 : CMD ["--server.port=8080"] ---> Running in 897617ae1528 Removing intermediate container 897617ae1528 ---> b4645def0a6a Step 4/5 : EXPOSE 8080 ---> Running in 67d9573d6cf7 Removing intermediate container 67d9573d6cf7 ---> b8c8c72df946 Step 5/5 : ENTRYPOINT ["java", "-jar", "/app.jar"] ---> Running in e31b0f00eb7e Removing intermediate container e31b0f00eb7e ---> 56ad3a39695f Successfully built 56ad3a39695f Successfully tagged boot-220606:latest -------------------------------------------------------------- [root@abc:/home/idea]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE boot-220606 latest 56ad3a39695f About a minute ago 661MB tomcat latest fb5657adc892 5 months ago 680MB tomcat 7 9dfd74e6bc2f 11 months ago 533MB redis 5.0.9-alpine3.11 3661c84ee9d0 2 years ago 29.8MB java 8 d23bdf5b1b1b 5 years ago 643MB [root@abc:/home/idea]# docker run -d -P --name boot220606 boot-220606 c5eee30cfdc95ebbc29c4aa5d6d426c02d92561778e6c065942d95c6db975cb8 [root@abc:/home/idea]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c5eee30cfdc9 boot-220606 "java -jar /app.jar …" 26 seconds ago Up 25 seconds 0.0.0.0:49167->8080/tcp, :::49167->8080/tcp boot220606 [root@abc:/home/idea]# curl localhost:49167 {"timestamp":"2022-06-06T06:04:57.404+00:00","status":404,"error":"Not Found","path":"/"} [root@abc:/home/idea]# curl localhost:49167/test1 helloMon Jun 06 06:05:08 UTC 2022[root@abc:/home/idea]# curl localhost:49167/test2 定时任务已触发[root@abc:/home/idea]#
以后我们使用了Docker之后,给别人交互的就是一个镜像即可!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。