Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

756
Views
docker-compose hostname and local resolution

When creating docker containers with a docker-compose file which specifies hostname (hostname:) and ip addresses (ipv4_address:), I would like the local computer (the computer that runs the docker daemon service, aka my laptop) to be able to resolve those hostnames, without the need of (too much) manual intervention. I.e. if the container is a webserver service with hostname my_webserver, I would like that my_werbserver resolve to the IP address I assigned to that container.

What's the best way to achieve that? Anything better than maintaining /etc/hosts on my laptop manually?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

As @Kārlis Ābele mentioned, I don't think you can do what you need without additional services. One solution would be to run dnsmasq in a docker container in the same network as the other docker containers. See Make the internal DNS server available from the host

docker run -d --name dns -p 53:53 -p 53:53/udp --network docker_network andyshinn/dnsmasq:2.76 -k -d

Check that it works using localhost as DNS

nslookup bar localhost

Optionally setup localhost as DNS server. On Ubutu 18.04 for example, edit /etc/resolvconf/resolv.conf.d/head.

nameserver localhost

Restart the resolvconf service.

sudo service resolvconf restart

Now you should be able to ping the containers by name.

EDITED: An alternative solution (based on @Kārlis Ābele answer) is to listen to docker events and update /etc/hosts. A barebones implementation:

#!/usr/bin/env bash

while IFS= read -r line; do
  container_id=$(echo ${line}| sed -En 's/.*create (.*) \(.*$/\1 /p')
  container_name=$(echo ${line}| sed -En 's/.*name=(.*)\)$/\1 /p')

  ip_addr=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${container_id})

  echo ${ip_addr} ${container_name} >> /etc/hosts
done < <(docker events --filter 'event=create')
over 4 years ago · Santiago Trujillo Report

0

Well it seems like something that won't be possible without some custom service that is listening for events on Docker daemon...

How I would do this, is write a simple service that is listening for Docker events (https://docs.docker.com/engine/reference/commandline/events/) and updates /etc/hosts file accordingly.

Other than that, without manually updating hosts file I don't think there are other options though.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!