Nginx does not resolve hostname

Hi everyone, I have a few containers running with docker-compose with one of them running an Nginx reverse-proxy. I’m trying to forward a particular route to another container as per the following directive:

location ~ /wp-content/themes/.*/main\..*\.(css|js).* {
  error_log /dev/stdout;
  proxy_pass http://webpack:8080/main.$1;
}

However it’s returning following error:

[error] 31#31: *6 webpack could not be resolved (3: Host not found)

The hosts “nginx” and “webpack” are defined in the docker-compose.yml file as so (I’m removing unnecessary details for brevity):

nginx:
    image: nginx:local
    healthcheck:
      test: "curl -f 127.0.0.1"
      retries: 3
    depends_on:
      wordpress:
        condition: service_healthy
      webpack:
        condition: service_started


  webpack:
    image: node:16-buster-slim
    healthcheck:
      test: "node -v"
      retries: 3

Oddly enough, if I instead attach to the “nginx” container and try to run curl against the “webpack” container, it works without issues.

# Attach to nginx container (with all containers up, of course)
$ docker-compose exec nginx /bin/bash

# Contact the webpack container which fails to resolve
root@1eccb1ab5dd5:/# curl -fso /dev/null -D - http://webpack:8080/main.js

HTTP/1.1 200 OK
X-Powered-By: Express
Accept-Ranges: bytes
Content-Type: application/javascript; charset=UTF-8
Content-Length: 9598
ETag: W/"257e-WZ+gwBL51WZ43ve2ZIPolu4Nnb4"
Date: Thu, 30 Sep 2021 00:16:01 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Has anyone any ideas as to why could this possibly happen? I have verified that the containers are all indeed under the same network created by docker-compose, and reachable from one another by running similar tests as above.

Thank you, any help is much appreciated!

1 Like

So it appears the problem is Nginx does not use the default Docker resolver when the hostname appears after proxy_pass for some reason. This means I would have to specify the resolver IP address specifically using the resolver function, but how can I find out the IP address used by Docker so that I can resolve it in this instance?

You might want to take a look at an older post of mine:

1 Like

That was spot on, setting up the resolver at that address ( I wasn’t sure which one should I have used) was all that is needed. Thanks very much for that !