Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

570
Vistas
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?

9 months ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

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')
9 months ago · Santiago Trujillo Denunciar

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.

9 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.