在AWS Linux AMI 2上启用Docker Bridge网络

时间:2019-07-20 15:09:38

标签: amazon-web-services docker

我已经在AWS中创建了一个ec2实例。我已经成功安装了Docker,并且能够使容器成功运行。但是,我无法使两个容器相互通信。具体来说,我试图获取一个Prometheus容器(端口9090)与Alertmanager容器(端口9093)进行通信。

我已经尝试了使用桥接网络模式的标准端口映射,并且尝试了主机网络模式。但无济于事。

在本地主机上的ec2上使两个容器相互通信是否需要特殊的魔术手段?

我的Prometheus配置:

rule_files:
  - 'alert.rules'
scrape_configs:
  - job_name: prometheus
    static_configs:
     - targets:
        - localhost:9090
alerting:
  alertmanagers:
   - static_configs:
      - targets: 
        - localhost:9093

docker ps的输出:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
1c83d903a274        prom/prometheus     "/bin/prometheus --c…"   22 hours ago        Up 22 hours         0.0.0.0:9090->9090/tcp   cranky_banach
d1404c0ee182        prom/alertmanager   "/bin/alertmanager -…"   22 hours ago        Up 22 hours         0.0.0.0:9093->9093/tcp   competent_chatterjee

这是CloudWatch的示例Prometheus日志行:

level=error ts=2019-07-19T20:31:36.001Z caller=notifier.go:528 component=notifier alertmanager=http://localhost:9093/api/v1/alerts count=1 msg="Error sending alert" err="Post http://localhost:9093/api/v1/alerts: dial tcp 127.0.0.1:9093: connect: connection refused"

这是docker inspect bridge的输出:

[
    {
        "Name": "bridge",
        "Id": "8ce9c8be6d071b098930a40873d54c5a6da68bcba91404e360cd0ca4532b365d",
        "Created": "2019-07-19T10:02:37.509745972Z",
        "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": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "1c83d903a2742a95323c626cb2e9a556908e6701790af79d61f8f9cf453acc0d": {
                "Name": "cranky_banach",
                "EndpointID": "23e33b9b0e2f2af24bd6da870bad625fe3d111d92f47a9fdf16d253eba9e3889",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "d1404c0ee18280a80fcfa96e7df3bfa5ff023a58f70b3bac67c23b31de7994df": {
                "Name": "competent_chatterjee",
                "EndpointID": "49b38cbf678abd3942f1d8b2a65f8b353b9e8e228cc84a9311cd1ac6549ddc96",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "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": {}
    }
]

2 个答案:

答案 0 :(得分:0)

寻找容器到容器网络时,需要使用主机网络模式。桥接网络(除非您使用用户定义的桥接模式)不会在Docker守护程序中将容器彼此公开。这是安全和隔离模式。

https://docs.docker.com/network/bridge/

答案 1 :(得分:0)

它应该使用容器的主机网络或另一个容器的网络。

假设prom/alertmanager已在运行,请将这些选项之一添加到您的prometheus docker run命令中。

--network host

--network container:$(docker ps | grep "prom/alertmanager" | cut -d" " -f1) 

$(docker ps | grep "prom/alertmanager" | cut -d" " -f1)将返回prom/alertmanager容器ID。

更多信息:https://docs.docker.com/engine/reference/run/#network-settings