我的Docker容器没有IP地址.为什么?

前端之家收集整理的这篇文章主要介绍了我的Docker容器没有IP地址.为什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用该命令启动了一个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地址?

码头检查的输出如下

[
    {
        "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,"BlkioDeviceRea@R_404_410@ps": 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","--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,"Aliases": null,"NetworkID": "30b6150968580d33aae0e45f7d850b9e67ae2aa29e1bb837ca4fa74f2b0d4d42","EndpointID": "05e93ec514ee6694e57d344f3e4362252104347c3cc48c607708d125715ed6ec","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. (我在VM上这样做).它是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

当我执行码头工作检查时,我看到:

"IPAddress": "172.17.0.4",

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

原文链接:https://www.f2er.com/docker/436926.html

猜你在找的Docker相关文章