Docker Compose Tip #36: Using extra_hosts for custom DNS entries
Need custom DNS resolution in containers? Use extra_hosts to add hostname mappings without touching system files! Basic extra_hosts usage Add custom host entries to containers: services: app: image: myapp extra_hosts: - "api.local:192.168.1.100" - "db.local:192.168.1.101" - "cache.local:192.168.1.102" Inside the container: docker compose exec app cat /etc/hosts # 127.0.0.1 localhost # 192.168.1.100 api.local # 192.168.1.101 db.local # 192.168.1.102 cache.local Dynamic host resolution Use host machine’s IP dynamically: services: app: image: myapp extra_hosts: - "host.docker.internal:host-gateway" # Magic value! This maps to: ...