[Docker] 創建容器時指定gateway與subnet

可參考之前的[Docker] 創建和使用 bridge

$ docker network create --help

Usage:  docker network create [OPTIONS] NETWORK

Create a network

      --attachable           Enable manual container attachment
      --aux-address map      Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
      --config-from string   The network from which to copy the configuration
      --config-only          Create a configuration only network
  -d, --driver string        Driver to manage the Network (default "bridge")
      --gateway strings      IPv4 or IPv6 Gateway for the master subnet
      --ingress              Create swarm routing-mesh network
      --internal             Restrict external access to the network
      --ip-range strings     Allocate container ip from a sub-range
      --ipam-driver string   IP Address Management Driver (default "default")
      --ipam-opt map         Set IPAM driver specific options (default map[])
      --ipv6                 Enable IPv6 networking
      --label list           Set metadata on a network
  -o, --opt map              Set driver specific options (default map[])
      --scope string         Control the network's scope
      --subnet strings       Subnet in CIDR format that represents a network segment
docker network create -d bridge --gateway --subnet demo


$ docker network inspect demo
        "Name": "demo",
        "Id": "9a5ccdbfa5b5e9968cedb3a9b66b5714e35eade805582538bde47c187e593df5",
        "Created": "2022-01-15T18:42:53.457025805Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                    "Subnet": "",
                    "Gateway": ""
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}


docker container run -d --rm --name box4 --network demo busybox /bin/sh -c "while true; do sleep 3600; done"


docker network connect demo box3
docker exec -it box4 ping box3



docker exec -it box3 ping box4