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

时间:2019-06-27 22:06:18

标签: docker-compose containers

我知道有人问过这个问题,但有人早就让它工作了,此后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解析主机中的本地主机,这是我的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

0 个答案:

没有答案