定制docker桥接网络

时间:2018-04-04 09:14:34

标签: docker gitlab docker-container docker-networking docker-network

我希望我的docker0和所有容器具有相同的网关地址,或者与我的本地计算机位于相同的IP范围内。我开始在ther daemon.json文件/etc/docker/daemon.json

中定义一个fixed-cidr
{
  "bip": "10.80.44.248/24",
  "fixed-cidr": "10.80.44.250/25",
  "mtu": 1500,
  "default-gateway": "10.80.44.254",
  "dns": ["10.80.41.14"]
}

似乎正在查看ip -a的输出 看来docker0从未收到过任何数据。

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet *10.80.44.248*  netmask 255.255.255.0  broadcast *10.80.44.255*
        ether 02:42:9c:b9:e1:63  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet *10.80.44.39*  netmask 255.255.255.0  broadcast *10.80.44.255*
        inet6 fe80::250:56ff:feb1:79e4  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:b1:79:e4  txqueuelen 1000  (Ethernet)
        RX packets 211061  bytes 30426474 (29.0 MiB)
        RX errors 0  dropped 33861  overruns 0  frame 0
        TX packets 3032  bytes 260143 (254.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

本地计算机和docker0在相同的IP范围内具有相同的网关。好。 但是当我启动docker容器并检查桥接设置时,一切都不同了。这是

的输出
docker network inspect bridge


[
    {
        "Name": "bridge",
        "Id": "b326a37a589245449e1268bbb9ee65262eb7986574c0e972c56d350aa82d7238",
        "Created": "2018-04-04T03:25:52.00544539+02:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "10.80.44.248/24",
                    "IPRange": "10.80.44.128/25",
                    "Gateway": "10.80.44.248",
                    "AuxiliaryAddresses": {
                        "DefaultGatewayIPv4": "10.80.44.254"
                    }
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": 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": {}
    }
]

我不明白为什么现在IPAM配置有IPv4 als辅助

&#34; AuxiliaryAddresses&#34;:{                         &#34; DefaultGatewayIPv4&#34;:&#34; 10.80.44.254&#34;                     }

我意识到现在桥不是由daemon配置的同一子网创建的,我创建了2个具有不同IP范围的不同网桥。这仍然是docker的默认设置。

  

docker network ls

NETWORK ID          NAME                   DRIVER              SCOPE
b326a37a5892        bridge                 bridge              local
6ce11066cdea        dockergitlab_default   bridge              local
d5a36c04b809        host                   host                local
15f66b88ee67        none                   null                local
  

docker network inspect dockergitlab_default

[
    {
        "Name": "dockergitlab_default",
        "Id": "6ce11066cdeabf3cfe65b2dff22046bd1e9c18d2588f47b9cd3c52ea24f7a636",
        "Created": "2018-03-14T08:56:23.351051727+01:00",
        "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,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "60f769c91cff1de47794a7c8b587b778488883da094ae32cfde5196ee0f528f1": {
                "Name": "gitlab-runner",
                "EndpointID": "5122fe862537fb8434a484b4797153274b945e20bc3c7223efc6fd0bd55eae14",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
            "9c46e1fde6390142bddf67270cfeda7b3e68b1a6e68cabc334046db687240a8d": {
                "Name": "dockergitlab_postgresql_1",
                "EndpointID": "8488b32cc34a2c92308528de74b5eddcecac12a402ee6e67c1ef0f2750b72721",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "eaf29f5f405cbf9bdd918efad26ceae1a8c3f58f4bef0aa8fd86b4631bcfdf43": {
                "Name": "dockergitlab_gitlab_1",
                "EndpointID": "d7f78ee9bd51dd13826d7834470d03a9084fc7ab8c6567c0181acecc221628c6",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "f460687ec00eff214fa08adfe9a0af5b85c392ceb470c4ed630ef7ecb0bfcba1": {
                "Name": "dockergitlab_redis_1",
                "EndpointID": "8b18906f1c79a5faaadd32afdef20473f9b635e9a1cd2c7108dd98df48eaed86",
                "MacAddress": "02:42:ac:11:00:05",
                "IPv4Address": "172.17.0.5/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "dockergitlab"
        }
    }
]

我不知道为什么现在使用旧的默认IP地址创建了docker bridge。

  

本地系统详情

我可以在本地机器上执行apt update但是当我登录gitlab-runner时我无法apt update

Linux  4.9.0-6-amd64 #1 SMP Debian 4.9.82-1+deb9u3 (2018-03-02) x86_64

Docker version 17.12.0-ce, build c97c6d6

docker-compose version 1.18.0, build 8dd22a9

有没有办法可以设置桥接设置。根据我的阅读,当我在daemon.json文件中定义/配置cdir和gateway时,将从那里获取所有内容以创建桥接网络和所有其他容器。

先谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

首先,您已正确配置docker0网桥,并使用普通docker run命令启动容器,应将它们连接到网桥并在10.80.44.250/25中为其提供IP。

根据您粘贴的内容,我猜您正在使用docker-compose来启动容器。

  如果您未指定任何内容,

docker-compose将为每个docker-compose.yml创建一个myproject_default网络。

今天您无法选择在哪个池中选择IP范围,默认情况下为172.[17-31].0.0/16。目前有一个有效的拉取请求,允许覆盖此行为:https://github.com/moby/moby/pull/36396

如果您想在docker-compose.yml中手动指定IP范围,可以写下:

networks:
  default:
    ipam:
      config:
        - subnet: 10.80.44.250/25

编辑:这只与docker-compose语法&gt; = 3.0兼容。