简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM