I have several services packaged in a docker container. Each service will communicate with each other using the consul server which acts as the default DNS server. Where each service can communicate using Consul's FQDN such as (service-a.service.consul) or (service-b.service.consul).
My approach has worked by building a docker image containing my API binaries along with dnsmasq which will be used to forward DNS queries to my Consul DNS server listening on port 8600/udp 8600/tcp as the default configuration of Consul itself.
docker-compose.yml:
version: "3"
services:
consul-server:
container_name: consul-server
image: hashicorp/consul:1.15
ports:
- 8500:8500
- 8600:8600
- 8600:8600/udp
command: "agent"
networks:
- sample-net
service-a:
container_name: service-a
image: service-a
ports:
- 3000:80
networks:
- sample-net
depends_on:
- consul-server
dns:
- 127.0.0.1
- 1.0.0.1
- 1.1.1.1
networks:
sample-net:
driver: bridge
My problem is, because I use a local DNS server on each container so that when my container makes an http request with the domain service-*.consul.service it can be resolved and forwarded to my Consul Server, but when I want to access the outside web like google .com, that I can not.
Can anyone help or give advice regarding my case?
When I remove the dns setting on my docker container
service-a:
container_name: service-a
image: service-a
ports:
- 3000:80
networks:
- sample-net
depends_on:
- consul-server
I can also ping external websites
/app # ping google.com
PING google.com (216.239.38.120): 56 data bytes
64 bytes from 216.239.38.120: seq=0 ttl=116 time=40.006 ms
64 bytes from 216.239.38.120: seq=1 ttl=116 time=47.230 ms
--- google.com ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 36.226/41.470/47.247 ms
But I can't ping other services on my docker network
/app # ping service-a.service.consul
ping: bad address 'service-a.service.consul'
The same is true when I set the dns server to the container's locale ie: 127.0.0.1, now I can ping other services using the Consul FQDN but not the external web.
/app # ping service-a.service.consul
PING service-a.service.consul (192.168.64.3): 56 data bytes
64 bytes from 192.168.64.3: seq=0 ttl=64 time=0.378 ms
64 bytes from 192.168.64.3: seq=1 ttl=64 time=0.176 ms
--- service-a.service.consul ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.174/0.220/0.378 ms
My goal is to be able to communicate between services using Consul FQDN, and also outside internet networks