Docker Community Forums

Share and learn in the Docker community.

Container can access hosts dns records but service task cant

I have added a few dns entires to my hosts /etc/hosts file.
If I start a container I can ping these dns entries (even though they are on my host, not the container itself) but if my container gets started by a service it can’t ping these dns records.

Is this the intended behaviour or am I missing something?

s per our conversation, here is bit summary of what’s happening.

If Service1(nginx in your case) needs to interact with Service2(redis) with AWS ServiceDiscovery option and use of SRV records then Service1 needs to be aware that it needs to perform DNS SRV lookup instead of DNS A(Address) lookup.
You have multiple options here. First, if you want to continue to use the SRV records use then your client nginx needs to proxy redis upstream server with options of service and resolve which are available only in premium version of nginx. Check my sample nginx configuration I have tested at the bottom of the answer which works.

Also make sure, you create the AWS Service discovery name with prefix _http._tcp otherwise, I had issues configuration SRV resolve/service option in nginx configuration without the prefix.

aws ecs service

Other option, If you do not want to rely on SRV records but go to standard A record lookup then you will have to use awsvpc mode for containers and select A option.
enter image description here

With DNS A option then your query of service_discovery_service_name.service_discovery_namespace will work fine.
With DNS A option, there are some constraints. You cannot create multiple tasks for a given service on same EC2 instance due to number of ENIs limit depending EC2 instance family so I would prefer SRV records only.