首页 > 解决方案 > Docker - 从容器调用主机 API 端点 (localhost)。主机名是无效的 HttpCode 400 错误

问题描述

我知道有人问过这个问题,但有些人不久前就开始工作了,此后 docker 版本发生了变化。

我一直在尝试从 docker(docker 桌面)容器内的服务调用主机中的 REST API 端点(http://localhost:9015/api/v1),但我一直在获取 HTTP Code 400 HostName 是无效的。

正如这里所建议的(https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248),我使用 host.docker.internal 来解析主机中的 localhost,这是我的 docker -compose.yml:

version: '3.5'

services:
api:
    build: 
      context: ./src/api
    environment:
      - PricingServiceUrl=http://host.docker.internal:9015/api/v1
    ports:
      - "8081:80"

这只是本地 pc 开发,没有 nginx,没有 LB,这是我在 pc 上安装的 docker 信息:

Docker version 18.09.2, build 6247962

Server Version: 18.09.2
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce
runc version: 09c8266bf2fcf9519a651b04ae54c967b9ab86ec
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.125-linuxkit
Operating System: Docker for Windows
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.934GiB
Name: linuxkit-00155d5c3a0f
ID: 57VL:MG6S:GAMM:QFDJ:73D2:B7QT:3I44:LB3Y:2XC4:EUJ4:A2QG:HGXC
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 125
 Goroutines: 177
 System Time: 2019-06-27T22:05:10.5740059Z
 EventsListeners: 4
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

此外,我 ssh-ed 进入容器并 ping 设法解析主机 ip,但为什么仍然得到主机名无效?

ping host.docker.internal
PING host.docker.internal (192.168.65.2) 56(84) bytes of data.
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=1 ttl=37 time=0.396 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=2 ttl=37 time=1.51 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=3 ttl=37 time=0.672 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=4 ttl=37 time=1.09 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=5 ttl=37 time=0.795 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=6 ttl=37 time=0.782 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=7 ttl=37 time=1.03 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=8 ttl=37 time=0.829 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=9 ttl=37 time=0.831 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=10 ttl=37 time=0.637 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=11 ttl=37 time=0.678 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=12 ttl=37 time=0.562 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=13 ttl=37 time=0.566 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=14 ttl=37 time=0.474 ms

标签: docker-composecontainers

解决方案


推荐阅读