DA--
DA--

Reputation: 765

DNS in docker-compose, map to non-80 port?

While experimenting with docker-compose, I'm running into some issues with NGINX and the dns. The error is the regular "connection refused while connecting to upstream" in NGINX. I think the problem arises due to the port numbers.

Examples online like this one run the NGINX on port 80, which doesn't cause the issue.

The docker-compose.yml:

version: '3'
services:
  http-server:
    networks:
      - mynetwork
    image: nginx_image
    ports:
      - 8080:8080
    depends_on:
      - frontend
      - rest_api
  frontend:
    networks:
      - mynetwork
    image: frontend_image
    ports:
      - 8001:8001
  rest_api:
    networks:
      - mynetwork
    image: rest_api_image
    ports:
      - 8000:8000

networks: mynetwork: driver: bridge

nginx.conf for nginx_image has this block:

server {
    listen 8080;

location /static/js/  {
  proxy_pass http://frontend;
    }

location /static/css/  {
    proxy_pass http://frontend;
    }

location /static/ {
    proxy_pass http://rest_api;
}

location / {
proxy_pass http://frontend;
    }

location /rest_api/ {
    proxy_pass http://rest_api;
    }
}

Now, both the frontend and api are called over port 80, while it should be 8001 and 8000.

What am I missing? I would expect docker-compose to make the port mapping automatically.

Thanks in advance!

Kind regards,

DA

EDIT1: here's the error (including the hostname suggestion)

EDIT2: updated the question

http-server_1  | 2018/06/04 14:47:50 [error] 14#14: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /rest_api/admin/ HTTP/1.1", upstream: "http://172.18.0.2:80/rest_api/admin/", host: "localhost:8080"

Upvotes: 1

Views: 490

Answers (1)

Maxim Antonov
Maxim Antonov

Reputation: 305

you have to specify hostnames:

  frontend:
    image: frontend_image
    hostname: frontend
    ports:
      - 8001:8001
  rest_api:
    hostname: rest_api

Upvotes: 1

Related Questions