DOCKER - Use system’s resolver in dockerized NGINX

less than 1 minute read

NGINX config Template


http {
  server {

    resolver $NAMESERVER valid=10s;


Docker Entrypoint

Use DNS server from Dockerhost for Container.

Your startup script can then use the envsubst program to generate an nginx.conf and then start nginx. eg:


if [ "$NAMESERVER" == "" ]; then
	export NAMESERVER=`cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}' | tr '\n' ' '`

echo "Nameserver is: $NAMESERVER"

echo "Copying nginx config"
envsubst '$NAMESERVER' < /nginx.conf.tmpl > /nginx.conf

echo "Using nginx config:"
cat /nginx.conf

echo "Starting nginx"
nginx -c /nginx.conf -g "daemon off;"