Docker compose Docker-从容器调用主机API端点(localhost)。主机名无效HttpCode 400错误

Docker compose Docker-从容器调用主机API端点(localhost)。主机名无效HttpCode 400错误,docker-compose,containers,Docker Compose,Containers,我知道有人问过这个问题,但是一些人在不久前就开始使用了,docker版本从那时起就改变了 我一直试图从docker(docker desktop)容器内的服务调用主机中的REST API端点(),但我得到的HTTP代码400主机名无效 正如这里所建议的(),我使用host.docker.internal解析主机中的localhost,这是我的docker-compose.yml: version: '3.5' services: api: build: context:

我知道有人问过这个问题,但是一些人在不久前就开始使用了,docker版本从那时起就改变了

我一直试图从docker(docker desktop)容器内的服务调用主机中的REST API端点(),但我得到的HTTP代码400主机名无效

正如这里所建议的(),我使用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插入到容器中,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 Desktop for Windows应用程序,看起来像(不是Docker工具箱,不是Linux或Mac主机)?您是否收到HTTP 400错误或“无效主机”错误(这将阻止HTTP连接)?是windows docker desktop。日志中的错误是“HTTP错误400。请求主机名无效”。如果您收到HTTP 400错误,这意味着您成功连接到主机系统上的服务,并且它正在将该错误发回。(主机名将在HTTP
host:
头中发送,
host.docker.internal
主机名,如果服务器尝试根据该头进行路由,理论上它可以给出此响应。)