3

Running 4 docker containers: server/client/nginx/mongo reverse-proxy The client seems to work on port 4200, I can see my app and routing works. When trying to register a user, I am requesting the below httpclient post to the Docker container called "myserver" which should call the api. I am getting this DNS error message.

When looking at the NGINX container at the resolv.conf (it shows 127.0.0.11) which is the correct dns Docker resolves the dns-es with within the container. When I ping from within the container to another container's name it works. when I telnet from within and outside of the container on the servers port 3000 it works as well as telnetting on client's port 4200 So what could be the cause of not being able to resolve myserver when I am trying to register an account on my website. it is almost as if the call to register is being made from outside the container, so it can't resolve it, is this normal behavior?

  let url = 'http://myserver:3000/api/register';

docker-compose.yml version: '3'

services:

  nginx:
      build: ./nginx
    # Map Nginx port 80 to the local machine's port 80
      volumes:
        - ./dist:/usr/share/nginx/html
      ports:
        - "80:80"
      depends_on: 
        - client
      networks:
        - app-network
   # Build the container using the client Dockerfile
  client:
      build: ./
    # This line maps the contents of the client folder into the container.
      volumes:
        - ./:/usr/src/app
      ports:
         - "4200:4200"
      networks:
        - app-network

  myserver:
      build: ./express-server
      volumes:
        - ./express-server:/usr/src/server
      environment:
        - NODE_ENV=development
      depends_on:
        - mongo
      ports:
        - "3000:3000"
      networks:
        - app-network

   # Link the client container so that Nginx will have access to it
  mongo:
      environment:
        - AUTH=yes
        - MONGO_INITDB_ROOT_USERNAME=superAdmin
        - MONGO_INITDB_ROOT_PASSWORD=admin123

      image: mongo
      volumes:
        - /var/mongodata/data:/data/db
      ports:
        - "27017:27017"
      networks:
        - app-network   

networks:
   app-network:
      driver: bridge

nginx default.conf

worker_processes 2 ;

events {
    worker_connections 1024;
}
http {
    upstream my-server {
    server myserver:3000;
    }

    upstream client {
    server client:4200;
    }

    server {
        
        location / {
            proxy_pass http://client;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
        location /api/ {
            proxy_pass http://my-server;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Forwarded-For $remote_addr;
    }        
    }
}

cbdb2e00c6db        meanchat_nginx      "nginx -g 'daemon ..."   15 minutes ago      Up 15 minutes       0.0.0.0:80->80/tcp         meanchat_nginx_1
22406a1e9a31        meanchat_client     "npm start"              15 minutes ago      Up 15 minutes       0.0.0.0:4200->4200/tcp     meanchat_client_1
aa024855d201        meanchat_myserver   "npm start"              About an hour ago   Up 15 minutes       0.0.0.0:3000->3000/tcp     meanchat_myserver_1
b657bd6db7b5        mongo               "docker-entrypoint..."   5 hours ago         Up 15 minutes       0.0.0.0:27017->27017/tcp   b657bd6db7b5_meanchat_mongo_1

172.20.0.1 - - [09/Jan/2018:00:30:02 +0000] "GET /sockjs-node/info?t=1515457802565 HTTP/1.1" 200 90 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /register HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /sockjs-node/044/zh1t2skg/websocket HTTP/1.1" 101 162 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /inline.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /polyfills.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /vendor.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /styles.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /scripts.bundle.js HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:03 +0000] "GET /main.bundle.js HTTP/1.1" 200 796563 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:06 +0000] "GET /assets/img/cryptoowls.jpg HTTP/1.1" 304 0 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:06 +0000] "GET /roboto-v15-latin-regular.7e367be02cd17a96d513.woff2 HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
172.20.0.1 - - [09/Jan/2018:00:30:06 +0000] "GET /sockjs-node/info?t=1515457806564 HTTP/1.1" 200 90 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"

172.20.0.1 - - [09/Jan/2018:03:31:24 +0000] "GET /sockjs-node/info?t=1515468684563 HTTP/1.1" 200 90 "http://localhost/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"

oudekaas
  • 325
  • 1
  • 6
  • 21
  • 1
    Do you have any logs from nginx showing that it is failing to resolve the hostname? How did you isolate the problem to there and not something like your application? There's nothing to go on in this question if the problem is with the application itself. – BMitch Jan 09 '18 at 00:47
  • good point, my application works without docker/ containers I didn't change anything to it since I started to dockerize, I added nginx logs, can't find anything there. Also the resolving issue didn't happen for a while and now it is back again. I am not sure what I changed to make it come back. – oudekaas Jan 09 '18 at 00:55
  • when I start everything without Docker, I am not getting any timeouts or resolve issues on port 3000. I do get: Failed to load http://localhost:3000/api/register: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. Which I think is a cors issue, could this be related? – oudekaas Jan 09 '18 at 01:08
  • 1
    If you're not seeing the error in nginx, then where are you seeing it? – BMitch Jan 09 '18 at 01:09
  • in the browser console, sorry I just ran it without docker I will try and see if I can find it in the logs again, running it in Docker! Also I thought the No 'Access-Control-Allow-Origin' problem would be resolved by using nginx.. – oudekaas Jan 09 '18 at 01:23
  • 1
    Something gave the browser that URL to go to, which would be your application. Nginx is not changing the content of the returned web pages, and you've described Docker as having the expected behavior with DNS. We don't have enough details of your application to tell you what's wrong with it, but it shouldn't be sending internal container URL's to an external browser. – BMitch Jan 09 '18 at 01:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162775/discussion-between-oudekaas-and-bmitch). – oudekaas Jan 09 '18 at 01:30
  • update: I rechecked. If I just fire up the app outside of Docker with a local mongo database it works, using Cors. Now with the Docker on again, I am still getting the resolve issue and when using the ip of the container a time out. Is my config file correct should it be /api or /api/? nginx log now shows: when using the ip 172.20.0.4 (myserver) 172.20.0.1 - - [09/Jan/2018:03:31:24 +0000] "GET /sockjs-node/info?t=1515468684563 HTTP/1.1" 200 90 "http://localhost/register" "Mozilla/5 – oudekaas Jan 09 '18 at 03:30
  • omg, days it took me and @BMitch you saved the day! How do I flag your comment as answer: All I had to do was change: let url ="http://myserver/api/register" to let url = '/api/registerCoin'; As you say the browser doesn't know the container domain names. – oudekaas Jan 10 '18 at 02:10

1 Answers1

0

Answer from comments:

When connecting to containers with a reverse proxy, all the URL's used by the application need to point to that reverse proxy and not the application. Typically you do this by giving a path in the URL without a hostname. If you plan to use a virtual path based reverse proxy, you'll want to go a step further and use relative paths in your URL's. This all needs to happen in the responses being sent by the application to the client.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • https://stackoverflow.com/questions/48180767/nginx-reverse-proxy-params-docker just running into another issue now – oudekaas Jan 10 '18 at 12:56
  • Do you have an idea of to overcome this problem when using sockets? When using sockets, you need to provide the port to which you want to connect, therefore (AFAIK) you must provide a base/host address. Do you know how can one solve the above issue in such case? – Hummus Jan 06 '20 at 18:28
  • @Hummus I believe that's a different question from the one here. Use the "Ask Question" link at the top. – BMitch Jan 06 '20 at 18:32