What is the gateway for a container?

I’ve got Docker Desktop 4.25.2 running on my Macbook. After running

docker network create foo
docker run --rm -it --network foo ubuntu:22.04 bash

I ran docker inspect <container_name> in another terminal. Down under "NetworkSettings", I see

"Networks": {
    "foo": {
        "IPAMConfig": null,
        "Links": null,
        "Aliases": [
            "a3166231defa"
        ],
        "NetworkID": "21a369e01a3ba29ad63e8add84ca9a9aa167a3cfcb2dfe7747dfc9f18ed761b3",
        "EndpointID": "95b582ce02a4c15d452d36ede34b3ad3e3291c4e991049a384b296bd8096da4d",
        "Gateway": "192.168.0.1",
        "IPAddress": "192.168.0.2",
        "IPPrefixLen": 20,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "MacAddress": "02:42:c0:a8:00:02",
        "DriverOpts": null
    }
}

I see that the gateway is 192.168.0.1. However, when I run ifconfig | grep inet on my host, I see no such address. My understanding was that the host is always the gateway on Docker networks.

Asked By: Daniel Walker

||

The network you created in docker is a separate, simulated, network. Containers on this network behave like they are connected to each other and there is a simulated router that allows access to the internet. The simulated router has address 192.168.0.1 on this simulated network.

Answered By: user253751
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.