2

I have an Angular app running in a NginX Docker container.

The Angular app can make a REST call to another container (Spring Boot API Docker) using localhost as server.

server: string = 'localhost';
return this.httpClient.post<MyClass[]>('http://' + this.server + ':6060/files-data/list-files', request);

But REST call fails when using Docker service name instead.

server: string = 'files-service';
return this.httpClient.post<MyClass[]>('http://' + this.server + ':8080/files-data/list-files', request);

*using 8080 port this time as trying to resolve docker service name

Error in UI console:

net::ERR_NAME_NOT_RESOLVED

Using default NginXimage with default nginx.conf.

  nginx-files-ui:
    image: nginx:latest
    container_name: nginx-files-ui
    volumes:
      - ./files-ui/dist/html:/usr/share/nginx/html
    ports:
      - 99:80

Containers all run on same Docker network.

networks:
  default:
    external:
      name: files-net

Modified nginx.conf to include resolver but no joy.

resolver 127.0.0.11 valid=30s;

Any idea why I cannot make a REST call using Docker service name ?

EDIT

It seems Angular code running in browser is not able to resolve service names.

Docker composed services can't communicate by service name

gary
  • 425
  • 5
  • 20

1 Answers1

7

I think this is the very common misunderstanding when running application inside docker container, especially for browser apps :).

You can only call API using service name in your app if your app and the api are running and being accessed within same network. In your case, the Angular app and your backend are running in same network,but the Angular app is being accessed from host machine, not inside container.

Detail: In current context, when you access your Angular app from browser, you're using the Angular app in host machine, in host network, not inside container anymore, your Angular app is exposed by mapping port from container to host machine. Therefore when you make an API request, it'll coming from host machine -> docker container, at that time, host machine will try to resolve service name in current context and of course from host machine there's no service name

Therefore, for all browser apps, even you deploy it using docker container, but when you call API, mostly you'll have to use localhost:<mapped_port> or something that resolves to host machine not inside docker container (Eg: domain name).

Duc Trung Mai
  • 2,141
  • 1
  • 24
  • 23
  • 2
    That's a very good and clear explanation. I should have know this as I'm relatively experienced with Docker but missed it. Bit silly. Hope my question and your explanation will save somebody else time in the future. – gary Jan 18 '21 at 08:55
  • You're welcome, I was struggling with this problem in the early day when I worked with Docker :D – Duc Trung Mai Jan 18 '21 at 09:18
  • This explanation saved me. I was mapping the service name to the /api/ in the nginx.conf and was confused why it was not working because when you do a docker exec on the nginx container and do a curl to the api, it works. So thank you! – carloliwanag Apr 27 '23 at 07:48