我的Docker容器没有IP地址。为什么? [英] My Docker container does not have IP address. Why?

查看:570
本文介绍了我的Docker容器没有IP地址。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用命令启动了 rethinkDB Docker容器

I started a rethinkDB docker container using the command

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

Now the container started successfully. I did 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地址。所以我做了

Now I want to find out the IP address of this container. So I did

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

它什么也没回报。我找不到此容器的IP地址。

It's returning me nothing. I couldn't find IP address for this container.

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

I checked if the rethinkDB configuration page is up or not by going to http://localhost:8080 and I see it's up and running.

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

Why doesn't this container have any IP address?

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 选项启动容器,这意味着您的容器将在以下位置运行您的主机网络。在此处中查看更多信息。因此,您的容器将可以在主机的IP上访问。

You are starting your container with the --net host option which means your container will run on your host network. Check here for more info. So your container will be accessible on the IP of your host.

一个非常基本的示例:

$ docker run --net=host -d jenkins

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

This will run jenkins on your real host network. So when you open the firewall (jenkins runs on 8080) you will access your container immediatly. So I open the firewall:

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

我用<$ c $检查eth0(主机网络)的真实IP c> ifconfig 。 (我正在VM上执行此操作)。它是inet 192.168.140.7 。我在浏览器中转到192.168.140.7:8080,然后看到了詹金斯人。因此,您的容器没有自己的IP。它运行在您的主机网络和主机IP上。

I check the real IP of my eth0 (host network) with ifconfig. (I'm doing this on a VM). It is inet 192.168.140.7. I go in my browser to 192.168.140.7:8080 and I see the jenkins. So your container has no own IP. It's running on your host network and host IP.

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

If you leave the --net host option, the container will be created in the default docker bridge network with range "172.17.0.0/16". Now your container will get an IP from in the range of that network. To access the container from the outside you have to map your ports on your host network (see the more info). You can do this with the -p option.

因此,对于詹金斯人示例:

So for the jenkins example:

$ docker run -d -p 8888:8080 jenkins

当我执行 docker检查时,我看到:

"IPAddress": "172.17.0.4",

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

So my container is running in the bridge network on 172.17.0.4:8080. Now with the -p option I'm mapping the port 8080 of my bridge network on 8888 of my host network: so host-IP:8888. Now it's accessible from the outside.

这篇关于我的Docker容器没有IP地址。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆