Docker - Calling host API endpoint (localhost) from container. HostName is Invalid HttpCode 400 error

Viewed 1867

I know this has been asked but some people got it working a while ago and the docker version has changed since then .

I have been trying to call a REST API endpoint (http://localhost:9015/api/v1) which in the host from the service inside of the docker (docker desktop) container, but I have been getting HTTP Code 400 HostName is invalid.

As suggested here (https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248), I use host.docker.internal to resolve localhost in host and this is my docker-compose.yml:

version: '3.5'

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

This is just local pc development, no nginx, no LB and this is my docker info installed on the pc:

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

Furthermore, I ssh-ed into the container and ping managed to resolve the host ip but why am still getting hostname is invalid ?

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 Answers
Related