0

I ran a Docker image. Both the container and host ports are mapped to 5000, and it can be accessed using localhost but not with the IP address. I don't have any knowledge on network systems and I'm a beginner in Docker.

I'm using Docker app for Windows, and upon docker inspect, I got this JSON:

[
    {
        "Id": "c38f489d712502ce57e5f6b86d8ad1efd3cf64d3c60ab3239bf34a727d0155b7",
        "Created": "2020-08-19T18:17:50.917578Z",
        "Path": "java",
        "Args": [
            "-jar",
            "/app.jar"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 3142,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-08-19T18:17:53.0308405Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:04d86c26964a44e147a0132bc6f6810f5aeba005d655b0d0b86f04499fc6a7a1",
        "ResolvConfPath": "/var/lib/docker/containers/c38f489d712502ce57e5f6b86d8ad1efd3cf64d3c60ab3239bf34a727d0155b7/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/c38f489d712502ce57e5f6b86d8ad1efd3cf64d3c60ab3239bf34a727d0155b7/hostname",
        "HostsPath": "/var/lib/docker/containers/c38f489d712502ce57e5f6b86d8ad1efd3cf64d3c60ab3239bf34a727d0155b7/hosts",
        "LogPath": "/var/lib/docker/containers/c38f489d712502ce57e5f6b86d8ad1efd3cf64d3c60ab3239bf34a727d0155b7/c38f489d712502ce57e5f6b86d8ad1efd3cf64d3c60ab3239bf34a727d0155b7-json.log",
        "Name": "/blissful_noether",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "5000/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "5000"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                44,
                168
            ],
            "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,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/asound",
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/49e30cda4983fa70169660522eb0264adb6a86ba7714f051ceb7fe9ba2e6f921-init/diff:/var/lib/docker/overlay2/52cc6d89b46870d8613801fc85cb6f9fa38c0b37b07857de839acd4d001b5530/diff:/var/lib/docker/overlay2/e058a1965c0dff7fbaf1f2fbbe9dbbd491f273d3389c093a3aff1a2c928e305e/diff:/var/lib/docker/overlay2/ea09144ed7206c329c5dac727f9ddd3f346260a59b9e595916f17ee11c3b6d97/diff:/var/lib/docker/overlay2/53820238bb96c5ce804cc77ecbad212bcd3bf6d6a1ca5fdcc3f4d6f4e2349007/diff",
                "MergedDir": "/var/lib/docker/overlay2/49e30cda4983fa70169660522eb0264adb6a86ba7714f051ceb7fe9ba2e6f921/merged",
                "UpperDir": "/var/lib/docker/overlay2/49e30cda4983fa70169660522eb0264adb6a86ba7714f051ceb7fe9ba2e6f921/diff",
                "WorkDir": "/var/lib/docker/overlay2/49e30cda4983fa70169660522eb0264adb6a86ba7714f051ceb7fe9ba2e6f921/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "df1fe5e69c6437bd3e43d737b5cf7b37f9c1c5325b5505964579fd093ed4beae",
                "Source": "/var/lib/docker/volumes/df1fe5e69c6437bd3e43d737b5cf7b37f9c1c5325b5505964579fd093ed4beae/_data",
                "Destination": "/tmp",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "c38f489d7125",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "5000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin",
                "LANG=C.UTF-8",
                "JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk",
                "JAVA_VERSION=8u212",
                "JAVA_ALPINE_VERSION=8.212.04-r0"
            ],
            "Cmd": null,
            "ArgsEscaped": true,
            "Image": "04d86c26964a",
            "Volumes": {
                "/tmp": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "java",
                "-jar",
                "/app.jar"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "fb790b4e2c72fc9b2c9641e1eb91aca5e74759b2ee0265ce5a1907ddb3eb1865",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "5000"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/fb790b4e2c72",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "0f084ea8ca77ec70d850291644523b81597b761dabe687b4e0035b4ae2e3dd56",
            "Gateway": "xxx.xx.x.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "xxx.xx.x.x",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:bc:19:10:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "51b3be5f2161bb70042d5efd31f5b611cea241248a142f46fa612352c",
                    "EndpointID": "0f084ea8ca77ec70d850291644523b81597b761dabe687b45b4ae2e3dd56",
                    "Gateway": "xxx.xx.x.1",
                    "IPAddress": "xxx.xx.x.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:bc:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

[![ipconfig][1]][1]

I even just came to know that even a Docker container consists of IP. Why is it so? Can anyone help me understand how the container can be accessed with IP?

I have tried with all the IPs available in the JSON above and ipconfig image, but it does not work.

  • @NeoAnderson-Thank you, docker run -p5000:5000 - works with localhost but not with ip –  Aug 20 '20 at 06:15
  • if you are running Docker on Linux, this should work by default. If it is not, then you might need to add the route to the 172.17.0.0 network. If it is Windows, then the same principle applies: you need to make sure your request is routed to the container network, but things are more complicated because there’s a Linux VM in between – Neo Anderson Aug 20 '20 at 14:52
  • Assume it is deployed on a linux environment of the same network and i need to access from windows machine? is that possible ? and How can i add routes to the containers of a network ? –  Aug 20 '20 at 19:31
  • Assume you have a computer that has a private IP address(say 192.168.0.2). Can you access your computer from the Internet using that IP address? I hope my question answers your question. – Neo Anderson Aug 20 '20 at 19:56

1 Answers1

0

Yes, Docker containers have their Unique IP address(more in Kubernetes).

https://stackoverflow.com/a/43694284/9650284

refer this for an explanation(comments on the solutions). TLDR it is not possible to access the internal IP of the docker container from your host.

Sahil Yadav
  • 88
  • 1
  • 9
  • Have tried with this ip as well but it did not work and browser works at port 8000 means ? did not provide any configuration for port 8080. –  Aug 20 '20 at 06:16
  • Yes sorry for the previous response, I tried what you asked using an Nginx image. The internal IP was not accessible. After a little digging on the internet, I came across this question, hope this question will shed some light on your question. – Sahil Yadav Aug 21 '20 at 02:43