Docker Community Forums

Share and learn in the Docker community.

Docker network with nginx


I have a Docker Network with the following containers:

  • app - ports “80:80”
  • nginx_api - ports “81:80”

In app, there is an nginx listening to 80 and doing a proxy_pass to nginx_api:81 like this:

# app - nginx.con
http {
    server {
        location /api {
            proxy_pass    http://nginx_api:81/;

When I run the network and the containers and try to query, from a browser, the address, nginx from app replies with the following error (and nginx from nginx_api is not even reached):

[error] 6#0: *2 connect() failed (111: Connection refused) while connecting to upstream,
    request: "GET /api/posts/ HTTP/1.1",
    upstream: ",
    host: "localhost",
    referrer: "http://localhost/"

However, when I directly query, the API responds accordingly (nginx_api works well).

Funny thing is, in the error message above, is the correct address for nginx_api container (checked with docker network inspect).

I might be missing something here.

Important notes:

The two containers above are run with two different docker-compose.yml files, separately. The network they both belong to was created beforehand (docker network create my-network).

If I log into app container, I can ping nginx_api without any problem.

Any idea of how to make the proxy_pass to nginx_api:81 work in this configuration? Thanks!

The two containers above are run with two different docker-compose.yml files, separately.
Why not run from same docker-compose file? Would fix the networking issue.

I have no idea if this is the right answer as I’m still very new to this, but I know docker-compose supports external link - that might help here:

proxy_pass http://nginx_api:81/;

I think you have to use the dns name pattern {containername}.{networkname}, so I’d give a try

I fixed this by removing the port number.

 proxy_pass http://nginx_api;

The I found out by I logging into my nginx container trying to curl the url specified by proxy_pass and getting a 'Connection refused' style error. Removing the port fixed the issue. I now have nginx running in a container proxying 2 domains to 2 separate services