Moule Lin
← All notes

Docker Network

Once Docker is installed, there are three networks by default (docker network ls):

C:\Users\zsl-pc>docker network ls
NETWORK ID NAME DRIVER SCOPE
992344cd89fe bridge bridge local
06445f4f5774 host host local
2d964ece79b9 none null local

bridge: the default network for containers; once configured, it can communicate with the host and thereby reach the Internet.

host and none: these count as having no network (check with docker network inspect host: there is no subnet or gateway). Adding a container to either of them attaches it to a specific container network stack, and it cannot communicate with external networks.

Whenever you start a container with docker run, it gets a default network, namely bridge.

You can use the inspect command to see the containers in each network (here, the bridge network):

C:\Users\zsl-pc>docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "992344cd89fe90aa240651e7497131e5dc40539d3f1ad540f973feab6b345b3c"
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Containers": {},
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

Newly created containers are placed on the bridge network by default, so they can communicate with each other via their IPs.

You can create a custom network, attach the containers that need to communicate to it, and access them by alias; IPs also work, but they are hard to remember
- 1. Create a network named test-net:
docker network create test-net

- 2. Run Redis on the test-net network with the alias redis
docker run -d --name redis --network test-net --network-alias redis redis:latest

- 3. Run the web project on the same network
docker run -p 8080:8080 --name test -v D:/test:/app --network test-net -d test:v1