0

My first webserver (without Docker) is listening on http://127.0.0.1:5000. I have an nginx proxy (Docker) to forward d5000.local:80 to http://127.0.0.1:5000. My hosts file maps the domain to the IP:

#etc/hosts
127.0.0.1 d5000.local

Now I run curl -v http://d5000.local on my host. And everything works fine.

I have an second webserver in a docker container (bridged network) on my development machine. If I go into the container and run curl -v http://d5000.local I get a response from the second webserver instead from the first webserver. It seems that the container is using the hosts file of the host. The container requests its own lookup address instead the lookup address of the host.

How can I request the webserver on the host from my docker container by domain name (d5000.local)? Do I need a DNS? What is the best practise for a development machine?

Details: I need to call the first webserver from my browser and from the second webserver with the same address. It is an OAuth-Server with a front- and a backchannel.

koalabruder
  • 2,794
  • 9
  • 33
  • 40

2 Answers2

1

It's fairly simple to make requests within a container to your local host-maschine

curl -v http://host.docker.internal:<port>

Found it here: stack overflow: Add postgres connection to pgadmin wihtin docker container

Docs: docker docs: i want to connect from a container to a service on the host

Pascal
  • 108
  • 1
  • 7
0

It works with extra_hosts in the docker-compose.yml. --add-host should work too (https://docs.docker.com/engine/reference/commandline/run/#add-entries-to-container-hosts-file---add-host).

# docker-compose.yml
services:
  secondWebserver:
    # ...
    extra_hosts: 
      - "d5000.local:172.17.0.1"

On my machine it works with the IP 172.17.0.1. If there is a better way please leave a comment or write an answer.

https://docs.docker.com/network/network-tutorial-standalone/#use-user-defined-bridge-networks

... the default bridge network, whose gateway is 172.17.0.1. The exact IP address may be different on your system.

koalabruder
  • 2,794
  • 9
  • 33
  • 40