diff --git a/roles/container-engine/docker/tasks/set_facts_dns.yml b/roles/container-engine/docker/tasks/set_facts_dns.yml
index 99b9f0e26e1788c14c39b682f635c732f76e5805..23464dabb0470778f3b4ec157f8e303b4de1dbcf 100644
--- a/roles/container-engine/docker/tasks/set_facts_dns.yml
+++ b/roles/container-engine/docker/tasks/set_facts_dns.yml
@@ -29,13 +29,13 @@
     docker_dns_search_domains: "{{ docker_dns_search_domains + searchdomains|default([]) }}"
 
 - name: check system nameservers
-  shell: grep "^nameserver" /etc/resolv.conf | sed 's/^nameserver\s*//'
+  shell: grep "^nameserver" /etc/resolv.conf | sed -r 's/^nameserver\s*([^#\s]+)\s*(#.*)?/\1/'
   changed_when: False
   register: system_nameservers
   check_mode: no
 
 - name: check system search domains
-  shell: grep "^search" /etc/resolv.conf | sed 's/^search\s*//'
+  shell: grep "^search" /etc/resolv.conf | sed -r 's/^search\s*([^#]+)\s*(#.*)?/\1/'
   changed_when: False
   register: system_search_domains
   check_mode: no