The docker compose file:
version: '3'
services:
rs:
build: .
ports:
- "9090:9090"
consul:
image: "consul"
ports:
- "8500:8500"
hostname: "abc"
rs is a go micro server app which will access consul and the consul address configured in a file like:
"microservice": {
"serviceName": "hello",
"registry": "consul=abc:8500",
However this don't work, the rs report error log:
register error: Put http://abc:8500/v1/agent/service/register: dial tcp: lookup abc on 127.0.0.11:53: no such host
I can access consul ui in host machine by http://127.0.0.1:8500, it works properly.
How to configure the network let rs can access consul?
You have changed hostname of the consul container, however
rsservice is not aware of this, and it attempts to resolveabcby querying default DNS server which is127.0.0.11port53. You can see this in error message, since this DNS server is unable to resolveabcas it has no information about it.The easiest way to solve this, and have it working in docker-compose, on the network created between the services in docker-compose is following:
This will create new network
rs-consul(check withdocker network ls, it will have some prefix, mine hadworking_directory_name_as prefix). And on this network the Consul machine has aliasabc. Now yourrsservice should be able to reach Consul service viahttp://abc:8500/I have used commented lines (
image:alpine:3.7andcommand: sleep 600) instead ofbuild: ., to test the connection, since I don't have yourrsservice code to use inbuild:. Once containers are started, I useddocker exec -it <conatiner-id> shto start shell onrscontainer, then installedcurland was able to retrieve Consul UI page via following command:Hope this helps.