dockerdocker-composeipport

I set in docker-compose "127.0.0.101:80:80" and why gateway and IP address I get from another subnet


**i set in docker-compose "127.0.0.101:80:80"

and why gataway and ip adres i get from another subnet**

"Gateway": "172.31.0.1", "IPAddress": "172.31.0.2",

i create docker container with docker-file:

version: '3.6'

services:
    web:
        image: nginx
        hostname: project1
        ports:
            - "127.0.0.101:80:80"
        networks:
            some_network:
                aliases:
                    - project1.local
                    - www.project1.local

networks:
    some_network:
        # Общая сеть со смежными проектами
        name: 'our_network'

**make **

**docker container inspect 50631755223c**

im get

        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "53e14814f2bd19036439e5327a91416784920841435195d362c751d9995de5b5",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        **"HostIp": "127.0.0.101",**
                        "HostPort": "80"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/53e14814f2bd",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "our_network": {
                    "IPAMConfig": {},
                    "Links": null,
                    "Aliases": [
                        "pr1-web-1",
                        "web",
                        "project1.local",
                        "www.project1.local",
                        "50631755223c",
                        "project1"
                    ],
                    "NetworkID": "9610c4d18d33068427f3eb4d5c3ecce87da5afc9eef59f610757f10a98156915",
                    "EndpointID": "1d60133501de2edc51f45db05ba0727ced4d2a102d63a8e26a3d9a2f93567d04",
                    "Gateway": **"172.31.0.1",**
                    "IPAddress": **"172.31.0.2",**
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:1f:00:02",
                    "DriverOpts": null

**i set in docker-compose "127.0.0.101:80:80"

and why gataway and ip adres i get from another subnet**

"Gateway": "172.31.0.1", "IPAddress": "172.31.0.2",

I was expecting the gataway to be from the same subnet as I'm asking.


Solution

  • docker inspect dumps out very low-level diagnostic information about your container. You almost never need any of the information there. More specifically, the IP-related information it prints out is all but useless.

    There are two layers of Docker networking. Each container internally does happen to have its own IP address, and there is an IP network corresponding to the Docker network (in your example, the manually-configured some_network network). Docker's assigned that network the IPv4 address range 172.31.0.0/16. Again, this is an implementation detail you don't need to know about.

    Separately from this, you've requested to publish ports: from the container to the host. ports: ['127.0.0.101:80:80'] says, in order, that on the host interface bound to the address 127.0.0.101, forward port 80 to port 80 inside the container. This is visible in the docker inspect output (under ports:), but that IP address is the host's IP address and not part of the Docker-internal networking space.

    To reiterate, in practice none of this matters. I'd use the normal localhost IP address 127.0.0.1 as the bind address if you want a container port to be accessible from outside a container on the same host but not from other hosts, and I'd use the default network Compose provides for you. I'd reduce the Compose setup to just

    version: '3.8'
    services:
        web:
            image: nginx
            ports:
                - "127.0.0.1:80:80"
    

    (removing all of the networks: blocks, the hostname: setting, and fixing the host IP address in ports:)

    and ignore the docker inspect output entirely.