Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to docker container using host name instead of localhost

I am using docker-compose and docker. everything works great. but the only way to access a service from the host machine is by http://localhost:port. i would like to be able to access by using the container name like: http://elastic:9020.

It works well inside the docker. if one container needs to access another i can use the http://elastic:9020 but not from the Host. Of Course that i am exposing the ports and using port mapping and it works using localhost as i said. Docker

UPDATE: I was looking for a native solution without the needs of installing DNS Server or updating the .host file.

like image 371
Omtechguy Avatar asked Sep 05 '19 17:09

Omtechguy


1 Answers

There is an OSS project aimed for resolving Docker's hostnames:

mageddo/dns-proxy-server: Solve your DNS hosts from your docker containers, then from your local configuration, then from internet

Main features:

  • Solve hostnames from local configuration database
  • Solve hostnames from docker containers using docker hostname option or HOSTNAMES env
  • Solve hostnames from a list of configured DNS servers(as a proxy) if no answer of two above
  • Solve hostnames using wildcards
  • Graphic interface to Create/List/Update/Delete A/CNAME records
  • Solve host machine IP using host.docker hostname
  • Access container by it’s container name / service name
  • Specify from which network solve container IP

Howto use:

docker run --rm --hostname dns.mageddo \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /etc/resolv.conf:/etc/resolv.conf \ defreitas/dns-proxy-server

like image 69
Yasen Avatar answered Oct 26 '22 02:10

Yasen