gpt4 book ai didi

docker - 可以通过桥 IP 连接到 docker 容器,但不能通过 pod 容器内的 0.0.0.0 连接

转载 作者:行者123 更新时间:2023-12-02 12:10:58 24 4
gpt4 key购买 nike

我正在运行 Jenkins,它在运行 maven 构建的 pod 中运行容器。
在构建中,测试启动了 mysql 容器并获得“连接被拒绝”。在本地,在 Jenkins 之外,它运行良好。
容器通过以下方式运行:

docker run --name mysql_unit_test -e MYSQL_ROOT_PASSWORD=abcd1234! -e MYSQL_DATABASE=db -p 4306:3306 -d mysql:5.7.13
docker ps产生:
816a19116f2d  mysql:5.7.13  "docker-entrypoint.s…"  17 hours ago  Up 17 hours  0.0.0.0:4306->3306/tcp  mysql_unit_test
docker inspect :
[
{
"Id": "816a19116f2dd947013baa2abe5555317afec448fa8f56258fce4dce53f33a09",
"Created": "2020-07-22T04:43:36.556743179Z",
"Path": "docker-entrypoint.sh",
"Args": [
"mysqld"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 84461,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-07-22T04:43:37.219671572Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:1195b21c3a45d9bf93aae497f2538f89a09aaded18d6648753aa3ce76670f41d",
"ResolvConfPath": "/var/lib/docker/containers/816a19116f2dd947013baa2abe5555317afec448fa8f56258fce4dce53f33a09/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/816a19116f2dd947013baa2abe5555317afec448fa8f56258fce4dce53f33a09/hostname",
"HostsPath": "/var/lib/docker/containers/816a19116f2dd947013baa2abe5555317afec448fa8f56258fce4dce53f33a09/hosts",
"LogPath": "/var/lib/docker/containers/816a19116f2dd947013baa2abe5555317afec448fa8f56258fce4dce53f33a09/816a19116f2dd947013baa2abe5555317afec448fa8f56258fce4dce53f33a09-json.log",
"Name": "/mysql_unit_test",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {
"max-file": "5",
"max-size": "50m"
}
},
"NetworkMode": "default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "",
"HostPort": "4306"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"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/ccba4cc84fd186833c2bb0a6976a356bf1167cc834b06f4165984367b8ca69ef-init/diff:/var/lib/docker/overlay2/cf95f3dcd155ff4d3723bb3283e50af755a9a48c1febef3b1889ace131b9242d/diff:/var/lib/docker/overlay2/58a035059ace166a43c538e8bd48d80ad80af4bf5c43a3ff1da6b31490fd6380/diff:/var/lib/docker/overlay2/045b842ce5119b1ec1aa1da2e93a54d69df9d16cdbc531746089a4e881bff44a/diff:/var/lib/docker/overlay2/ed9de3c8ec5891ee8619e4cca70ea041b112650b3fce543840e25ce5acf038f7/diff:/var/lib/docker/overlay2/cf3af22c1d68cd4b42f97896d5b55f9433972354f67b48499fce6fd7a9c8b2da/diff:/var/lib/docker/overlay2/079d799ee0d806d8428533c7ca65347bc579d795e47b5fb8d9f0491d11658261/diff:/var/lib/docker/overlay2/ba3cd6723b68d9cde09712cad0dd6f6a3cd3989e5f69872a35d60ea5ab76a12d/diff:/var/lib/docker/overlay2/0c9c8a78c4f329888e1fb40aba0fc688f1090c84a6823aa443d08445f2c9ba5f/diff:/var/lib/docker/overlay2/12cac8cdd33fabd38487d14a1599516f14bd79421f5a9a04cf926ca8d2c5bc0f/diff:/var/lib/docker/overlay2/f844165a3a8f81c0d5703f0e27b6cb3ace6c6559b256f1b4dcf91158e0f5a384/diff:/var/lib/docker/overlay2/34c41e90c5210a0aec026543d85d5c17e01287a087607b79c74db82ebff94ee5/diff",
"MergedDir": "/var/lib/docker/overlay2/ccba4cc84fd186833c2bb0a6976a356bf1167cc834b06f4165984367b8ca69ef/merged",
"UpperDir": "/var/lib/docker/overlay2/ccba4cc84fd186833c2bb0a6976a356bf1167cc834b06f4165984367b8ca69ef/diff",
"WorkDir": "/var/lib/docker/overlay2/ccba4cc84fd186833c2bb0a6976a356bf1167cc834b06f4165984367b8ca69ef/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "571074f25fbd2d154dcf2b563af0a3fd42c1d5987c561f4fb23ba37c785a49d1",
"Source": "/var/lib/docker/volumes/571074f25fbd2d154dcf2b563af0a3fd42c1d5987c561f4fb23ba37c785a49d1/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "816a19116f2d",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_ROOT_PASSWORD=abcd1234!",
"MYSQL_DATABASE=db",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.7",
"MYSQL_MAJOR=5.7",
"MYSQL_VERSION=5.7.13-1debian8"
],
"Cmd": [
"mysqld"
],
"Image": "mysql:5.7.13",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "99fb1d64dbd98500d7dddae88ea2b53e712da9a709f8541fcae413f52d18a309",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "4306"
}
]
},
"SandboxKey": "/var/run/docker/netns/99fb1d64dbd9",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "375f5604e67cd5282d09585e2b211df7d8c1482b448f95003b38eaf5755c3982",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "526388f448225716c3159ae806600b8507086e898f41d3945d24d8c4572e3848",
"EndpointID": "375f5604e67cd5282d09585e2b211df7d8c1482b448f95003b38eaf5755c3982",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
telnet :
telnet 0.0.0.0 4306
Trying 0.0.0.0...
telnet: Unable to connect to remote host: Connection refused
telnet 172.17.0.1 4306
Trying 172.17.0.1...
Connected to 172.17.0.1.
默认情况下,测试使用 jdbc:mysql://0.0.0.0:4306/db ,但是如果我将其更改为 jdbc:mysql://172.17.0.1:4306/db它能够连接。
我尝试使用 postgres而不是 mysql并有同样的问题。
我的问题是为什么它无法连接到 0.0.0.0 ?是因为它是容器中容器中的容器吗?我是这项技术的新手。

最佳答案

0.0.0.0甚至不是 address 🤷,所以我不希望它起作用。它通常用于服务器/服务以绑定(bind)到 any IP 地址(或含义不适用)但不是由尝试连接到服务器/服务的客户端。如果您真的在本地连接,您可以使用 localhost (DNS 规范)或只是 127.0.0.1 .您没有在测试中指定连接字符串,但它可能只是连接到本地套接字 /var/lib/mysql/mysql.sock而不是 IP 地址 (❓)172.17.0.1是本地容器地址,因此只要您公开端口,它就始终可以工作,但问题是您的容器可能并不总是具有相同的地址,具体取决于您实例化容器的特定机器上运行的内容。
✌️

关于docker - 可以通过桥 IP 连接到 docker 容器,但不能通过 pod 容器内的 0.0.0.0 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63044070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com