gpt4 book ai didi

docker - 我的 Docker 容器没有 IP 地址。为什么?

转载 作者:IT老高 更新时间:2023-10-28 12:44:10 24 4
gpt4 key购买 nike

我使用命令启动了一个 rethinkDB docker 容器

docker pull rethinkdb:2.3
docker run --rm --name rethinkdb -v /srv/rethinkdb:/data --net host rethinkdb:2.3 rethinkdb --bind all --cache-size 8192 --no-update-check

现在容器已成功启动。我做了 docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
f288961ef376 rethinkdb:2.3 "rethinkdb --bind ..." 9 minutes ago Up 9 minutes rethinkdb
1f71722698ae sorccu/adb:latest "/sbin/tini -- adb..." 14 minutes ago Up 14 minutes adbd

现在我想找出这个容器的 IP 地址。所以我做到了

docker inspect --format '{{ .NetworkSettings.IPAddress }}' f288961ef376

它什么也没给我。我找不到这个容器的 IP 地址。

我通过转到 http://localhost:8080 检查了 rethinkDB 配置页面是否已启动,我看到它已启动并正在运行。

为什么这个容器没有 IP 地址?

docker inspect的输出如下

[
{
"Id": "f288961ef376531c97d2264cb8ef3c6077a6a75107905d6a47734303adfcb117",
"Created": "2017-05-05T11:13:45.382460184Z",
"Path": "rethinkdb",
"Args": [
"--bind",
"all",
"--cache-size",
"8192",
"--no-update-check"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 8157,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-05-05T11:13:45.774035358Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:c5ed876750b40cde4725ea9eb9d8503f4d1419a2f23ac2ef8e4cc1d535e2c3a2",
"ResolvConfPath": "/var/lib/docker/containers/f288961ef376531c97d2264cb8ef3c6077a6a75107905d6a47734303adfcb117/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/f288961ef376531c97d2264cb8ef3c6077a6a75107905d6a47734303adfcb117/hostname",
"HostsPath": "/var/lib/docker/containers/f288961ef376531c97d2264cb8ef3c6077a6a75107905d6a47734303adfcb117/hosts",
"LogPath": "/var/lib/docker/containers/f288961ef376531c97d2264cb8ef3c6077a6a75107905d6a47734303adfcb117/f288961ef376531c97d2264cb8ef3c6077a6a75107905d6a47734303adfcb117-json.log",
"Name": "/rethinkdb",
"RestartCount": 0,
"Driver": "aufs",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/srv/rethinkdb:/data"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "host",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": true,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"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": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": null,
"Name": "aufs"
},
"Mounts": [
{
"Type": "bind",
"Source": "/srv/rethinkdb",
"Destination": "/data",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "dev-machine",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": true,
"AttachStderr": true,
"ExposedPorts": {
"28015/tcp": {},
"29015/tcp": {},
"8080/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"RETHINKDB_PACKAGE_VERSION=2.3.5~0jessie"
],
"Cmd": [
"rethinkdb",
"--bind",
"all",
"--cache-size",
"8192",
"--no-update-check"
],
"Image": "rethinkdb:2.3",
"Volumes": {
"/data": {}
},
"WorkingDir": "/data",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "bd17bf8e1663ff18f6674a1a3a1665c4e1bf65283d358ffc97dc238ef4a79088",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "30b6150968580d33aae0e45f7d850b9e67ae2aa29e1bb837ca4fa74f2b0d4d42",
"EndpointID": "05e93ec514ee6694e57d344f3e4362252104347c3cc48c607708d125715ed6ec",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": ""
}
}
}
}
]

最佳答案

您正在使用 --net host 选项启动您的容器,这意味着您的容器将在您的主机网络上运行。查看 here了解更多信息。因此,您的容器将可以在您的主机的 IP 上访问。

一个非常基本的例子:

$ docker run --net=host -d jenkins

这将在您的真实主机网络上运行 jenkins。因此,当您打开防火墙(jenkins 在 8080 上运行)时,您将立即访问您的容器。于是我打开了防火墙:

$ sudo iptables -I INPUT 5 -p tcp -m tcp --dport 8080 -j ACCEPT

我使用 ifconfig 检查我的 eth0(主机网络)的真实 IP。 (我在虚拟机上做这个)。它是 inet 192.168.140.7。我在浏览器中进入 192.168.140.7:8080 并看到 Jenkins 。所以你的容器没有自己的IP。它在您的主机网络和主机 IP 上运行。

如果您保留 --net host 选项,容器将在默认的 docker bridge 网络中创建,范围为 "172.17.0.0/16"。现在您的容器将从该网络范围内获得一个 IP。要从外部访问容器,您必须在主机网络上映射端口(请参阅更多信息)。您可以使用 -p 选项来做到这一点。

所以对于 Jenkins 的例子:

$ docker run -d -p 8888:8080 jenkins

当我执行 docker inspect 我看到:

"IPAddress": "172.17.0.4",

所以我的容器在 172.17.0.4:8080 的桥接网络中运行。现在使用 -p 选项,我将桥接网络的端口 8080 映射到主机网络的 8888 上:所以 host-IP:8888。现在它可以从外部访问了。

关于docker - 我的 Docker 容器没有 IP 地址。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43803849/

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