Docker containers "bad gateway" 使用容器名称作为 DNS 名称时出错(使用 ip 有效)
Docker containers "bad gateway" error using container name as DNS name (with ip it works)
摘要:我可以使用(自定义)docker 网络将一个 docker 容器连接到另一个容器(在 docker-compose 文件中),只有IP地址。使用容器名称作为 DNS 会导致“错误的网关”错误。
我正在尝试让 Grafana(作为 docker-compose)与 Prometheus(作为 docker 容器)一起工作。我将 prometheus 容器连接到 Grafana docker compose 定义的网络。这样,只要我使用它的 IP 地址,我就可以将 prometheus 服务器用作 Grafana 的数据源。如何使用 DNS 名称使其工作?使用容器名称作为 DNS 名称会导致“错误的网关”错误。
docker-compose.yml
version: '3.4'
networks:
k6:
grafana:
services:
influxdb:
image: influxdb:1.8
networks:
- k6
- grafana
ports:
- "8086:8086"
environment:
- INFLUXDB_DB=k6
grafana:
image: grafana/grafana:latest
networks:
- grafana
ports:
- "3000:3000"
environment:
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_BASIC_ENABLED=false
volumes:
- ./grafana:/etc/grafana/provisioning/
k6:
image: loadimpact/k6:latest
networks:
- k6
ports:
- "6565:6565"
environment:
- K6_OUT=influxdb=http://influxdb:8086/k6
volumes:
- ./samples:/scripts
普罗米修斯容器命令:
docker run -d -p 9090:9090 --network k6_grafana -v C:\xy\QA\K6\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
docker网络检查k6_grafana
[
{
"Name": "k6_grafana",
"Id": "15de38f97d93a0c697de4bfdc4fe0f441bcacde9fd43f5b9d1608908bb26c954",
"Created": "2021-10-26T09:45:17.6385164Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.27.0.0/16",
"Gateway": "172.27.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"37e0a02a2c7f73b2be78623a4d239b86eb2e2a1a14e8b3416d472de2a719d701": {
"Name": "k6_grafana_1",
"EndpointID": "f0bf0f4844775c3fa6ce6758e2e4413ff3de40e5d6b93158e5668ebe4661f6a5",
"MacAddress": "02:42:ac:1b:00:02",
"IPv4Address": "172.27.0.2/16",
"IPv6Address": ""
},
"6efce075e766ce581e134947ef2067d0b3ae433476c850e02768b624c672da0f": {
"Name": "practical_lalande",
"EndpointID": "363c42b1c5194e95d4469f7c474973ba8b21f8fe2573fb22f3fac76c5ab8a1af",
"MacAddress": "02:42:ac:1b:00:04",
"IPv4Address": "172.27.0.4/16",
"IPv6Address": ""
},
"d075b1aa33452c38e0340e4a10997be3665377a43a961e99ecab580af22dfe02": {
"Name": "k6_influxdb_1",
"EndpointID": "4716f96665f4f93acd3cc087d1cad1048ee035062d950fddfe9d3e176a34bcbf",
"MacAddress": "02:42:ac:1b:00:03",
"IPv4Address": "172.27.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "grafana",
"com.docker.compose.project": "k6",
"com.docker.compose.version": "1.28.5"
}
}
]
所以用http://172.27.0.4:9090连接就可以了!但是使用 http://practical_lalande:9090 连接(或任何其他名称)是行不通的!
根据 docker 文档,这应该有效:
User-defined bridges provide automatic DNS resolution between
containers.
Containers on the default bridge network can only access each other by
IP addresses, unless you use the --link option, which is considered
legacy. On a user-defined bridge network, containers can resolve
each other by name or alias.
事实证明,这不适用于 docker 生成的名称。 “practical_lalande”容器名称由 docker 生成。使用“--name”选项创建具有自定义名称的容器可以解决问题。
摘要:我可以使用(自定义)docker 网络将一个 docker 容器连接到另一个容器(在 docker-compose 文件中),只有IP地址。使用容器名称作为 DNS 会导致“错误的网关”错误。
我正在尝试让 Grafana(作为 docker-compose)与 Prometheus(作为 docker 容器)一起工作。我将 prometheus 容器连接到 Grafana docker compose 定义的网络。这样,只要我使用它的 IP 地址,我就可以将 prometheus 服务器用作 Grafana 的数据源。如何使用 DNS 名称使其工作?使用容器名称作为 DNS 名称会导致“错误的网关”错误。
docker-compose.yml
version: '3.4'
networks:
k6:
grafana:
services:
influxdb:
image: influxdb:1.8
networks:
- k6
- grafana
ports:
- "8086:8086"
environment:
- INFLUXDB_DB=k6
grafana:
image: grafana/grafana:latest
networks:
- grafana
ports:
- "3000:3000"
environment:
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_BASIC_ENABLED=false
volumes:
- ./grafana:/etc/grafana/provisioning/
k6:
image: loadimpact/k6:latest
networks:
- k6
ports:
- "6565:6565"
environment:
- K6_OUT=influxdb=http://influxdb:8086/k6
volumes:
- ./samples:/scripts
普罗米修斯容器命令:
docker run -d -p 9090:9090 --network k6_grafana -v C:\xy\QA\K6\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
docker网络检查k6_grafana
[
{
"Name": "k6_grafana",
"Id": "15de38f97d93a0c697de4bfdc4fe0f441bcacde9fd43f5b9d1608908bb26c954",
"Created": "2021-10-26T09:45:17.6385164Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.27.0.0/16",
"Gateway": "172.27.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"37e0a02a2c7f73b2be78623a4d239b86eb2e2a1a14e8b3416d472de2a719d701": {
"Name": "k6_grafana_1",
"EndpointID": "f0bf0f4844775c3fa6ce6758e2e4413ff3de40e5d6b93158e5668ebe4661f6a5",
"MacAddress": "02:42:ac:1b:00:02",
"IPv4Address": "172.27.0.2/16",
"IPv6Address": ""
},
"6efce075e766ce581e134947ef2067d0b3ae433476c850e02768b624c672da0f": {
"Name": "practical_lalande",
"EndpointID": "363c42b1c5194e95d4469f7c474973ba8b21f8fe2573fb22f3fac76c5ab8a1af",
"MacAddress": "02:42:ac:1b:00:04",
"IPv4Address": "172.27.0.4/16",
"IPv6Address": ""
},
"d075b1aa33452c38e0340e4a10997be3665377a43a961e99ecab580af22dfe02": {
"Name": "k6_influxdb_1",
"EndpointID": "4716f96665f4f93acd3cc087d1cad1048ee035062d950fddfe9d3e176a34bcbf",
"MacAddress": "02:42:ac:1b:00:03",
"IPv4Address": "172.27.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "grafana",
"com.docker.compose.project": "k6",
"com.docker.compose.version": "1.28.5"
}
}
]
所以用http://172.27.0.4:9090连接就可以了!但是使用 http://practical_lalande:9090 连接(或任何其他名称)是行不通的!
根据 docker 文档,这应该有效:
User-defined bridges provide automatic DNS resolution between containers.
Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy. On a user-defined bridge network, containers can resolve each other by name or alias.
事实证明,这不适用于 docker 生成的名称。 “practical_lalande”容器名称由 docker 生成。使用“--name”选项创建具有自定义名称的容器可以解决问题。